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
๐ŸŒ
Matplotlib
matplotlib.org โ€บ stable โ€บ gallery โ€บ color โ€บ color_by_yvalue.html
Color by y-value โ€” Matplotlib 3.11.0 documentation
Use masked arrays to plot a line with different colors by y-value. import matplotlib.pyplot as plt import numpy as np t = np.arange(0.0, 2.0, 0.01) s = np.sin(2 * np.pi * t) upper = 0.77 lower = -0.77 supper = np.ma.masked_where(s < upper, s) slower = np.ma.masked_where(s > lower, s) smiddle ...
๐ŸŒ
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 ...
Discussions

python - How to color scatter markers as a function of a third variable - Stack Overflow
In this case with continuous numbers, I suspect you would use the colorbar. What about in the case of discrete values? e.g. could I add 3 labels in the legend for my three categories? 2017-06-19T16:37:03.353Z+00:00 ... In matplotlib grey colors can be given as a string of a numerical value ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Change colour of curve according to its y-value in matplotlib
Hello, there is the possibility to color the y-values according to their value in matplotlib like import matplotlib.pyplot as plt import 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() ... More on community.plotly.com
๐ŸŒ community.plotly.com
2
0
December 30, 2021
python - How to change data points color based on some variable - Stack Overflow
I have 2 variables (x,y) that change ... for lowest value is dark red, and for intermediate values the color will be scaled in between green and red. ... Save this answer. ... Show activity on this post. This is what matplotlib.pyplot.scatter is for.... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to color different segments of plotted curve based on value?
I doubt that the Pandas methods will let you do this, so you'd probably need to create a Matplotlib "linecollection" from the dataframe and use Matplotlib's plotting methods. See this example: import numpy as np import matplotlib.pyplot as plt from matplotlib.collections import LineCollection x = np.linspace(0,1, 1000) y = np.sin(x * 4 * np.pi) colorvalue = x * 4 % 1 points = np.vstack((x, y)).T.reshape(-1, 1, 2) segments = np.hstack((points[:-1], points[1:])) fig, ax = plt.subplots() lc = LineCollection(segments, array=colorvalue, cmap="rainbow", lw=3) line = ax.add_collection(lc) ax.autoscale() plt.show() Taken from: https://stackoverflow.com/a/71222645/4637427 Edit: Matplotlib colormaps here: https://matplotlib.org/stable/gallery/color/colormap_reference.html More on reddit.com
๐ŸŒ r/learnpython
3
1
June 10, 2022
๐ŸŒ
Matplotlib
matplotlib.org โ€บ stable โ€บ users โ€บ explain โ€บ colors โ€บ colors.html
Specifying colors โ€” Matplotlib 3.11.2 documentation
In combination, they represent the colorspace. The alpha value of a color specifies its transparency, where 0 is fully transparent and 1 is fully opaque. When a color is semi-transparent, the background color will show through. The alpha value determines the resulting color by ...
๐ŸŒ
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 - In this example, We are going to see how to color scatterplot with their variable value. Here we will plot a simple scatterplot with x and y data, Then will use c attributes for coloring the point(Scatterplot variable points). ... import matplotlib.pyplot as plt x =[5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6] y =[99, 86, 87, 88, 100, 86, 103, 87, 94, 78, 77, 85, 86] plt.scatter(x, y, c = 'red') # To show the plot plt.show() ... Example 2: Color Scatterplot point with dependent values.
๐ŸŒ
Matplotlib
matplotlib.org โ€บ 3.3.4 โ€บ gallery โ€บ color โ€บ color_by_yvalue.html
Color by y-value โ€” Matplotlib 3.3.4 documentation
January 28, 2021 - You are reading an old version of the documentation (v3.3.4). For the latest version see https://matplotlib.org/stable/gallery/color/color_by_yvalue.html ... Use masked arrays to plot a line with different colors by y-value.
๐ŸŒ
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 to color the y-values according to their value in matplotlib like import matplotlib.pyplot as plt import 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() ...
Find elsewhere
๐ŸŒ
Matplotlib
matplotlib.org โ€บ 2.0.2 โ€บ api โ€บ colors_api.html
colors โ€” Matplotlib 2.0.2 documentation
RGBA values are returned, which can then be used to plot the shaded image with imshow. The color of the resulting image will be darkened by moving the (s,v) values (in hsv colorspace) toward (hsv_min_sat, hsv_min_val) in the shaded regions, or lightened by sliding (s,v) toward (hsv_max_sat hsv_max_val) in regions that are illuminated.
๐ŸŒ
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.
๐ŸŒ
Zubairkhalid
zubairkhalid.com โ€บ blog โ€บ data-analysis โ€บ matplotlib-colors
Matplotlib Colors: How to Set and Choose Plot Colors
3 weeks ago - Zubair Khalid, DVM, MS, PhD ยท ... main ways: a named string such as "steelblue", a hex string such as "#ea580c", an RGB or RGBA tuple of floats in the range 0 to 1, or a colormap that maps numbers to colors [1]. You pass ...
Top answer
1 of 2
107

