Skip to content
y-sunflowerPublic

About

Simulate colorblindness in Python charts

Topics

Resources

Stars

27 stars

Watchers

0 watching

Forks

Latest commit

 

History

37 Commits

Folders and files

Repository files navigation

realcolor: simulate colorblindness in Python charts

realcolor is a lightweight Python package designed to show how colorblind people see your graphs. It simulates all types of colorblindness (deuteranopia, protanopia, tritanopia).

It works with the following library: matplotlib, seaborn, plotnine and plotly.

Note

Colorblindness affects up to 1 in 12 males (8%) and 1 in 200 females (0.5%)1


Installation

pip install realcolor

Quick start

  • Matplotlib
import matplotlib.pyplot as plt
from realcolor import simulate_colorblindness

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 2, 5], label="Group A", lw=4)
ax.plot([1, 2, 3], [2, 5, 3], label="Group B", lw=4)
ax.legend()

simulate_colorblindness(fig)

Matplotlib line charts simulated for deuteranopia, protanopia, tritanopia, and desaturation

  • Plotnine
from plotnine import ggplot, geom_point, aes
from plotnine.data import anscombe_quartet
from realcolor import simulate_colorblindness

gg = ggplot(anscombe_quartet, aes(x="x", y="y", color="dataset")) + geom_point(size=10)

simulate_colorblindness(gg)

Plotnine scatter plots simulated for deuteranopia, protanopia, tritanopia, and desaturation

  • Plotly
import plotly.express as px
from realcolor import simulate_colorblindness

fig = px.scatter(
    x=[1, 2, 3, 4],
    y=[4, 2, 5, 3],
    color=["Group A", "Group A", "Group B", "Group B"],
)

simulate_colorblindness(fig)

Plotly scatter plots simulated for deuteranopia, protanopia, tritanopia, and desaturation


Other features

  • Simulate just one kind of colorblindness (one of "deuteranopia", "protanopia", "tritanopia", "desaturated"):
simulate_colorblindness(fig, kind="protanopia")

Two-group line chart simulated for protanopia

  • Control the severity of the simulation (between 0 to 100, default to 100):
simulate_colorblindness(fig, kind="protanopia", severity=50)

Two-group line chart simulated for protanopia at reduced severity

  • Score how colorblind-friendly a set of colors is (0 = indistinguishable, 100 = perfectly distinguishable):
from realcolor import colorblind_score

score = colorblind_score(["red", "green", "blue"])

score.overall
# > 52.1

score.deuteranopia
# > {"score": 66.7, "min_deltaE": 16.7, "worst_pair": ("#ff0000", "#008000")}

Contributing

  • Fork the repository to your own GitHub account.

  • Clone your forked repository to your local machine (ensure you have Git installed):

git clone https://github.com/YOUR_NAME/realcolor.git
cd realcolor
  • Create a new branch:
git checkout -b my-feature
  • Set up your Python environment (ensure you have uv installed):
uv sync --all-extras --dev
uv run pre-commit install
uv pip install -e .
  • Test that everything works correctly by running:
uv run pytest


Footnotes

  1. Deane B. Judd, "Facts of Color-Blindness*," J. Opt. Soc. Am. 33, 294-307 (1943) ↩

About

Simulate colorblindness in Python charts

Topics

Resources

Stars

27 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages