There's no need to manually set the colors. Instead, specify a grayscale colormap...

import numpy as np
import matplotlib.pyplot as plt

# Generate data...
x = np.random.random(10)
y = np.random.random(10)

# Plot...
plt.scatter(x, y, c=y, s=500) # s is a size of marker 
plt.gray()

plt.show()

Or, if you'd prefer a wider range of colormaps, you can also specify the cmap kwarg to scatter. To use the reversed version of any of these, just specify the "_r" version of any of them. E.g. gray_r instead of gray. There are several different grayscale colormaps pre-made (e.g. gray, gist_yarg, binary, etc).

import matplotlib.pyplot as plt
import numpy as np

# Generate data...
x = np.random.random(10)
y = np.random.random(10)

plt.scatter(x, y, c=y, s=500, cmap='gray')
plt.show()
Answer from Joe Kington on Stack Overflow
🌐
Statology
statology.org β€Ί home β€Ί matplotlib: how to color a scatterplot by value
Matplotlib: How to Color a Scatterplot by Value
September 3, 2020 - Fortunately this is easy to do using the matplotlib.pyplot.scatter() function, which takes on the following syntax: matplotlib.pyplot.scatter(x, y, s=None, c=None, cmap=None) ... You can use c to specify a variable to use for the color values and ...
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί how-to-color-scatterplot-by-a-variable-in-matplotlib
How to Color Scatterplot by a variable in Matplotlib? - GeeksforGeeks
July 23, 2025 - Example 2: Color Scatterplot point with dependent values. In this example, We will plot a variable depending on another variable. Sometimes we need precisely based visualization so in this case, It can help us to visualize the data that depend on another variable. Here we will use three different values for each point and use colormap with specific data. ... import matplotlib.pyplot as plt x =[5, 7, 8, 7, 3, 4, 5, 6, 7] y =[99, 86, 87, 88, 4, 43, 34, 22, 12] z = [1,2,3,4, 5, 6, 7, 8, 9] plt.scatter(x, y, c = z , cmap = "magma")
🌐
Matplotlib
matplotlib.org β€Ί stable β€Ί api β€Ί _as_gen β€Ί matplotlib.pyplot.scatter.html
matplotlib.pyplot.scatter β€” Matplotlib 3.11.2 documentation
Otherwise, value-matching will have precedence in case of a size matching with x and y. If you wish to specify a single color for all points prefer the color keyword argument. Defaults to None. In that case the marker color is determined by the value of color, facecolor or facecolors.
🌐
Python Guides
pythonguides.com β€Ί matplotlib-scatter-plot-color
Matplotlib Scatter Marker: Shape, Size, Color
July 16, 2025 - Matplotlib scales your numbers to the 0–1 range and looks each one up in the colormap. The default is viridis. Always add the colorbar. Without it the colours are decoration, because nobody can tell which shade means which value.
🌐
W3Schools
w3schools.com β€Ί python β€Ί matplotlib_scatter.asp
Matplotlib Scatter
You can specify the colormap with the keyword argument cmap with the value of the colormap, in this case 'viridis' which is one of the built-in colormaps available in Matplotlib. In addition you have to create an array with values (from 0 to ...
🌐
Python Examples
pythonexamples.org β€Ί matplotlib-scatter-plot-color-based-on-condition
Matplotlib - Scatter Plot Color based on Condition
Let us put all the above mentioned steps together, and write a program for a scatter plot where the data points are colored 'red' or 'blue' based on the condition applied on data points. import matplotlib.pyplot as plt import numpy as np # Number of data points num_points = 50 # Generate random values for X and Y x_values = np.random.rand(num_points) y_values = np.random.rand(num_points) # Assign colors based on conditions (e.g., higher or lower than a threshold) colors = np.where(y_values > 0.5, 'red', 'blue') # Create scatter plot with customized colors plt.scatter(x_values, y_values, c=colors, label='Scatter Plot') # Customize the plot plt.title('Scatter Plot with Customized Colors') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.legend() # Show the plot plt.show()
🌐
py4u
py4u.org β€Ί blog β€Ί python-scatter-plot-different-colors-depending-on-value
How to Color Python Scatter Plot Points by Value in DataFrame (Matplotlib Tutorial)
In this tutorial, we’ll walk through how to create scatter plots in Python using Matplotlib, where points are colored by values from a Pandas DataFrame. We’ll cover both categorical (e.g., "Group A," "Group B") and numerical (e.g., 10, 20, 30) variables, customize color maps, add color ...
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com β€Ί how-to-create-a-scatter-plot-with-several-colors-in-matplotlib
How to Color Scatterplot by a variable in Matplotlib?
May 10, 2023 - import matplotlib.pyplot as plt ... You can assign specific colors to different categories by passing a list of color names or values to the c parameter....
🌐
Matplotlib
matplotlib.org β€Ί 3.5.0 β€Ί api β€Ί _as_gen β€Ί matplotlib.pyplot.scatter.html
matplotlib.pyplot.scatter β€” Matplotlib 3.5.0 documentation
June 17, 2020 - A single color format string. Note that c should not be a single numeric RGB or RGBA sequence because that is indistinguishable from an array of values to be colormapped. If you want to specify the same RGB or RGBA value for all points, use a 2D array with a single row.
🌐
Reedgroup
reedgroup.github.io β€Ί FigureLibrary β€Ί scatterplot_colorbar.html
Scatter plots and colorbars using Matplotlib β€” Reed group lab manual
# import data df_scatter = pd.read_csv("scatter_colorbar.csv", index_col=0) # extract the data to plot RF = df_scatter['RF'] inf_trigger = df_scatter['inf_trigger'] RF_regret = df_scatter['RF_regret'] Before we plot any colors, its generally good practice to normalize the values of the variable we would like to plot as the color.
🌐
TutorialsPoint
tutorialspoint.com β€Ί how-to-color-a-matplotlib-scatterplot-using-a-continuous-value
How to color a Matplotlib scatterplot using a continuous value?
June 4, 2021 - To color a matplotlib scatterplot using continuous value, we can take the following steps βˆ’ Set the figure size and adjust the padding between and around the subplots. Create x, y an
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί how-to-create-a-scatter-plot-with-several-colors-in-matplotlib
How to create a Scatter Plot with several colors in Matplotlib? - GeeksforGeeks
January 10, 2024 - Colormap instances are used to convert data values (floats) from the interval [0, 1] to the RGBA color. In this example, the code utilizes the Matplotlib library to create a scatter plot.
🌐
Moonbooks
moonbooks.org β€Ί Articles β€Ί How-to-create-a-scatter-plot-with-several-colors-in-matplotlib-
How to create a scatter plot with several colors in matplotlib ?
April 5, 2019 - import matplotlib.pyplot as plt import numpy as np a = np.array([[ 1, 1.5, 2.5, 3, 3.5, 6.5, 5, 6, 7, 8, 7.5 ], [ 8, 11, 10, 8, 12, 4.3, 4, 7, 2, 5, 7.5 ]]) categories = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]) # Supervised #categories = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) # Unsupervised color1=(0.69411766529083252, 0.3490196168422699, 0.15686275064945221, 1.0) color2=(0.65098041296005249, 0.80784314870834351, 0.89019608497619629, 1.0) colormap = np.array([color1,color2]) plt.scatter(a[0], a[1], s=500, c=colormap[categories]) plt.scatter(2, 6, s=500, c='k') plt.text(2.4, 5.7, '?', fontsize=16) plt.text(1.4, 8, 'Label 1', fontsize=16) plt.text(6.5, 5.8, 'Label 2', fontsize=16) plt.title('Supervised Learning') plt.savefig('ScatterClassPlot.png') plt.show()
🌐
Plotly
community.plotly.com β€Ί πŸ“Š plotly python
Change colour of curve according to its y-value in matplotlib - πŸ“Š Plotly Python - Plotly Community Forum
December 30, 2021 - Hello, there is the possibility ... numpy as np from matplotlib import cm x = np.linspace(0, 2*np.pi, 1000) y = np.sin(2*x) plt.scatter(x,y, c=cm.hot(np.abs(y)), edgecolor='none') plt.show() ......
🌐
Matplotlib
matplotlib.org β€Ί 3.1.1 β€Ί api β€Ί _as_gen β€Ί matplotlib.pyplot.scatter.html
matplotlib.pyplot.scatter β€” Matplotlib 3.1.2 documentation
Fundamentally, scatter works with 1-D arrays; x, y, s, and c may be input as 2-D arrays, but within scatter they will be flattened. The exception is c, which will be flattened only if its size matches the size of x and y. ... In addition to the above described arguments, this function can take a data keyword argument. If such a data argument is given, the following arguments are replaced by data[<arg>]: All arguments with the following names: 'c', 'color', 'edgecolors', 'facecolor', 'facecolors', 'linewidths', 's', 'x', 'y'.
🌐
YouTube
youtube.com β€Ί watch
Scatter plot with third variable as color | Python Matplotlib - YouTube
How to make and customize a color map and color bar in pythonChoosing Colormaps in Matplotlib:https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html---
Published: March 3, 2021