Accessibility and Human Factors in Plotting#

Whenever you are generating plots to go into a paper or a presentation, there are a few things you can do to make sure that everyone can understand your plots:

Always make sure your text is large enough to read.#

  • In Matplotlib plot commands, Use the fontsize parameter in xlabel, ylabel, title, and legend, and tick_params with labelsize to increase the text size of the numbers on your axes.

Make graph elements easy to see#

  • Similarly, you should make the lines and symbols in your plots themselves bit enough to see comfortably. In Matplotlib plot commands, you can use s to increase the size of your scatterplot markers and linewidth to increase the sizes of your plot lines.

Seaborn contexts#

Seaborn provides a convenient way scale both text and plot elements all at once, with the sns.set_context() function. “Contexts” are different contexts in which you might want to use a plot, such as in a paper versus a slide presentation or conference poster.

If you import seaborn and run `sns.set_context()`, it affects *all* Matplotlib plots, not just those made with seaborn.

Here’s some code to plot some sine waves using Matplotlib

import seaborn as sns 
import numpy as np
import matplotlib.pyplot as plt

# A simple function for plotting sine waves
def sinplot(flip=1):
    x = np.linspace(0, 14, 100)
    for i in range(1, 7):
        plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)

Plot with default styling; no seaborn context#

sinplot()
../_images/71383ce4c65766a49dd3f0f8f441a5a9a0c4c8defed073d0be79acc7daa09fbd.png

Plot with paper context#

sns.set_context("paper")
sinplot()
../_images/65a1a18cb5be3f77f7c22eb3a7f0f3cc74d43f738498f89a13419c9bc62162c8.png

Plot with talk context#

sns.set_context("talk")
sinplot()
../_images/291aa4fdab758028676deaeaa6d87b1a2a3a93203fceff1a17a11453da92145c.png

Plot with poster context#

sns.set_context("poster")
sinplot()
../_images/3c9644ead4999f854d24f08cd5f4806900a69cb2ff1669e83c35993b538bfaad.png

Consider colorblindness#

A non-negligible proportion of the population has some form of colorblindness. Human color perception relies on our having three different types of cone cells in our retinas. It is estimated that 8% of males (1 in 12) have some form of colorblindness, typically a genetic absence of one of these cone types. colorblindness is caused by a recessive gene on the X chromosome, so it is far less common for females to have it. So if you anticipate that, say, even 50 people view a graph you make, the odds are that 2 or more of them will have some form of colorblindness. Or put another way, if there are 20 people enrolled in this class, there is likely at least one colorblind student.

colorblindness can range from mild forms (common) such as deuteranopia and protanopia (missing green or red cones, respectively; inability to distinguish red and green), and tritanopoa (missing blue cones) to complete achromatopsia (rare).

Using color (and nothing else) to distinguish between different plot elements (like lines representing different categories) may make your plots unreadable to anyone who is colorblind. For lines, the linestyle parameter lets you use different types of lines. For scatterplots, marker lets you change the shape of your points for different categories.

It’s worth trying out Coblis or Color Oracle to simulate what your plots (or any other image) would look like to those with colorblindness. This can be educational, and also a useful tool to check your plots. However, there are many color palettes available in Matplotlib and Seaborn (including the defaults) that are designed to be distinguishable even by people with mild colorblindness. Install Color Oracle on your computer and view this page, and see what the Seaborn plots above look. like!

Matplotlib has an excellent page on choosing colormaps.

But as usual, Seaborn provides an even simpler option. It’s default color palette is quite robust to different forms of colorblindness (i.e., most hues are distinguishable from each other), and it also offers a colorblind palette that is a slightly-adjusted version of the default that makes the hues even more distinguishable. Seaborn’s developer, Markus Waksom, also provides a page simulating what the Seaborn palettes look like with different forms of colorblindness.

sns.set_palette('colorblind')
sinplot()
../_images/903f3fecddfd96add87967b236eae8d312cf147ceeb3c4fd5e45c5b251b9d6fc.png

Note that once you’ve run sns.set_context() and/or sns.set_palette(), the setting apply to all further plots until you change the settings by running those commands again (or restarting the kernel)

sinplot()
../_images/903f3fecddfd96add87967b236eae8d312cf147ceeb3c4fd5e45c5b251b9d6fc.png
sns.set_palette('bright')
sns.set_context('paper')
sinplot()
../_images/63c3effc581b5100d8f6462d7f6789e4b83f5d11ab6411c502e1c0b03d9f2e1e.png