Plot the unit sphere centered at the origin. Surf (X,Y,Z) axis equal Define X2, Y2, and Z2 as coordinates of a sphere with a radius of 5 by multiplying the coordinates of the unit sphere. Plot the second sphere, centering it at (5,-5,0).
Matplotlib has a number of built-in colormaps accessible viamatplotlib.cm.get_cmap
. There are also external libraries like[palettable] and [colorcet] that have many extra colormaps.Here we briefly discuss how to choose between the many options. Forhelp on creating your own colormaps, seeCreating Colormaps in Matplotlib.
The idea behind choosing a good colormap is to find a good representation in 3Dcolorspace for your data set. The best colormap for any given data set dependson many things including:
For many applications, a perceptually uniform colormap is the bestchoice --- one in which equal steps in data are perceived as equalsteps in the color space. Researchers have found that the human brainperceives changes in the lightness parameter as changes in the datamuch better than, for example, changes in hue. Therefore, colormapswhich have monotonically increasing lightness through the colormapwill be better interpreted by the viewer. A wonderful example ofperceptually uniform colormaps is [colorcet].
Color can be represented in 3D space in various ways. One way to represent coloris using CIELAB. In CIELAB, color space is represented by lightness,(L^*); red-green, (a^*); and yellow-blue, (b^*). The lightnessparameter (L^*) can then be used to learn more about how the matplotlibcolormaps will be perceived by viewers.
An excellent starting resource for learning about human perception of colormapsis from [IBM].
Colormaps are often split into several categories based on their function (see,e.g., [Moreland]):
For the Sequential plots, the lightness value increases monotonically throughthe colormaps. This is good. Some of the (L^*) values in the colormapsspan from 0 to 100 (binary and the other grayscale), and others start around(L^*=20). Those that have a smaller range of (L^*) will accordinglyhave a smaller perceptual range. Note also that the (L^*) function variesamongst the colormaps: some are approximately linear in (L^*) and othersare more curved.
Many of the (L^*) values from the Sequential2 plots are monotonicallyincreasing, but some (autumn, cool, spring, and winter) plateau or even go bothup and down in (L^*) space. Others (afmhot, copper, gist_heat, and hot)have kinks in the (L^*) functions. Data that is being represented in aregion of the colormap that is at a plateau or kink will lead to a perception ofbanding of the data in those values in the colormap (see [mycarta-banding] foran excellent example of this).
For the Diverging maps, we want to have monotonically increasing (L^*)values up to a maximum, which should be close to (L^*=100), followed bymonotonically decreasing (L^*) values. We are looking for approximatelyequal minimum (L^*) values at opposite ends of the colormap. By thesemeasures, BrBG and RdBu are good options. coolwarm is a good option, but itdoesn't span a wide range of (L^*) values (see grayscale section below).
For Cyclic maps, we want to start and end on the same color, and meet asymmetric center point in the middle. (L^*) should change monotonicallyfrom start to middle, and inversely from middle to end. It should be symmetricon the increasing and decreasing side, and only differ in hue. At the ends andmiddle, (L^*) will reverse direction, which should be smoothed in(L^*) space to reduce artifacts. See [kovesi-colormaps] for moreinformation on the design of cyclic maps.
The often-used HSV colormap is included in this set of colormaps, although itis not symmetric to a center point. Additionally, the (L^*) values varywidely throughout the colormap, making it a poor choice for representing datafor viewers to see perceptually. See an extension on this idea at[mycarta-jet].
Qualitative colormaps are not aimed at being perceptual maps, but looking at thelightness parameter can verify that for us. The (L^*) values move all overthe place throughout the colormap, and are clearly not monotonically increasing.These would not be good options for use as perceptual colormaps.
Some of the miscellaneous colormaps have particular uses for whichthey have been created. For example, gist_earth, ocean, and terrainall seem to be created for plotting topography (green/brown) and waterdepths (blue) together. We would expect to see a divergence in thesecolormaps, then, but multiple kinks may not be ideal, such as ingist_earth and terrain. CMRmap was created to convert well tograyscale, though it does appear to have some small kinks in(L^*). cubehelix was created to vary smoothly in both lightnessand hue, but appears to have a small hump in the green hue area.
The often-used jet colormap is included in this set of colormaps. We can seethat the (L^*) values vary widely throughout the colormap, making it apoor choice for representing data for viewers to see perceptually. See anextension on this idea at [mycarta-jet].
First, we'll show the range of each colormap. Note that some seemto change more 'quickly' than others.
Here we examine the lightness values of the matplotlib colormaps.Note that some documentation on the colormaps is available([list-colormaps]).
It is important to pay attention to conversion to grayscale for colorplots, since they may be printed on black and white printers. If notcarefully considered, your readers may end up with indecipherableplots because the grayscale changes unpredictably through thecolormap.
Conversion to grayscale is done in many different ways [bw]. Some of thebetter ones use a linear combination of the rgb values of a pixel, butweighted according to how we perceive color intensity. A nonlinear method ofconversion to grayscale is to use the (L^*) values of the pixels. Ingeneral, similar principles apply for this question as they do for presentingone's information perceptually; that is, if a colormap is chosen that ismonotonically increasing in (L^*) values, it will print in a reasonablemanner to grayscale.
With this in mind, we see that the Sequential colormaps have reasonablerepresentations in grayscale. Some of the Sequential2 colormaps have decentenough grayscale representations, though some (autumn, spring, summer,winter) have very little grayscale change. If a colormap like this was usedin a plot and then the plot was printed to grayscale, a lot of theinformation may map to the same gray values. The Diverging colormaps mostlyvary from darker gray on the outer edges to white in the middle. Some(PuOr and seismic) have noticeably darker gray on one side than the otherand therefore are not very symmetric. coolwarm has little range of gray scaleand would print to a more uniform plot, losing a lot of detail. Note thatoverlaid, labeled contours could help differentiate between one side of thecolormap vs. the other since color cannot be used once a plot is printed tograyscale. Many of the Qualitative and Miscellaneous colormaps, such asAccent, hsv, and jet, change from darker to lighter and back to darker graythroughout the colormap. This would make it impossible for a viewer tointerpret the information in a plot once it is printed in grayscale.
There is a lot of information available about color blindness (e.g.,[colorblindness]). Additionally, there are tools available to convert imagesto how they look for different types of color vision deficiencies.
The most common form of color vision deficiency involves differentiatingbetween red and green. Thus, avoiding colormaps with both red and green willavoid many problems in general.
[colorcet] | (1, 2)https://colorcet.pyviz.org |
[Ware] | http://ccom.unh.edu/sites/default/files/publications/Ware_1988_CGA_Color_sequences_univariate_maps.pdf |
[Moreland] | http://www.kennethmoreland.com/color-maps/ColorMapsExpanded.pdf |
[list-colormaps] | https://gist.github.com/endolith/2719900#id7 |
[mycarta-banding] | https://mycarta.wordpress.com/2012/10/14/the-rainbow-is-deadlong-live-the-rainbow-part-4-cie-lab-heated-body/ |
[mycarta-jet] | (1, 2)https://mycarta.wordpress.com/2012/10/06/the-rainbow-is-deadlong-live-the-rainbow-part-3/ |
[kovesi-colormaps] | https://arxiv.org/abs/1509.03700 |
[bw] | http://www.tannerhelland.com/3643/grayscale-image-algorithm-vb6/ |
[colorblindness] | http://www.color-blindness.com/ |
[IBM] | https://doi.org/10.1109/VISUAL.1995.480803 |
[palettable] | https://jiffyclub.github.io/palettable/ |
Total running time of the script: ( 0 minutes 4.634 seconds)
Keywords: matplotlib code example, codex, python plot, pyplotGallery generated by Sphinx-Gallery
pyplot.subplots
creates a figure and a grid of subplots with a single call,while providing reasonable control over how the individual plots are created.For more advanced use cases you can use GridSpec
for a more general subplotlayout or Figure.add_subplot
for adding subplots at arbitrary locationswithin the figure.
subplots()
without arguments returns a Figure
and a singleAxes
.
This is actually the simplest and recommended way of creating a singleFigure and Axes.
Out:
The first two optional arguments of pyplot.subplots
define the number ofrows and columns of the subplot grid.
When stacking in one direction only, the returned axs
is a 1D numpy arraycontaining the list of created Axes.
Out:
If you are creating just a few Axes, it's handy to unpack them immediately todedicated variables for each Axes. That way, we can use ax1
instead ofthe more verbose axs[0]
.
Out:
To obtain side-by-side subplots, pass parameters 1,2
for one row and twocolumns.
Out:
When stacking in two directions, the returned axs
is a 2D NumPy array.
If you have to set parameters for each subplot it's handy to iterate overall subplots in a 2D grid using foraxinaxs.flat:
.
You can use tuple-unpacking also in 2D to assign all subplots to dedicatedvariables:
By default, each Axes is scaled individually. Thus, if the ranges aredifferent the tick values of the subplots do not align.
Out:
You can use sharex or sharey to align the horizontal or vertical axis.
Out:
Setting sharex or sharey to True
enables global sharing across thewhole grid, i.e. also the y-axes of vertically stacked subplots have thesame scale when using sharey=True
.
Out:
For subplots that are sharing axes one set of tick labels is enough. Ticklabels of inner Axes are automatically removed by sharex and sharey.Still there remains an unused empty space between the subplots.
The parameter gridspec_kw of pyplot.subplots
controls the gridproperties (see also GridSpec
). For example, we can reduce the heightbetween vertical subplots using gridspec_kw={'hspace':0}
.
label_outer
is a handy method to remove labels and ticks from subplotsthat are not at the edge of the grid.
Apart from True
and False
, both sharex and sharey accept thevalues 'row' and 'col' to share the values only per row or column.
The parameter subplot_kw of pyplot.subplots
controls the subplotproperties (see also Figure.add_subplot
). In particular, this can be usedto create a grid of polar Axes.
Total running time of the script: ( 0 minutes 1.314 seconds)
Keywords: matplotlib code example, codex, python plot, pyplotGallery generated by Sphinx-Gallery