Introduction to Customizing Colorbars in MatPlotLib
The MatPlotLib series has been quite thorough in breaking down the intricacies of creating professional plots with MatPlotLib. In one regard, we’ve spent a great deal of time elaborating on plot types. In particular, we have discussed in great depths the plotting of simple line plots, scatter plots, plotting density and contour plots, as well as a variety of histogram types. Furthermore, we have devoted significant attention towards customization features with plots. In our previous article, we focused exclusively on customizing plot legends in MatPlotLib. Herein, we continue on our thread of explicating customization features by exploring the function of customizing colorbars in MatPlotLib.
Customizing Colorbars in MatPlotLib
One of the reasons we so thoroughly berated the subject of legends in MatPlotLib is because they really are one of the most effective tools for organizing graphical data. However, legends are not the only means by which this may be accomplished. Often times, the data we seek to represent is conferred by color. In these cases, we use color to represent some quantitative or qualitative significance about the data. Colorbars efficaciously establish a relationship between data types in a plot.
Fortunately, MatPlotLib makes encoding and customizing these features relatively easy. In the simples of cases, all a programmer must do is use the plt.colorbar function. MatPlotLib then defaults to creating a colorbar for a given plot. Let’s first begin by creating some data and using the colorbar function to create a colorbar. The code for creating these features appears as follows:
This ultimately creates an interesting plot that appears as:
Had we not encoded the colorbar, we would have no idea as to the significance of the blue or yellow colors that appear on this plot. However, utilizing this colorbar makes interpretation of the plot relatively easy.
Controlling Color Maps
The color map helps to denote the color system to be employed in a given plot. MatPlotLib has available a large variety of color map systems available. Perhaps the simples of these is the grayscale color map. If we specify the ‘cmap’ keyword argument as ‘gray’, not only will the color of the plot be constructed in accordance with the grayscale color map, but so too will the colorbar. Let’s first take a look at the code:
Note here that the ‘cmap’ keyword argument has been set to gray. Now, let us observe the effects on the plot and the colorbar:
Color Map Types
Not all color maps are created equally. Different types of color maps and their associated systems are more applicable to different data models. In fact, there exist three primary types of color maps. Sequential Color Maps are continuous spectra of colors through a particular range. Divergent Color Maps are typically comprised of two distinct colors, which are useful for modeling positive and negative values. Qualitative Color Maps mix colors together in a non-ordinal manner, such as the ‘rainbow’ color map.
The gray color map we previously employed is a decent example of a sequential color map. Let’s observe what happens when we use a divergent colormap, such as the ‘RdBu’ color map. It procures a plot that appears as follows:
While this color map makes distinct the difference between positive and negative values in the plot, it does not do well at demonstrating intermediate values or gradients between the two. Let’s take a look at a qualitative color map, like ‘rainbow’:
For our purposes, the use of the qualitative map may have been the best option. The issue with the gray colormap is that the grays are often difficult to distinguish between the black and the white. In the ‘RdBu’ color map, we fail to see a gradient between positive and negative values. However, with the qualitative color map, at least in this case, delivers the best of both worlds.
Defining Color Limits
Color bars in MatPlotLib are themselves properties of a plot’s axes. Fortunately, MatPlotLib supports great flexibility for the color bar associated with a plot. For example, the limits and values that constitute the color bars are alterable accessories. For example, we can set the limits of the color bar using the ‘clim’ argument. Let’s take a look at our code using this argument:
Note here that using the ‘clim’ argument, we specify the limits of the color bar as -1 and 1. If we take a look at the plot, we might note that there has been a subtle change in the color bar.
Take a moment here to realize the bounds of the colorbar have been slightly increased. In some cases, we might have values potentially extending beyond those demonstrated by the color bar. To model this, we are able to put arrows on either end of the color bar utilizing the ‘extend’ argument. When coding the colorbar, we can add arrows using the ‘both’, ‘min’, or ‘max’ arguments. Let’s first apply this code:
When we put into practice the code employed above, the following plot is produced:
By employing the code, we create a colorbar with arrows on either end, indicating that values may extend above or below the bounds demonstrated.
Discrete Color Bars
Color maps may often be continuous like in the rainbow color bar above. However, in some cases, it is preferable for data to be presented as discrete values. MatPlotLib makes coding this feature readily accessible by utilizing the ‘plt.cm.get_cmap’. Consider how we might apply the apply this discrete color bar to our code:
Here, when we use the cm.get_cmap attribute for the colorbar, we establish the color map we desire (rainbow), and the number of colors represented in the colorbar (6). Doing this creates a set of distinct colors rather than a continuous spectrum. Executing that code produces the following plot:
The Take Away
The color bars and other color coordinated features present in MatPlotLib are some of the most versatile properties of the MatPlotLib library. For that reason, customizing colorbars in MatPlotLib is an essential concept to understand to procure sophisticated data models. Of particular importance is a comprehension of the color map attributes available so that they may be applied in the most optimal circumstance. Additionally, the color bar can also be readily modified to fit the purpose of the data model. In our subsequent article, we elaborate further on customization features, particularly the plotting of multiple subplots simultaneously. Nevertheless, if you seek to explore customizing colorbars in MatPlotLib further, consider checking out the MatPlotLib manual which may be found here.