Best Practices for Visualizing Data

What makes a chart accurate to read, with the evidence behind each rule. Cleveland and McGill's 1984 ranking of the perceptual tasks a chart asks of its reader, from position on a common scale down to shading, and their experiments on bars and pies; the same NHANES blood pressure shares drawn as a pie, stacked bars and a dot chart; honest axes; Tufte's data-ink idea; color scales that rise steadily in lightness, measured along jet and viridis; and color pairs simulated for color-vision deficiency, where a red and green pair that differ by 120 units become nearly identical.

Among NHANES adults aged 40 to 59, 36.0% have normal blood pressure under the 2017 guideline of the American College of Cardiology and American Heart Association, 14.5% have elevated pressure, 25.9% have stage 1 hypertension and 23.6% have stage 2. Drawn as a pie chart, the stage 1 and stage 2 slices look about the same size, and a reader asked which is larger, or by how much, has to guess. Drawn as four dots along one axis, the order is plain and the 2.3-point difference is visible. Nothing about the data changed; what changed is the visual task the chart asks of its reader. One chart asks for a comparison of angles and areas, the other for a comparison of positions along a scale, and people are much better at the second.

This article collects the design rules that make charts accurate to read, and gives the evidence for each where there is evidence. Its two main sources are an experimental one, Cleveland and McGill's (1984) study of graphical perception, and a measurable one, the lightness and color differences of the colors a chart uses, computed here. The rules about axes that keep a single graph honest, starting bars at zero, avoiding three-dimensional effects and dual axes, are set out with an NHANES example in Tables and Graphs for Data and are summarized here only briefly.

Graphical perception

Elementary perceptual tasks

Cleveland and McGill (1984) started from a simple observation: whatever a chart looks like, a reader who wants a number from it must perform some visual judgment, comparing the positions of two marks, the lengths of two bars, the angles of two slices, the areas of two circles. They called these judgments elementary perceptual tasks and listed ten: position along a common scale, position along nonaligned scales, length, direction, angle, area, volume, curvature, shading and color saturation. They then proposed an ordering of the tasks by how accurately people perform them, from most to least accurate:

  1. Position along a common scale
  2. Positions along nonaligned scales
  3. Length, direction, angle
  4. Area
  5. Volume, curvature
  6. Shading, color saturation

The ordering combined their own reasoning with results from psychophysics. For example, the perceived size of a length, area or volume follows a power law in its actual size, with an exponent close to 1 for length and smaller for area and still smaller for volume, so lengths are judged with little distortion while areas and volumes are systematically underestimated. Position on nonaligned scales ranks above length because a scale or frame gives extra cues (Cleveland and McGill, 1984, §3). The authors were careful about what the ordering claims: it concerns the accuracy of extracting quantities, which is one goal of a graph among others, and items in the same rank were tied because there was not yet information to separate them.

The experiments

Two experiments tested parts of the ordering (Cleveland and McGill, 1984, §4). In the first, 55 people judged pairs of values on bar charts drawn in five ways, three that allowed a judgment of position along a common scale and two (divided, or stacked, bar charts) that required a judgment of length; in the second, 54 people judged the same kind of values on pie charts and on ordinary bar charts. In the first, readers were asked what percentage the smaller of two marked values was of the larger; in the second, what percentage each slice or bar was of the largest; in both, by "a quick visual judgment". After removing subjects who had not followed instructions, 51 remained in each experiment. Accuracy was measured by the error on a log scale, $\log_2(|\text{judged percent} - \text{true percent}| + 1/8)$, where the $1/8$ keeps errors near zero from dominating.

The results agreed with the ordering. Average errors for the length judgments were 1.4 to 2.5 times those for the position judgments. Position judgments on bar charts were 1.96 times as accurate as angle judgments on pie charts, and 88% of the large errors in the second experiment came from the angle judgments, a rate 7.3 times that of the position judgments. Both length and angle judgments showed a consistent bias, underestimating true percentages between about 25 and 50. Within the position judgments, accuracy fell as the two values being compared were farther apart in the direction perpendicular to the common scale.

