Python Hour - 20 October 2014
Meeting Summary
Color and colormaps in matplotlib
We informally discussed various aspects of color and colormaps in matplotlib. Some of the topics included:
- Color and colormap basics
- Generating custom diverging colors
- Centering colormaps and displaying the center-valued color
- Differences between
pcolormesh
andcontourf
- Setting upper, lower limits, and bad colors
- Using a colormap to color lines
After the meeting, I created a notebook expanding on many of the examples that were shown. The notebook may be found in the How-to section and has been added to the notebook repository on GitHub.
The problem with jet
We also talked about the disadvantages of the jet colormap that is the default for matplotlib and Matlab (pre-2014b). The main disadvantage of the jet colormap is that there are discontinuities in the luminosity (lightness) of the map. Because of these kinks in lightness and non-ordered colors, figures using the jet colormap often seem to have details that aren't in the data, for example, bands existing in smoothly-varying data around yellow. Worse yet, jet can also obscure details in green and cyan bands because the lightness doesn't change as rapidly in these regions. So jet both obscures features and generates false features. For examples of this, see some of the references below.
Basic colormap theory
What is needed instead is a perceptual colormap, i.e. one in which equal steps in data are perceived as equal steps in the colorspace. Because humans perceive changes in lightness more readily than changes in hue, colormaps with monotonically and evenly increasing lightness are easier to interpret. jet does not fit this category.
Colormaps can generally be split into 4 groups:
- Sequential: Lightness increases/decreases monotonically, often from white through a single hue. Good for showing magnitude and ordering of data. Generally converts to grayscale well.
- Diverging: Lightness has a maximum at some central or critical value, and reaches a minimum at either end. Good for showing anomalies about zero or some other critical value as well as direction. Usually does not convert well to grayscale because the two ends often have similar luminosity.
- Qualitative: These colormaps represent categorical data that may not have an ordering and are often discrete. For these mappings, it is important that all the colors are distinguishable from all the others.
- Special / Miscellaneous: These are maps that may or may not fit the above categories and usually consist of maps designed for a particular use such as showing both land and water topography or displaying hot/cold regions. These colormaps try to connect with intuitive colors for the parameters and, while they may not be perfect perceptual maps, are useful for some purposes where nature suggests a colormap.
Other important considerations when designing colormaps include awareness of colorblindness, conversion to grayscale, and how the colors look when used in different media such as online, print, and projection.
Colormap references
During the meeting, we compiled a number of useful colormap references, both from online and published literature:
matplotlib references
- Choosing colormaps Guide: Good guide on choosing colormaps provided by the matplotlib team. Has plots of the various default colormaps show how lightness changes throughout the maps
- Default colormaps: Quick reference for all of the default colormaps in matplotlib.
Webpages
- Colorbrewer: Good source for colormaps. Shows example maps applying the colormap in different regions. Also gives color-blind and printer-safe options.
- Palletton and Adobe Color CC: Online utilities for generating custom color schemes that follow established color paradigms.
- How bad is your colormap?: Quick and recent overview of problems with the jet colormap along with some Python code (and a haiku!).
- The Rainbow is Dead. Long live the Rainbow!: Long series examining the jet colormap, how to make it better, and looking at different colorspaces for interpolation.
Publications
-
Nice article with a quick overview and theory of color perception.
-
Great overview of colormaps and some color theory by a Matlab developer. Also includes a comprehensive annotated bibliography of more in-depth references. This was likely developed as a whitepaper to support the change in the Matlab default colormap (from jet to parula) in Release 2014b.
-
This paper focuses on colormaps in the geosciences and colorblindness. Matplotlib has a few similar default colormaps and a library for Matlab is available. Ironically, the first few pages of the online PDF are in black-and-white with color pages reproduced afterwards.
-
Short review of the problems with rainbow colormaps and some statistics on how often it's used in medical imaging.
-
A longish paper that goes into some color theory and discusses various colorspaces and the math involved in colors. Also included on the website are supplemental materials like Excel, R, Python, and Matlab tools to create custom colormaps.