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/f0e1e3f02e99ebdba1954fb3ea852b29665d6e7c934c879c878468dbb57a9708.png

Plot with paper context#

sns.set_context("paper")
sinplot()
../_images/92ac2439e2c64087264632ee61ebba0439f8e18db06972bb3d7e8fc09c66e505.png

Plot with talk context#

sns.set_context("talk")
sinplot()
../_images/65f70770ef58e01114c01b923581f64101fef008f3b40ce9fc0a2125e83b0e47.png

Plot with poster context#

sns.set_context("poster")
sinplot()
../_images/28cb9f951b14427baeb9b3551482b0ccd3294455a214b952aa406bcee2f70da7.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/f8cf5641f3c3f8051da938c23329e792cf394116138ed5178361f7bf169a11f7.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/f8cf5641f3c3f8051da938c23329e792cf394116138ed5178361f7bf169a11f7.png
sns.set_palette('bright')
sns.set_context('paper')
sinplot()
../_images/641abd47786e836242e93dc2d35d465f961a8b3e34508bac2babd3cfe010f724.png