Skip to content

Commit 35e539e

Browse files
Add pypalettes blog post (#247)
1 parent fdfe4ff commit 35e539e

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
---
2+
title: "PyPalettes: all the colors you'll ever need"
3+
description: "Matplotlib is the go-to library for data visualization in Python. While it offers quality built-in colormaps like viridis and inferno, the limited selection can make Matplotlib charts look similar. To address this, I developed pypalettes, a Python library with over 2,500 high-quality, pre-made color palettes, based on Paletteer. The library includes a web app for browsing and previewing all of them."
4+
date: 2025-04-01
5+
tags: ["matplotlib", "color", "colormap"]
6+
displayInList: true
7+
author: ["Joseph Barbier", "Yan Holtz"]
8+
draft: false
9+
resources:
10+
- name: featuredImage
11+
src: "preview.png"
12+
params:
13+
description: "pypalettes library"
14+
showOnTop: false
15+
---
16+
17+
## Finding the right color has never been easier
18+
19+
[PyPalettes](https://github.com/JosephBARBIERDARNAL/pypalettes) is a new Python library designed to simplify the use of color palettes in Python charts.
20+
21+
It provides mainly two things:
22+
23+
- a [super-easy-to-use library](https://github.com/JosephBARBIERDARNAL/pypalettes) that requires only 1 line of code (in 99.99% of cases, 2 otherwise 🙃) to access thousands of pre-defined and attractive palettes.
24+
- a [web app](https://python-graph-gallery.com/color-palette-finder/) to browse, filter, search, and preview all available palettes (with **bonus**: copy-pastable code to reproduce the charts).
25+
26+
[![Preview and try all the palettes](https://github.com/holtzy/The-Python-Graph-Gallery/raw/master/static/asset/pypalettes.gif)](https://python-graph-gallery.com/color-palette-finder/)
27+
28+
<center><i>A small sample of the available palettes</i></center>
29+
30+
<br>
31+
32+
## From R to Python
33+
34+
In R, there are dozens of packages dedicated to colors for data visualization. Then [Paletteer](https://emilhvitfeldt.github.io/paletteer/) came out to **aggregate** every color palette from those packages into a single one, meaning you **only need one package** to access almost all the color palettes people have created!
35+
36+
While re-crafting the [colors section of the Python Graph Gallery](https://python-graph-gallery.com/python-colors/), I started thinking of a way to have a similar tool to Paletteer but for Python.
37+
38+
<center><h3 style="color: lightgray;">That's where PyPalettes comes in.</h3></center>
39+
40+
Paletteer has a community-maintained [gallery](https://pmassicotte.github.io/paletteer_gallery/)—a single page showcasing all its color palettes, along with their original sources and names. With the author’s approval, I scraped this gallery to compile the data.
41+
42+
While there may have been other ways to obtain this information, using a short Python script to reproduce the dataset ensures both simplicity and reproducibility (the script scrapes a page stored locally instead of the web page). To make **pypalettes** more comprehensive, I also incorporated all **built-in colors** from `Matplotlib`.
43+
44+
As a result, I created a dataset containing approximately **2,500 unique palettes**, each with a name, a list of hexadecimal colors, and a source.
45+
46+
At this point, the hardest part was already done. I just had to create a simple API to make them usable in a Python environment and add some additional simple features.
47+
48+
And since [Yan](https://www.yan-holtz.com/) supported the idea, he created this amazing [web app](https://python-graph-gallery.com/color-palette-finder/), making it much easier to browse available palettes.
49+
50+
As a thank-you to `Paletteer`, Yan also created a color finder that features only `Paletteer` palettes! If you use R, [check it out here](https://r-graph-gallery.com/color-palette-finder).
51+
52+
<br>
53+
54+
## How to use pypalettes
55+
56+
The goal was to make the simplest API possible, and I'm quite satisfied with the result. For example, you really like the ["Esox lucius" palette](https://python-graph-gallery.com/color-palette-finder/?palette=Esox_lucius), and you want to make a chart with it.
57+
58+
First, you import the `load_cmap()` function (the main function of the library):
59+
60+
```python
61+
from pypalettes import load_cmap
62+
```
63+
64+
And then you just have to call this function with `name="Esox_lucius"`
65+
66+
```python
67+
cmap = load_cmap("Esox_lucius")
68+
```
69+
70+
The output of `load_cmap()` is either a [matplotlib.colors.ListedColormap](https://matplotlib.org/stable/api/_as_gen/matplotlib.colors.ListedColormap.html) or a [matplotlib.colors.LinearSegmentedColormap](https://matplotlib.org/stable/api/_as_gen/matplotlib.colors.LinearSegmentedColormap.html), depending on the value of the `cmap_type` argument (default is `"discrete"`, so it's `ListedColormap` in this case).
71+
72+
Finally, you can create your chart as you normally would:
73+
74+
```python
75+
# load libraries
76+
import cartopy.crs as ccrs
77+
import geopandas as gpd
78+
import matplotlib.pyplot as plt
79+
from matplotlib.font_manager import FontProperties
80+
from highlight_text import fig_text
81+
82+
# load font
83+
personal_path = "/Users/josephbarbier/Library/Fonts/" # change this to your own path
84+
font = FontProperties(fname=personal_path + "FiraSans-Light.ttf")
85+
bold_font = FontProperties(fname=personal_path + "FiraSans-Medium.ttf")
86+
87+
# projection
88+
proj = ccrs.Mercator()
89+
90+
# load the world dataset
91+
df = gpd.read_file(
92+
"https://raw.githubusercontent.com/holtzy/The-Python-Graph-Gallery/master/static/data/all_world.geojson"
93+
)
94+
df = df[~df["name"].isin(["Antarctica"])]
95+
df = df.to_crs(proj.proj4_init)
96+
97+
fig, ax = plt.subplots(figsize=(12, 8), dpi=300, subplot_kw={"projection": proj})
98+
ax.set_axis_off()
99+
df.plot(
100+
column="name",
101+
ax=ax,
102+
cmap=cmap, # here we pass the colormap loaded before
103+
edgecolor="black",
104+
linewidth=0.2,
105+
)
106+
fig_text(
107+
x=0.5,
108+
y=0.93,
109+
s="World map with <PyPalettes> colors",
110+
fontsize=25,
111+
ha="center",
112+
font=font,
113+
highlight_textprops=[{"font": bold_font}],
114+
)
115+
fig_text(
116+
x=0.85, y=0.14, s="Joseph Barbier & Yan Holtz", fontsize=8, ha="right", font=font
117+
)
118+
119+
plt.show()
120+
```
121+
122+
<center>
123+
124+
![](map.png)
125+
126+
</center>
127+
128+
And once the code is working, you can change the color map name and see straight away what it would look like!
129+
130+
<br>
131+
132+
## Other usages
133+
134+
PyPalettes is primarily designed for `matplotlib` due to its **high compatibility** with the `cmap` argument, but one can imagine **much more**.
135+
136+
For example, the output of `load_cmap()` includes attributes like `colors` and `rgb`, which return lists of hexadecimal colors or RGB values. These can be used in **any context**—from Python visualization libraries like Plotly, Plotnine, and Altair to colorimetry, image processing, or any application that requires color!
137+
138+
<br>
139+
140+
## Learn more
141+
142+
The main links to find out more about this project are as follows:
143+
144+
- the [web app](https://python-graph-gallery.com/color-palette-finder/) to browse the palettes
145+
- this [introduction to PyPalettes](https://python-graph-gallery.com/introduction-to-pypalettes/) for a more in-depth code explanation
146+
- the [Github repo](https://github.com/JosephBARBIERDARNAL/pypalettes) with source code and palettes (give us a star! ⭐)
209 KB
Loading
Loading

0 commit comments

Comments
 (0)