Using colormaps

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.

Getting ready

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:

  • Sequential: Monochromatic colormaps of two color tones from low to high saturation of the same color. For example, from white to bright blue. Ideal for most cases, as they clearly show the change from low to high values.
  • Diverging: The central point here is the median value (some light color usually), but then, ranges go from there to two different color tones in direction for high and for low values. This can be ideal for data with significant median value. For example, when the median is at 0, it clearly shows the difference between negative and positive values.
  • Qualitative: For cases where data has no inherent ordering, and all you want is to make sure different categories are easily discernible from each other.
  • Cyclic: It is used where data can wrap around endpoint values, such as representing time of the day, wind direction, phase angle, or similar.

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.

Note

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:

Colormap

Description

brg

This is blue-red-green

bwr

Diverging blue-white-red

coolwarm

Useful for 3D shading, color blindness, and ordering of colors

rainbow

Spectral purple-blue-green-yellow-orange-red colormap with diverging luminance

seismic

Diverging blue-white-red

terrain

Mapmaker's colors, blue-green-yellow-brown-white, originally from IGOR Pro software

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.

How to do it...

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:

  1. Use the Color Brewer website to get divergent colormap color values in the hex format
  2. Generate a random sample of x and y, where y is cumulative sum of value (simulate stock price variations)
  3. Apply customization to scatter plot functions of matplotlib
  4. Tweak scatter marker line color and width to make the plot more readable and pleasant for viewers.
    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:

How to do it...

How it works...

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.

Note

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.

There's more...

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()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.51.228