Their conclusion was that graphs should be built from tasks as high in the ordering as possible, and that several common forms, pie charts and divided bar charts among them, could be replaced by dot charts, which place each value as a point along a common scale. That core result, that position judgments are more accurate than length or angle judgments, underlies most of the advice below.

The same blood pressure categories of NHANES adults, three ways. Left: a pie for ages 40 to 59; stage 1 (25.9%) and stage 2 (23.6%) must be compared by angle and area. Middle: stacked bars for three age groups; only the bottom segment (normal) and the top segment (stage 2) start from a common baseline, and the middle segments must be compared by length. Right: a dot chart of the same shares; every comparison, within and across age groups, is a judgment of position along one scale.
The same blood pressure categories of NHANES adults, three ways. Left: a pie for ages 40 to 59; stage 1 (25.9%) and stage 2 (23.6%) must be compared by angle and area. Middle: stacked bars for three age groups; only the bottom segment (normal) and the top segment (stage 2) start from a common baseline, and the middle segments must be compared by length. Right: a dot chart of the same shares; every comparison, within and across age groups, is a judgment of position along one scale.

Applied: pies, stacked bars and dot charts

The figure applies the ordering to the blood pressure categories of 5,232 NHANES adults with both a systolic and a diastolic reading. Within ages 40 to 59 the pie asks the reader to compare angles; the two hypertension slices differ by 2.34 points of 100, 8.4 degrees of arc. The stacked bars do better for the normal category, which starts at zero in every bar, and for stage 2, which ends at 100%, but the middle categories float: elevated pressure, 13.4% of adults aged 18 to 39, 14.5% at 40 to 59 and 18.4% at 60 and over, sits on a different base in each bar, and its growth is hard to see. The dot chart places every share on the same axis: normal pressure falls from 65.6% to 36.0% to 20.0% across the three age groups, stage 2 rises from 5.7% to 23.6% to 39.7%, and the smaller changes in the middle categories are as easy to read as the large ones.

None of this forbids pies and stacked bars. A pie with two or three slices answers "about what share of the whole?" well enough, and a stacked bar is the natural display when totals matter and the parts are secondary. The rule is to put the comparison the reader most needs to make on a common scale. In a stacked bar, that means putting the most important category at the bottom.

Honest axes

A chart is a set of encoding rules, and a chart misleads when it breaks its own rules. Tables and Graphs for Data works through the main cases on NHANES data; in brief:

  • Bars start at zero. A bar encodes a value by its length, so its length must be measured from zero. A bar chart of 58.6% and 51.1% with the axis starting at 50% shows bars in a ratio of 7.9 to 1 for values in a ratio of 1.15. Points and lines encode by position and may use an axis that does not include zero.
  • No unearned dimensions. Three-dimensional bars and pies, and icons scaled in both height and width, add depth and area that stand for nothing, and area and volume are low in the perceptual ordering anyway.
  • One scale per axis. Two vertical scales on one chart can each be stretched and shifted independently, so any two series can be made to appear to move together. Two panels with their own axes, one above the other, show the same data without the implied comparison.
  • Up and right mean more. Readers assume larger values are higher and further right. An inverted axis, larger values lower, reverses the reading of every mark, and needs a reason (depth below the surface, rank where 1 is best) and a clear label.
  • Label scale changes. A logarithmic axis turns ratios into distances; it is often the right choice (Visualizing Data with One Feature), but it must be labeled in original units at evenly spaced ratios so that readers know what equal distances mean.
  • Mind the aspect ratio. The slope of a line on a chart depends on the chart's width and height as much as on the data; stretching a chart vertically makes every trend look steeper. Cleveland (1993) proposed choosing the aspect ratio so that the average absolute orientation of the line segments is 45 degrees, "banking to 45", where differences in slope are judged most accurately.

Show the data

Summary statistics can agree while the data behind them differ completely. Anscombe (1973) made the point with four sets of eleven points that share their means, variances, correlation and least-squares line, but of which only one is described well by that line; the four are drawn in Introduction to Regression, and their residual plots in Linear Regression Assumptions. The same lesson recurs throughout this topic: a correlation of 0.04 that hides a clear curve, a correlation of $-0.19$ that vanishes within groups (Visualizing Data with Multiple Features), a box plot that hides two clusters (Visualizing Data with One Feature).

