如何在 Joypy 图中更改颜色图? [英] How to change colormap in joypy plot?
问题描述
我有一个看起来像这样的数据框:
团队分钟类型148 12 1148 22 1143 27 1148 29 1143 32 1143 32 1
我使用Python库
一切都很好.但是,颜色图现在已经没有意义了,因此我尝试根据第二个数据框为图表着色-这是所有团队的 Type
的总和.
为此,我创建了一个规范,并使用这些行创建了一个颜色图:
norm = plt.Normalize(group_df[Type"].min(), group_df[Type"].max())cmap = plt.cm.viridissm = matplotlib.cm.ScalarMappable(cmap = cmap,norm = norm)ar = np.array(group_df[类型"])Cm = cmap(norm(ar))sm.set_array([])
在这里出现了问题,因为我不知道如何更改操纵杆的颜色.我尝试了几种方法:
-
我尝试将
Cm
作为colormap参数传递.但是,这引发了一个错误 -typeerror 'numpy.ndarray' object is not callable
我尝试在
axes
和Cm
上使用 for 循环 -
for col, ax in zip(Cm, axes):ax.set_facecolor(col)#ax.patch.set_facecolor(col)##也尝试过这个;没有改变任何东西
如何更好地控制操纵杆的颜色并进行更改?任何帮助将不胜感激.
MCVE
我正在读取的csv文件的示例(数据帧的实际形状为(4453,2)):
小组会议记录0 148 51 148 52 148 113 148 114 148 125 148 226 143 27
我的代码:
df = pd.read_csv(r路径")##获取每个团队的总和-总共20个团队group_df = df.groupby(["Team"]).size().to_frame("Count").reset_index()df ["Minute"] = pd.to_numeric(df ["Minute"])##尝试创建颜色图norm = plt.Normalize(group_df[Count"].min(), group_df[Count"].max())cmap = plt.cm.viridissm = matplotlib.cm.ScalarMappable(cmap = cmap,norm = norm)ar = np.array(group_df[计数"])Cm = cmap(norm(ar))sm.set_array([])图,轴= joypy.joyplot(df,by =团队",列=分钟",figsize =(10,16),x_range = [0,94],色图= plt.cm.viridis)
我想根据 group_df[Count"]
值中的团队总数为图中的每个子图着色.目前,颜色图只是统一的,而不是根据总值.上图是制作出来的.
joypy 从颜色图中按顺序填充 KDE 曲线的颜色.因此,为了使颜色与第三个变量匹配,您可以提供一个色图,其中包含所需顺序的颜色.可以使用 ListedColormap
完成.
import matplotlib导入matplotlib.pyplot作为plt将 numpy 导入为 np;np.random.seed(21)将熊猫作为pd导入导入欢乐df = pd.DataFrame({"Team":np.random.choice([143,148,159,167],size = 200),分钟":np.random.randint(0,100,size = 200)})##获取每支球队的总和 - 总共 20 支球队group_df = df.groupby(["Team"]).size().to_frame("Count").reset_index()打印(group_df)##尝试创建颜色图规范= plt.Normalize(group_df ["Count"].min(),group_df ["Count"].max())ar = np.array(group_df["Count"])original_cmap = plt.cm.viridiscmap = matplotlib.colors.ListedColormap(original_cmap(norm(ar)))sm = matplotlib.cm.ScalarMappable(cmap=original_cmap, norm=norm)sm.set_array([])无花果,轴= joypy.joyplot(df,by ="Team",column ="Minute",x_range = [0,94],colormap = cmap)fig.colorbar(sm,ax = axes,label ="Count")plt.show()
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.
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()
这篇关于如何在 Joypy 图中更改颜色图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!