简体   繁体   中英

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.

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. 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)

That hasn't worked, of course, with seaborn documentation telling me that y has to be equal to a column in my dataframe. And I can't seem to find a way to get a simple count of element as my measure on y axis. I've also tried counting my unique_id , but that hasn't worked either.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM