JosephBARBIERDARNAL / pypalettes

A large (+2500) collection of color maps for Python
https://python-graph-gallery.com/color-palette-finder/
Other
235 stars 8 forks source link

mapping with categorical variables #5

Closed JosephBARBIERDARNAL closed 4 months ago

JosephBARBIERDARNAL commented 4 months ago

find a way to automate the mapping between a categorical cmap and a categorical variable (i.e get a list of colors of the same length as the variable:

# load libraries
import squarify # pip install squarify (algorithm for treemap)
import matplotlib.pyplot as plt
from pypalettes import load_cmap
import pandas as pd

# set a higher resolution
plt.rcParams['figure.dpi'] = 300

# create a color palette
cmap = load_cmap('Acadia')
category_codes, unique_categories = pd.factorize(df['parent'])
colors = [cmap(code / (len(unique_categories) - 1)) for code in category_codes]

# create a treemap
fig, ax = plt.subplots(figsize=(10,10))
ax.set_axis_off()
squarify.plot(
   sizes=df["value"],
   label=df["name"],
   ec="white",
   color=colors,
   pad=True,
   ax=ax
)
plt.show()
JosephBARBIERDARNAL commented 4 months ago

wrong problem. see documentation for a tuto