Skip to content Skip to sidebar Skip to footer

How To Change Colormap In Joypy Plot?

I have a dataframe which looks like this: Team Minute Type 148 12 1 148 22 1 143 27 1 148 29 1 143 32 1 143

Solution 1:

joypy fills the colors of the KDE curves sequentially from a colormap. So in order to have the colors match to a third variable you can supply a colormap which contains the colors in the order you need. This can be done using a ListedColormap.

import matplotlib
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(21)
import pandas as pd
import joypy

df = pd.DataFrame({"Team" : np.random.choice([143,148,159,167], size=200),
                   "Minute" : np.random.randint(0,100, size=200)})

##getting the sum for every team - total of 20 teams
group_df = df.groupby(["Team"]).size().to_frame("Count").reset_index()
print(group_df)



##Trying to create a colormap 
norm = plt.Normalize(group_df["Count"].min(), group_df["Count"].max())
ar = np.array(group_df["Count"])

original_cmap = plt.cm.viridis
cmap = matplotlib.colors.ListedColormap(original_cmap(norm(ar)))
sm = matplotlib.cm.ScalarMappable(cmap=original_cmap, norm=norm)
sm.set_array([])

fig, axes = joypy.joyplot(df, by="Team", column="Minute", x_range = [0,94], colormap = cmap)
fig.colorbar(sm, ax=axes, label="Count")

plt.show()

enter image description here

Post a Comment for "How To Change Colormap In Joypy Plot?"