Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

What named colours are available in matplotlib for use in plots? I can find a list on the matplotlib documentation that claims that these are the only names:

b: blue 

g: green 

r: red 

c: cyan 

m: magenta 

y: yellow 

k: black 

w: white

However, I've found that these colours can also be used, at least in this context:

scatter(X,Y, color='red') 

scatter(X,Y, color='orange') 

scatter(X,Y, color='darkgreen')

but these are not on the above list. Does anyone know an exhaustive list of the named colours that are available?

1 Answer

0 votes
by (106k points)

You can use the below-mentioned code:-

import matplotlib.pyplot as plt 

from matplotlib import colors as mcolors 

colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS) 

# Sort colors by hue, saturation, value and name. 

by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name) 

for name, color in colors.items()) 

sorted_names = [name for hsv, name in by_hsv] 

n = len(sorted_names) 

ncols = 4 

nrows = n // ncols 

fig, ax = plt.subplots(figsize=(12, 10)) 

# Get height and width 

X, Y = fig.get_dpi() * fig.get_size_inches() 

h = Y / (nrows + 1) 

w = X / ncols 

for i, name in enumerate(sorted_names): 

row = i % nrows 

col = i // nrows 

y = Y - (row * h) - h 

xi_line = w * (col + 0.05) 

xf_line = w * (col + 0.25) 

xi_text = w * (col + 0.3)
 

ax.text(xi_text, y, name, fontsize=(h * 0.8), 

horizontalalignment='left', 

verticalalignment='center') 

ax.hlines(y + h * 0.1, xi_line, xf_line, 

color=colors[name], linewidth=(h * 0.8))

ax.set_xlim(0, X) 

ax.set_ylim(0, Y) 

ax.set_axis_off() 

fig.subplots_adjust(left=0, right=1, 

top=1, bottom=0, 

hspace=0, wspace=0) 

plt.show()

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 9, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...