Color coding the data can have great impact on how your visualizations are perceived by the viewer, as they come with assumptions about color and what that color represents.
Being explicit if the color is used to add additional information to the data is always good. To know when and how to use color in your visualizations is even better.
If your data is not naturally color coded (such as earth/terrain altitudes or object temperature), it's better not to make any artificial mappings to natural coloring. We want to understand the data appropriately and make a choice of color to help the reader decode data easily. We don't want readers constantly trying to suppress learned mapping of color for temperatures, if we are representing financial data that has no connection with Kelvins or Celsius.
If possible, avoid the usual red/green associations, if there are no strong correlations in the data to associate them with those colors.
To help you pick the right color mapping, we will explain some colormaps available in the matplotlib package that can save a lot of time and help us, if we know what they are used for and how to find them.
Colormaps, in general, can be categorized as follows:
Matplotlib comes with a lot of predefined maps, and we are able to divide them into several categories. We will suggest when to use some of these colormaps.
The most common and base colormaps are autumn
, bone
, cool
, copper
, flag
, gray
, hot
, hsv
, jet
, pink
, prism
, sprint
, summer
, winter
, and spectral
.
We have another set of colormaps coming from the "Yorick scientific visualization package". This is evolution from GIST package, so all colormaps in this collection have gist_
as prefix in their name.
The Yorick is a visualization package and also an interpreted language, written in C, not quite active lately. You can find more information on an official website – http://yorick.sourceforge.net/index.php
These colormap set contain following maps: gist_earth, gist_heat, gist_ncar, gist_rainbow,and gist_stern
.
Then, we have the following colormaps based on Color Brewer (http://colorbrewer.org), where we can categorize them into: Diverging, where luminance is highest at the midpoint and decreases towards different endpoints; Sequential, where luminance decreases monotonically; Qualitative, where different sets of colors are used to differentiate data categories.
Also, some miscellaneous colormaps are also available:
Most of the maps presented here can be reversed by putting _r
postfix after a name of the colormap, for example, hot_r
is an inverse cycle colormap of hot
.
We can set colormap on many items in matplotlib
. For example, colormap can be set on image
, pcolor
, and scatter
. This is accomplished usually via argument to a function called cmap
. This argument is an expected instance of colors.Colormap
.
We can also use matplotlib.pyplot.set_cmap
to set cmap
for latest object plotted on the axes.
You can get all available colormaps easily with matplotlib.pyplot.colormaps
. Fire up IPython and type in the following:
In [1]: import matplotlib.pyplot as plt In [2]: plt.colormaps() Out[2]: ['Accent', 'Accent_r', 'Blues', 'Blues_r', ... 'winter', 'winter_r']
Note that we have shortened the preceding list because it contains around 140 items and would span across several pages here.
This will import the pyplot
function interface and allow us to call the colormaps
function, which returns a list of all registered colormaps.
Finally, we want to show you how to make a nice looking colormap. In the following example, we need to:
import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np # Red Yellow Green divergent colormap red_yellow_green = ['#d73027', '#f46d43', '#fdae61', '#fee08b', '#ffffbf', '#d9ef8b', '#a6d96a', '#66bd63', '#1a9850'] sample_size = 1000 fig, ax = plt.subplots(1) for i in range(9): y = np.random.normal(size=sample_size).cumsum() x = np.arange(sample_size) ax.scatter(x, y, label=str(i), linewidth=0.1, edgecolors='grey', facecolor=red_yellow_green[i]) ax.legend() plt.show()
This will render a nice looking figure:
We used the ColorBrewer website to find out colors in red, yellow, and green divergent colormap from Colorbrew. Then, we listed those colors in our code and applied them to our scatter plot.
Colorbrew is a web tool, built by Cynthia Brewer, Mark Harrower, and The Pennsylvania State University as a tool to explore color maps. It is a very handy tool to pick up color maps of different ranges and see them applied on a map using slight variations so that you immediately sense what will they look like on a chart. This particular map is at http://colorbrewer2.org/?type=diverging&scheme=RdYlGn&n=9.
Sometimes, we will have to make our customization on matplotlib.rcParams
, which is the first thing we want to do before we create figure or any of the axes.
For example, matplotlib.rcParams['axes.cycle_color']
is the configuration setting we want to change in order to set up default colormap for most of the matplotlib functions.
Using matplotlib.pyplot.register_cmap
, we can register a new colormap to matplotlib, so it can be found using the get_cmap
function. We can use it in two different ways. Here are both signatures:
register_cmap(name='swirly', cmap=swirly_cmap)
register_cmap(name='choppy', data=choppydata, lut=128)
The first signature allows us to specify colormap as an instance of colors.Colormap
and register it via the name
argument. The name
argument can be omitted in which case it will be inherited from the name
attribute of the cmap
instance provided.
The latter one, we are passing three arguments to the linear segmented colormap constructor, and registering that colormap afterwards.
Using maplotlib.pyplot.get_cmap
, we can get the colors.Colormap
instance using name argument.
Here's how to make your own map using matplotlib.colors.LinearSegmentedColormap
:
from pylab import * cdict = {'red': ((0.0, 0.0, 0.0), (0.5, 1.0, 0.7), (1.0, 1.0, 1.0)), 'green': ((0.0, 0.0, 0.0), (0.5, 1.0, 0.0), (1.0, 1.0, 1.0)), 'blue': ((0.0, 0.0, 0.0), (0.5, 1.0, 0.0), (1.0, 0.5, 1.0))} my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap',cdict,256) pcolor(rand(10,10),cmap=my_cmap) colorbar()
This is the simplest part, while the hardest part is to actually come with a combination of colors that are informative, do not take away from the data we want to visualize, and that are pleasant for the eyes of the viewer.
For the base map list (colormaps listed in the preceding table), we can use the pylab
shortcut to set colormap. For example, the following code would set colormap of the image X to cmap='hot'
:
imshow(X) hot()