Specifying Colors — Matplotlib 3.8.4 Documentation
Specifying Colors — Matplotlib 3.8.4 Documentation
4 documentation
Specifying colors
Color formats
Matplotlib recognizes the following formats to specify a color.
Format Example
RGB or RGBA (red, green, blue, alpha) tuple of float values in a (0.1, 0.2, 0.5)
closed interval [0, 1]. (0.1, 0.2, 0.5, 0.3)
'#0f0f0f80'
String representation of float value in closed interval [0, 1] for '0' as black
grayscale values. '1' as white
Single character shorthand notation for some basic colors. 'b' as blue
'g' as green
Note 'r' as red
'c' as cyan
The colors green, cyan, magenta, and yellow do not
coincide with X11/CSS4 colors. Their particular shades 'm' as magenta
'w' as white
'mediumseagreen'
Case-insensitive color name from xkcd color survey with 'xkcd:sky blue'
'xkcd:' prefix. 'xkcd:eggshell'
'tab:orange'
Note 'tab:green'
'tab:red'
This is the default color cycle.
'tab:purple'
'tab:brown'
'tab:pink'
'tab:gray'
'tab:olive'
'tab:cyan'
Skip to main content
https://matplotlib.org/stable/users/explain/colors/colors.html 1/5
19/04/2024 Specifying colors — Matplotlib 3.8.4 documentation
Tuple of one of the above color formats and an alpha float. ('green', 0.3)
('#f00', 0.9)
See also
"Red", "Green", and "Blue" are the intensities of those colors. In combination, they represent the colorspace.
Transparency
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 blending the foreground color with the background color according to the
formula
https://matplotlib.org/stable/users/explain/colors/colors.html 2/5
19/04/2024 Specifying colors — Matplotlib 3.8.4 documentation
The orange rectangle is semi-transparent with alpha = 0.8. The top row of blue squares is drawn below and the bottom row of
blue squares is drawn on top of the orange rectangle.
def demo(sty):
mpl.style.use(sty)
fig, ax = plt.subplots(figsize=(3, 3))
demo('default')
demo('seaborn-v0_8')
https://matplotlib.org/stable/users/explain/colors/colors.html 3/5
19/04/2024 Specifying colors — Matplotlib 3.8.4 documentation
The first color 'C0' is the title. Each plot uses the second and third colors of each style's rcParams["axes.prop_cycle"] (default:
cycler('color', ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22',
95 out of the 148 X11/CSS4 color names also appear in the xkcd color survey. Almost all of them map to different color values in
the X11/CSS4 and in the xkcd palette. Only 'black', 'white' and 'cyan' are identical.
For example, 'blue' maps to '#0000FF' whereas 'xkcd:blue' maps to '#0343DF' . Due to these name collisions, all xkcd
colors have the 'xkcd:' prefix.
The visual below shows name collisions. Color names where color values agree are in bold.
n_groups = 3
n_rows = len(overlap) // n_groups + 1
col_shift = (j // n_rows) * 3
y_pos = j % n_rows
text_args = dict(fontsize=10, weight='bold' if css4 == xkcd else None)
ax.add_patch(mpatch.Rectangle((0 + col_shift, y_pos), 1, 1, color=css4))
ax.add_patch(mpatch.Rectangle((1 + col_shift, y_pos), 1, 1, color=xkcd))
ax.text(0.5 + col_shift, y_pos + .7, css4,
color=css4_text_color, ha='center', **text_args)
ax.text(1.5 + col_shift, y_pos + .7, xkcd,
color=xkcd_text_color, ha='center', **text_args)
ax.text(2 + col_shift, y_pos + .7, f' {color_name}', **text_args)
for g in range(n_groups):
ax.hlines(range(n_rows), 3*g, 3*g + 2.8, color='0.7', linewidth=1)
ax.text(0.5 + 3*g, -0.3, 'X11/CSS4', ha='center')
ax.text(1.5 + 3*g, -0.3, 'xkcd', ha='center')
ax.set_xlim(0, 3 * n_groups)
ax.set_ylim(n_rows, -1)
ax.axis('off')
plt.show()
https://matplotlib.org/stable/users/explain/colors/colors.html 4/5
19/04/2024 Specifying colors — Matplotlib 3.8.4 documentation
© Copyright 2002–2012 John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the Matplotlib
development team; 2012–2024 The Matplotlib development team.
Built with the PyData
Created using Sphinx 7.2.6. Sphinx Theme 0.13.3.
https://matplotlib.org/stable/users/explain/colors/colors.html 5/5