[英]Set y-axis as count of hue using pandas + seaborn
I have a dataframe that looks like this: 我有一个看起来像这样的数据框:
unique_id element day-only
0 52221039 4 28
1 5212263 3 28
2 5244438 LS 27
3 4975676 4 22
4 9023691 1 24
I'm trying to get a lmplot
that maps out the frequency at which element
appears every day, the same as the following countplot, basically. 我正在尝试获取一个lmplot
,该lmplot
映射出element
每天出现的频率,基本上与以下计数图相同。
fig = sns.countplot(x="day-only", hue="element", data=df, hue_order=["1","2","3","4","LS","EA"], palette=color_element)
However, I can't seem to find a way to map my count of element
to the y value. 但是,我似乎找不到一种将element
计数映射到y值的方法。 I've tried 我试过了
horse = df["element"].value_counts()
count_of_y = numpy.asarray(horse)
And then fig = sns.lmplot(x="day-only", y=count_of_y, hue="element", data=df)
然后, fig = sns.lmplot(x="day-only", y=count_of_y, hue="element", data=df)
That hasn't worked, of course, with seaborn documentation telling me that y has to be equal to a column in my dataframe. 当然,这并不是可行的,因为seaborn文档告诉我y必须等于数据框中的一列。 And I can't seem to find a way to get a simple count of element as my measure on y axis. 而且我似乎找不到一种方法来获取简单的元素计数作为我在y轴上的度量。 I've also tried counting my unique_id
, but that hasn't worked either. 我也尝试过计算我的unique_id
,但这也没有用。
This feels like a really simple question, and yet I can't seem to find a solution! 这似乎是一个非常简单的问题,但我似乎找不到解决方案! Thanks for any help! 谢谢你的帮助!
I've realized where my error lay and solved my problem. 我已经意识到我的错误所在,并解决了我的问题。 I created a new dataframe in the following way: 我通过以下方式创建了一个新的数据框:
count_of_y = df["element"].groupby(df["day-only"]).value_counts().rename("counts").reset_index()
count_of_y
Which gave me three columns, with counts in the right place. 这给了我三栏,在正确的位置计数。 I was then able to plot with this. 然后,我就可以对此进行绘图。
fig = sns.lmplot(x="day-only", y="counts", hue="element", data=count_of_y, hue_order=["1","2","3","4","LS", "EA"], palette=color_element)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.