Practical consequences follow. Where the number of observations allows, draw the observations, not only their summary: points over a box plot, or the individual values beside a mean and its error bar. A bar chart of group means hides how many observations each mean rests on and how widely they vary, both of which the reader needs to judge a difference. And summaries that come before a plot, in a report or a dashboard, should be checked against one.

Data-ink and clutter

Tufte (1983) proposed the data-ink ratio, the share of a graphic's ink that represents data, as a measure of its economy, with the advice to maximize it "within reason": remove gridlines, borders, backgrounds, shadows and decorations that encode nothing, and erase ink that repeats what other ink already shows. The idea is a design principle rather than an experimental result, and its qualifier matters. Some non-data ink helps: light gridlines make values easier to read off, tick labels anchor the scale, and a caption explains what to look for. What the principle rules out is ink that competes with the data for attention, such as heavy grids, hatched fills, three-dimensional effects and saturated backgrounds.

Several related habits make charts faster to read:

  • Label directly. A label placed next to a line or a group saves the reader from matching colors to a legend and back. A legend is unavoidable when marks are too dense to label, and then it should list entries in the same order as they appear in the chart.
  • Encode consistently. If women are pink circles in one chart of an analysis, they should be pink circles in every chart.
  • Sort categories by something meaningful, usually their value, unless the categories have a natural order of their own, which must be kept (Visualizing Data with One Feature shows a chart of 635 categories sorted by frequency).
  • Title and caption say what the chart shows and what to notice, with units on every axis.
  • Match the display to the reader. A violin plot or a parallel coordinates plot may be the best summary for an analyst and an obstacle for a general audience; a simple dot chart with a sentence of explanation often serves better than a sophisticated display that needs a tutorial.

Color

Color does three different jobs in charts, and each needs a different kind of scale (Harrower and Brewer, 2003; matplotlib developers, "Choosing Colormaps"):

  • Qualitative (categorical) palettes distinguish unordered categories, with colors of different hue and no implied order.
  • Sequential scales encode ordered quantities, running from light to dark (or dark to light on a dark background) in one hue or a few.
  • Diverging scales encode quantities with a meaningful midpoint, such as correlations or changes, with a neutral color at the midpoint and two hues of increasing strength on either side.

Lightness carries order

People perceive differences in lightness as differences in quantity much more reliably than differences in hue, so a color scale for ordered data should change monotonically in lightness, and in equal steps for equal steps of the data. Lightness can be measured: the CIELAB color space has a lightness coordinate $L^*$ that runs from 0 (black) to 100 (white) and was designed so that equal differences are roughly equally perceptible.

CIELAB lightness L along five of matplotlib's color maps, computed at 256 points. Jet rises from 12.9 to a peak of 95.9 at 0.64 of the way along and falls back to 25.4, so low and high values end up similarly dark and the brightest band, yellow, sits at an arbitrary value. The hsv map is cyclic and rises and falls several times. Viridis and cividis rise steadily from about 14 to 91, as a grayscale does from 0 to 100.
CIELAB lightness L* along five of matplotlib's color maps, computed at 256 points. Jet rises from 12.9 to a peak of 95.9 at 0.64 of the way along and falls back to 25.4, so low and high values end up similarly dark and the brightest band, yellow, sits at an arbitrary value. The hsv map is cyclic and rises and falls several times. Viridis and cividis rise steadily from about 14 to 91, as a grayscale does from 0 to 100.

The figure shows why the rainbow map jet, long a default in scientific software, distorts data. Its lightness rises from 12.9 at the low end to 95.9 at 64% of the way along, at the yellow band, and then falls to 25.4 at the high end. Values near the bottom and near the top are therefore similarly dark, the brightest color marks a value with no special meaning, and the rate of change in lightness varies along the map, so equal steps in the data look like unequal steps on the chart. Crameri, Shephard and Heron (2020) show that such maps visibly distort known images and patterns and are unreadable for many people with color-vision deficiency. Viridis and cividis rise in lightness from 14.9 and 13.9 to 90.9 and 91.2 without a reversal, and convert to a usable grayscale. Matplotlib's documentation recommends maps with monotonically increasing lightness for this reason, and notes that maps such as jet and hsv become uninterpretable when printed in gray.

