Skip to content

Commit

Permalink
improve post about pypalettes
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephBARBIERDARNAL committed Jul 5, 2024
1 parent 466e779 commit bcb4eea
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
95 changes: 91 additions & 4 deletions content/posts/matplotlib/pypalettes/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ resources:
params:
description: "pypalettes library"
showOnTop: false
_build:
render: true
list: true
publishResources: false
---

## Finding the right color has never been easier
Expand All @@ -26,3 +22,94 @@ Recently (June 2024), the [pypalettes library](https://github.com/JosephBARBIERD
- a [web app](https://python-graph-gallery.com/color-palette-finder/) to browse, filter, search and preview all the available palettes (and **bonus**: copy-pastable code to reproduce the charts)

_A small sample of the available palettes_[![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/)

<br>

## How it happened

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!

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.

<center><h3 style="color: lightgray;">That's where PyPalettes comes in.</h3></center>

Basically, I scraped 2 websites:

- the [Paletteer gallery](https://pmassicotte.github.io/paletteer_gallery/), which contains all palettes from Paletteer with hexadecimal colors, palette names, and package sources.

This comment has been minimized.

Copy link
@stefanv

stefanv Jul 5, 2024

Member

You'd probably need to include

https://github.com/EmilHvitfeldt/paletteer/blob/main/LICENSE.note

Also, no need to scrape when the data is all at https://github.com/EmilHvitfeldt/paletteer/tree/main/data-raw

If you have a way of re-using their data, then you don't need to re-scrape periodically.

This comment has been minimized.

Copy link
@JosephBARBIERDARNAL

JosephBARBIERDARNAL Jul 6, 2024

Author

Makes sense. I didn't know there was raw data, thanks again!

This comment has been minimized.

Copy link
@JosephBARBIERDARNAL

JosephBARBIERDARNAL Jul 6, 2024

Author

After thinking about it, I'm going to keep the scraping code as it means I only need 2 files (html (local web page with palettes) + py) to retrieve the data.
I think it's simpler?

- [coolors.co](https://coolors.co/), another website with cool palettes.

This comment has been minimized.

Copy link
@stefanv

stefanv Jul 5, 2024

Member

Almost certainly, you are not allowed to distribute these colormaps:

https://coolors.co/license

This comment has been minimized.

Copy link
@JosephBARBIERDARNAL

JosephBARBIERDARNAL Jul 6, 2024

Author

Oh, you're right. I didn't even think to look at the licence. I'll delete them asap and make a new version too.
Thank you so much.


With these, I was able to create a dataset with around **2500 different palettes**, each with a name and a list of hexadecimal colors.

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.

<br>

## How to use it

The goal was to make the simplest API possible, and I'm quite satisfied with the result. For example, I really like the [Ingres palette](https://python-graph-gallery.com/color-palette-finder/?palette=ingres), and I want to make a chart with it.

First, you import the `load_cmap()` function (the main function of the library):

```python
from pypalettes import load_cmap
```

And then you just have to call this function with `name="Ingres"`

```python
cmap = load_cmap("Ingres")
```

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 `type` argument (default is `"discrete"`, so it's `ListedColormap` in this case).

Finally, you can create your chart as you normally would:

```python
# load libraries
import geopandas as gpd
import matplotlib.pyplot as plt
from pypalettes import load_cmap

# load the world dataset
df = gpd.read_file(
"https://raw.githubusercontent.com/holtzy/The-Python-Graph-Gallery/master/static/data/all_world.geojson"
)
df = df[~df["name"].isin(["Antarctica"])]

fig, ax = plt.subplots(figsize=(10, 10), dpi=300)
ax.set_axis_off()
df.plot(
ax=ax,
cmap=cmap, # here we pass the colormap loaded before
edgecolor="white",
linewidth=0.3,
)

plt.show()
```

<center>

![](map.png)

</center>

And once the code is working, you can change the color map name and see straight away what it would look like!

<br>

## Other usages

PyPalettes is primarily designed for `matplotlib` due to its **high compatibility** with the `cmap` argument, but one can imagine **much more**.

For example, it also provides the `get_hex()` and `get_rgb()` functions that return a list of hexadecimal colors or RGB values that can then be used in any context: other Python visualization libraries (plotly, plotnine, altair), colorimetry, image processing, or anything that requires color!

<br>

## Learn more

The main links to find out more about this project are as follows:

- the [web app](https://python-graph-gallery.com/color-palette-finder/) to browse the palettes
- the [Github repo](https://github.com/JosephBARBIERDARNAL/pypalettes) with source code and palettes (give us a star! ⭐)
- this [introduction to PyPalettes](https://python-graph-gallery.com/introduction-to-pypalettes/) for a more in-depth code explanation
Binary file added content/posts/matplotlib/pypalettes/map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified content/posts/matplotlib/pypalettes/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bcb4eea

Please sign in to comment.