This is what matplotlib.pyplot.scatter is for.

If no colormap is specified, scatter will use whatever the default colormap is set to. To specify which colormap scatter should use, use the cmap kwarg (e.g. cmap="jet").

As a quick example:

import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np

# Generate data...
t = np.linspace(0, 2 * np.pi, 20)
x = np.sin(t)
y = np.cos(t)

plt.scatter(t, x, c=y, ec='k')
plt.show()

One may specify a custom color map and norm

cmap, norm = mcolors.from_levels_and_colors([0, 2, 5, 6], ['red', 'green', 'blue'])
plt.scatter(x, y, c=t, cmap=cmap, norm=norm)

2 of 2
15

If you want to plot lines instead of points, see this example, modified here to plot good/bad points representing a function as a black/red as appropriate:

def plot(xx, yy, good):
    """Plot data

    Good parts are plotted as black, bad parts as red.

    Parameters
    ----------
    xx, yy : 1D arrays
        Data to plot.
    good : `numpy.ndarray`, boolean
        Boolean array indicating if point is good.
    """
    import numpy as np
    import matplotlib.pyplot as plt
    fig, ax = plt.subplots()
    from matplotlib.colors import from_levels_and_colors
    from matplotlib.collections import LineCollection
    cmap, norm = from_levels_and_colors([0.0, 0.5, 1.5], ['red', 'black'])
    points = np.array([xx, yy]).T.reshape(-1, 1, 2)
    segments = np.concatenate([points[:-1], points[1:]], axis=1)
    lines = LineCollection(segments, cmap=cmap, norm=norm)
    lines.set_array(good.astype(int))
    ax.add_collection(lines)
    plt.show()
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to color different segments of plotted curve based on value?
r/learnpython on Reddit: How to color different segments of plotted curve based on value?
June 10, 2022 -

Hello, so I am trying to figure out how to plot a dataframe column where the color of the line changes based on its value, but I am stuck on how to get these values referenced.

For example, I have this dataframe:

    Timestamp    Value
-------------------------
0       5:00         4
1       5:01         5
2       5:02         8
3       5:03        12
4       5:04        13
5       5:05         2
...

I simple run some form of:

df.plot(x="Timestamp", y=["Value"], figsize=(9, 8)) 

to get a plot of the values changing over time.

Now, I have my values separated into different categories/bins where 0-5 = "Low", 6-15 = "Mid" and 16+ = "High". I want to have my same plot, but now with all values on the curve corresponding to "Low" to be blue, corresponding to "Mid" to be green, and corresponding to "High" to be red. So I will see see a single curve plotted, made up from the "Value" column, but segmented into different colors based on value. How can I accomplish this in python? I am confused about how to set up an index/key for translating values to colors and what argument is needed in .plot() to actually have color based on value. I would appreciate any help with this, thanks!

๐ŸŒ
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.
๐ŸŒ
Matplotlib
matplotlib.org โ€บ stable โ€บ tutorials โ€บ colors โ€บ colors.html
Matplotlib
January 28, 2021 - Matplotlib has support for visualizing information with a wide array of colors and colormaps.
๐ŸŒ
MLJAR
mljar.com โ€บ blog โ€บ matplotlib-colors
9 ways to set colors in Matplotlib
November 21, 2022 - Matplotlib is a powerful visualization package for Python. It is very customizable, thanks to this it is widly used in commercial and in academic use cases. In this article, I will show you 9 different ways how to set colors in Matplotlib plots. All parts of the plot can be customized with a new color.
๐ŸŒ
Matplotlib
matplotlib.org โ€บ stable โ€บ gallery โ€บ color โ€บ named_colors.html
List of named colors โ€” Matplotlib 3.11.2 documentation
import math import matplotlib.pyplot as plt import matplotlib.colors as mcolors from matplotlib.patches import Rectangle def plot_colortable(colors, *, ncols=4, sort_colors=True): cell_width = 212 cell_height = 22 swatch_width = 48 margin = 12 # Sort colors by hue, saturation, value and name.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-color-scatterplot-by-a-variable-in-matplotlib
How to Color Scatterplot by a variable in Matplotlib?
March 27, 2026 - 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....
๐ŸŒ
Kanoki
kanoki.org โ€บ 2020 โ€บ 08 โ€บ 30 โ€บ matplotlib-scatter-plot-color-by-category-in-python
Matplotlib Scatter Plot Color by Category in Python | kanoki
August 30, 2020 - Colormap instances are used to convert data values (floats) from the interval [0, 1] to the RGBA color that the respective Colormap represents