Color-vision deficiency

About 8% of men and 0.5% of women worldwide have a color-vision deficiency, most commonly a reduced ability to distinguish red from green; the share is lower in some populations (Crameri et al., 2020). The two most common severe forms are protanopia and deuteranopia, in which one of the three kinds of cone cell is missing. Machado, Oliveira and Fernandes (2009) model these conditions with a matrix that maps a color's red, green and blue components to the color a person with the deficiency perceives; the computation here applies their matrices to linear RGB, before the gamma encoding of sRGB, as common implementations of the model do. Applying their matrices for complete protanopia and deuteranopia to pairs of colors, and measuring the difference between the two colors of each pair as the Euclidean distance in CIELAB ($\Delta E^*_{ab}$), gives the figure below.

Four pairs of colors as seen with normal color vision, simulated for protanopia and deuteranopia (Machado et al., 2009), and reduced to lightness alone. ΔE is the CIELAB distance between the two colors of a pair (for the last row, the difference in L). Matplotlib's default red and green differ by 120 units, and by 7 under deuteranopia, when both become olive. Blue and orange stay distinct under both deficiencies, but this site's blue and orange have almost the same lightness and merge in grayscale.
Four pairs of colors as seen with normal color vision, simulated for protanopia and deuteranopia (Machado et al., 2009), and reduced to lightness alone. ΔE is the CIELAB distance between the two colors of a pair (for the last row, the difference in L*). Matplotlib's default red and green differ by 120 units, and by 7 under deuteranopia, when both become olive. Blue and orange stay distinct under both deficiencies, but this site's blue and orange have almost the same lightness and merge in grayscale.

Matplotlib's default red and green (tab:red and tab:green) differ by $\Delta E = 119.8$ with normal vision, by 37.6 under simulated protanopia and by only 7.3 under deuteranopia, when both become muddy olive. Even pure red and pure green, which also differ in lightness, fall from 170.6 to 27.8. Blue and orange pairs survive: this site's blue and orange differ by 113.9 with normal vision, 97.3 under protanopia and 113.8 under deuteranopia, and the blue and vermilion of the palette proposed by Okabe and Ito (2008) for color-blind readers behave similarly. Hence the standard advice: avoid distinguishing categories by red against green alone, and prefer pairs built on blue against orange or yellow.

Two further results in the same computation are worth noting. First, the blue and orange that survive color-vision deficiency so well have nearly the same lightness ($L^* = 55.9$ and $54.1$, a WCAG contrast ratio of 1.07), so in grayscale print, or for the rare reader with no color vision, they are the same gray. Second, no palette of eight colors keeps every pair distinct. Under simulated protanopia, the closest pair among this site's eight series colors, its blue and its violet, differ by only $\Delta E = 2.5$, and the closest pair in matplotlib's default palette (its orange and green) by 4.6. The site's palette was checked so that adjacent slots stay distinct, which is why the slots are used in order. The safe practice is therefore threefold, and it does not depend on color at all: use few colors, encode the same distinction redundantly by shape, line style or position, and label directly so that no reader depends on matching colors. Many tools offer palettes designed for this purpose (seaborn's "colorblind", the Okabe and Ito palette), and simulators that preview a chart as it would be seen with each deficiency.

A checklist

  1. Put the most important comparison on a common scale: positions along one axis, bars from one baseline.
  2. Start bars at zero; let points and lines use the range of the data.
  3. Prefer sorted bars or dots to pies, and dots to stacked segments for comparing parts across groups.
  4. Draw the data, not only summaries; state $n$.
  5. Label axes with units, label scale changes, and label lines and groups directly.
  6. Remove ink that encodes nothing; keep what helps reading.
  7. Use a sequential, diverging or qualitative color scale to match the data; for ordered data use one that rises steadily in lightness.
  8. Do not rely on red against green, or on color alone; check the chart in grayscale and under simulated color-vision deficiency.

Corrections to the course notes

