mutlugazete.com

Creating Accessible Graphs for Colorblind Users: A Guide

Written on

Understanding Color Vision Deficiency

Color vision deficiency, commonly known as color blindness, impacts nearly 8% of men and about 0.5% of women. The predominant type is red-green color blindness, a hereditary condition where individuals lack the ability to perceive either red or green wavelengths. Consequently, they struggle to differentiate between color pairs such as red and green, cyan and grey, and blue and purple.

For data scientists, it is crucial to account for colorblind individuals when selecting color schemes for visualizations. A notable number of users will experience some form of color vision deficiency, which may affect their interpretation of graphs. My personal realization of this need occurred when my supervisor, unable to interpret my line graphs, revealed his challenges in distinguishing red from green.

This guide will cover:

  • A Python simulator for the most prevalent types of color blindness.
  • An accessibility evaluation of default color schemes in matplotlib and seaborn.
  • Strategies for creating inclusive color palettes using ColorBrewer.
  • Insights into the perception of color blindness.

Color Perception and Visual Representation

Color vision is enabled by cone cells in the retina, which respond to red, green, and blue light. Individuals with red-green deficiencies are either missing the cones for red light or those for green light. The absence of both types is rare, as is complete achromatopsia, where no color receptors are present.

For those who can see all colors, visualizing how graphs appear to someone with color blindness can be challenging. Color blindness simulators serve as valuable tools for assessing the accessibility of your graphics. The simulation below utilizes the DaltonLens online color simulator, employing the Brettel algorithm.

Color blindness simulation using DaltonLens

Python Simulators for Color Vision Deficiency

The DaltonLens project offers several algorithms to simulate color vision deficiencies, and its source code is accessible on GitHub. You can install the library with the following command:

pip install daltonlens

Using the Python Image Library (PIL), we can create a test image featuring a graph with two lines:

from matplotlib import pyplot as plt

import numpy as np

# Generate a test image with two line plots

fig, ax = plt.subplots(1, 1)

ax.plot(x, y1, label='data 1', color='red')

ax.plot(x, y2, label='data 2', color='green')

ax.set_xlabel('x')

ax.set_ylabel('y')

ax.legend()

plt.savefig('./plots/2024-dalton-lines.jpg', bbox_inches='tight')

Next, we load the test image, allowing the color blindness simulators to alter the RGB values, mimicking the perception of a person with color vision deficiency:

from daltonlens import convert, simulate, generate

import PIL

# Load the original image and convert it into a numpy array

im = np.asarray(PIL.Image.open('./plots/2024-dalton-lines.jpg').convert('RGB'))

# Create a simulator using the Brettel 1997 algorithm.

simulator = simulate.Simulator_Brettel1997()

# Simulate deutan anomaly ("green-blindness")

greenblind_im = simulator.simulate_cvd(im, simulate.Deficiency.DEUTAN, severity=1.0)

# Generate an image from the transformed array

im2 = PIL.Image.fromarray(greenblind_im)

A visual comparison of the original and simulated images demonstrates that the red and green lines are nearly indistinguishable for those with green-blindness.

Evaluating Default Color Schemes

The default color scheme in Python's matplotlib library consists of eight colors for line plots. When we simulate how this graph appears to a green-blind individual, the colors remain somewhat distinguishable, though they appear more similar than in the original graph. For instance, blue and purple become hard to differentiate.

In this case, employing different line styles (dashed, dotted, solid) can enhance accessibility. Additionally, limiting the number of lines in a graph can prevent clutter.

To illustrate how color blindness affects two-dimensional data, consider the following code that generates sample data with a color map transitioning from red to yellow to green:

# Create an example figure

fig, ax = plt.subplots(1, 1)

xx, yy = np.meshgrid(np.linspace(0, 2*np.pi, 40), np.linspace(0, 2*np.pi, 40))

zz = np.cos(xx) + np.sin(yy)

img = ax.imshow(zz, cmap='RdYlGn')

plt.colorbar(img)

ax.set_xlabel('x')

ax.set_ylabel('y')

fig.tight_layout()

plt.savefig('./plots/2024-dalton-mpl-heat-rdylgn.jpg')

The resulting heatmap demonstrates that this color map is not suitable for colorblind viewers, as positive and negative values can appear similar.

Fortunately, there are color palettes designed to be more accessible to colorblind individuals. The following panel shows a heatmap using the viridis color map, transitioning from yellow to blue.

Creating Bar Charts with Accessibility in Mind

Statistical graphs, such as bar charts, often utilize color for enhanced clarity. Using the seaborn plotting library, we can create a bar plot with toy data:

import pandas as pd

import seaborn as sns

fig, ax = plt.subplots(1, 1)

x = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']

y = np.random.rand(len(x))

data = pd.DataFrame(dict(x=x, y=y))

# Plot with seaborn

sns.barplot(data=data, x=x, y=y)

ax.set_xlabel('Category')

ax.set_ylabel('Value')

fig.tight_layout()

plt.savefig('./plots/2024-dalton-bar.jpg')

Upon simulating for green-blindness, the colors for categories C, D, and F (green, red, and brown) become closely matched. This indicates that color does not significantly enhance the clarity of the plot, and it may be advisable to present all bars in a single color.

ColorBrewer Palettes for Enhanced Accessibility

When I need to craft a custom color palette beyond the defaults provided in matplotlib and seaborn, I often refer to the ColorBrewer website. Initially intended for cartographic data, it offers a useful feature for color-blind-friendly palettes.

Users can generate palettes with up to 12 distinct colors, which can then be exported as hex values for use in plotting libraries.

ColorBrewer website showcasing accessible palettes

Conclusion

Color vision deficiency is prevalent, affecting around 8% of the population, with green-blindness and red-blindness being the most frequent types. Graphs convey meaning through color, making it essential to ensure that visualizations are accessible.

Color blindness simulators are invaluable for testing the accessibility of graphs and can be integrated directly into Python code for expedient use. Employing a color blindness check when selecting colors and palettes can significantly enhance the accessibility of your visualizations, ensuring they communicate effectively to all users.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

The Importance of Travel Agents in Today's World

Discover why using a travel agent is essential for stress-free travel in uncertain times.

Exploring Genetics: Insights from Siddhartha Mukherjee's 'The Gene'

A deep dive into Siddhartha Mukherjee's 'The Gene', exploring genetics' past, present, and future along with personal stories and ethical dilemmas.

Maximizing Your Learning with Double-Loop Strategies

Discover how double-loop learning can enhance your thinking and help you achieve the right goals in life.