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 32 1
I created a joyplot using the Python library joypy
fig, axes = joypy.joyplot(df, by="Team", column="Minute", figsize =(10,16), x_range = [0,94], linewidth = 1, colormap=plt.cm.viridis)
Which gave me this plot:
All Good.
However, the colourmap is meaningless now so I am trying to color the plots according to a second dataframe - which is the sum of Type
for all the teams.
To do that, I created a norm, and a colourmap using these lines:
norm = plt.Normalize(group_df["Type"].min(), group_df["Type"].max())
cmap = plt.cm.viridis
sm = matplotlib.cm.ScalarMappable(cmap=cmap, norm=norm)
ar = np.array(group_df["Type"])
Cm = cmap(norm(ar))
sm.set_array([])
Here's where the problem arose as I can't figure out how to change the color of the joyplots. I tried a couple of approaches:
I tried to pass this
Cm
as the colormap argument. However, that threw up an error -typeerror 'numpy.ndarray' object is not callable
I tried to use a for loop over the
axes
andCm
-
for col, ax in zip(Cm, axes):
ax.set_facecolor(col)
#ax.patch.set_facecolor(col) ##Also tried this; didn't change anything
How can I get greater control over the colours of the joyplot and change them around? Any help would be appreciated.
MCVE
Sample of the csv file I'm reading in(Actual shape of dataframe is (4453,2)):
Team Minute
0 148 5
1 148 5
2 148 11
3 148 11
4 148 12
5 148 22
6 143 27
My code:
df = pd.read_csv(r"path")
##getting the sum for every team - total of 20 teams
group_df = df.groupby(["Team"]).size().to_frame("Count").reset_index()
df["Minute"] = pd.to_numeric(df["Minute"])
##Trying to create a colormap
norm = plt.Normalize(group_df["Count"].min(), group_df["Count"].max())
cmap = plt.cm.viridis
sm = matplotlib.cm.ScalarMappable(cmap=cmap, norm=norm)
ar = np.array(group_df["Count"])
Cm = cmap(norm(ar))
sm.set_array([])
fig, axes = joypy.joyplot(df, by="Team", column="Minute", figsize =(10,16), x_range = [0,94], colormap = plt.cm.viridis)
I want to color every subplot in the plot by the total count of the team from the group_df["Count"]
values. Currently, the colormap is just uniform and not according to the total value. The picture above is what's produced.