The unit's notes state that studies "consistently show" lengths are compared far more accurately than angles; the experiment most often cited for this, Cleveland and McGill (1984), compared bar charts read by position along a common scale with pie charts, and found position judgments 1.96 times as accurate as angle judgments, while their length judgments, on stacked bars, had average errors themselves 1.4 to 2.5 times those of position judgments. The ranking therefore separates position from length as well as length from angle, and the case for dot charts rests on that. The notes also describe seaborn's default palette as reasonably safe for color-vision deficiency; the computation above finds that matplotlib's default palette, of which seaborn's default palette is a variation, includes a green and a red that differ by only 7.3 units under simulated deuteranopia, so defaults should be checked rather than assumed safe.

In Python

import matplotlib.pyplot as plt
import seaborn as sns

# a dot chart: categories on one axis, shares on a common scale, one marker per group
fig, ax = plt.subplots()
for (group, row), marker in zip(shares.iterrows(), ["o", "s", "^"]):
    ax.plot(100 * row.values, range(len(row)), marker, label=f"ages {group}")
ax.set_yticks(range(len(shares.columns)), shares.columns)
ax.set_xlim(0, 70)
ax.set_xlabel("Percent of age group")

plt.imshow(values, cmap="viridis")               # sequential, rising lightness
plt.imshow(correlations, cmap="RdBu_r", vmin=-1, vmax=1)   # diverging, centered at zero
sns.set_palette("colorblind")                    # qualitative palette for colour-blind readers

Every number in this article is computed by tools/experiments/data-science/data-visualization-best-practices.py in the site's repository, which takes the NHANES adults from tools/experiments/data-science/exploredata.py, implements the CIELAB conversion and the simulation matrices of Machado et al. (2009), and reads the color maps from matplotlib; the figures are drawn by tools/figures/data-visualization-best-practices.py.

References

  • Anscombe, F. J. (1973). Graphs in statistical analysis. The American Statistician, 27(1), 17–21. record · 4 pages
  • Cleveland, W. S. (1993). Visualizing Data. Hobart Press. record
  • Cleveland, W. S., and McGill, R. (1984). Graphical perception: Theory, experimentation, and application to the development of graphical methods. Journal of the American Statistical Association, 79(387), 531–554. record · 2 pages
  • Crameri, F., Shephard, G. E., and Heron, P. J. (2020). The misuse of colour in science communication. Nature Communications, 11, 5444. https://doi.org/10.1038/s41467-020-19160-7 record
  • Harrower, M., and Brewer, C. A. (2003). ColorBrewer.org: An online tool for selecting colour schemes for maps. The Cartographic Journal, 40(1), 27–37. record
  • Machado, G. M., Oliveira, M. M., and Fernandes, L. A. F. (2009). A physiologically-based model for simulation of color vision deficiency. IEEE Transactions on Visualization and Computer Graphics, 15(6), 1291–1298. Simulation matrices at https://www.inf.ufrgs.br/~oliveira/pubs_files/CVD_Simulation/CVD_Simulation.html record
  • matplotlib developers. Choosing colormaps in Matplotlib. https://matplotlib.org/stable/users/explain/colors/colormaps.html record
  • National Center for Health Statistics (2020). National Health and Nutrition Examination Survey 2017–2018: Demographics (DEMO_J) and Blood Pressure (BPX_J) data files and documentation. Centers for Disease Control and Prevention. https://wwwn.cdc.gov/nchs/nhanes/continuousnhanes/default.aspx?BeginYear=2017 record · 45 pages
  • Okabe, M., and Ito, K. (2008). Color Universal Design (CUD): How to make figures and presentations that are friendly to colorblind people. https://jfly.uni-koeln.de/color/ record
  • Tufte, E. R. (1983). The Visual Display of Quantitative Information. Graphics Press. record · 2 pages
  • Whelton, P. K., Carey, R. M., Aronow, W. S., et al. (2018). 2017 ACC/AHA/AAPA/ABC/ACPM/AGS/APhA/ASH/ASPC/NMA/PCNA guideline for the prevention, detection, evaluation, and management of high blood pressure in adults. Hypertension, 71(6), e13–e115. record