Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named colors in matplotlib

What named colors 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 colors 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 colors that are available?

like image 650
T.C. Proctor Avatar asked Mar 14 '14 14:03

T.C. Proctor


Video Answer


1 Answers

I constantly forget the names of the colors I want to use and keep coming back to this question =)

The previous answers are great, but I find it a bit difficult to get an overview of the available colors from the posted image. I prefer the colors to be grouped with similar colors, so I slightly tweaked the matplotlib answer that was mentioned in a comment above to get a color list sorted in columns. The order is not identical to how I would sort by eye, but I think it gives a good overview.

I updated the image and code to reflect that 'rebeccapurple' has been added and the three sage colors have been moved under the 'xkcd:' prefix since I posted this answer originally.

enter image description here

I really didn't change much from the matplotlib example, but here is the code for completeness.

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() 

Additional named colors

Updated 2017-10-25. I merged my previous updates into this section.

xkcd

If you would like to use additional named colors when plotting with matplotlib, you can use the xkcd crowdsourced color names, via the 'xkcd:' prefix:

plt.plot([1,2], lw=4, c='xkcd:baby poop green') 

Now you have access to a plethora of named colors!

enter image description here

Tableau

The default Tableau colors are available in matplotlib via the 'tab:' prefix:

plt.plot([1,2], lw=4, c='tab:green') 

There are ten distinct colors:

enter image description here

HTML

You can also plot colors by their HTML hex code:

plt.plot([1,2], lw=4, c='#8f9805') 

This is more similar to specifying and RGB tuple rather than a named color (apart from the fact that the hex code is passed as a string), and I will not include an image of the 16 million colors you can choose from...


For more details, please refer to the matplotlib colors documentation and the source file specifying the available colors, _color_data.py.


like image 70
joelostblom Avatar answered Sep 19 '22 17:09

joelostblom