[英]Bar plot of two columns in x-axis in seaborn
My dataset is called df
:我的数据集称为
df
:
year![]() |
french![]() |
flemish![]() |
---|---|---|
2014 ![]() |
200 ![]() |
200 ![]() |
2015 ![]() |
170 ![]() |
210 ![]() |
2016 ![]() |
130 ![]() |
220 ![]() |
2017 ![]() |
120 ![]() |
225 ![]() |
2018 ![]() |
210 ![]() |
250 ![]() |
I want to create a histogram in seaborn with french
and flemish
on the x-axis and year
as the hue.我想在
flemish
中创建一个直方图,在 x 轴上使用french
和佛兰芒语,以year
作为色调。
I tried this, but it didn't work successfully:我试过了,但没有成功:
sns.histplot(data=df, x="french", hue="year", multiple="dodge", shrink=.8)
The y-axis should show the height of the number of the columns of french
and flemish
. y 轴应显示
french
和flemish
列数的高度。
melt
the french
and flemish
columns into " long form ."french
和flemish
柱melt
为“ 长式”。 Then x
will be the language, and y
will be the counts.x
将是语言, y
将是计数。sns.barplot(data=df.melt("year", var_name="language", value_name="count"),
x="language",
y="count",
hue="year")
plt.legend(loc=(1.05, 0))
The melted dataframe for reference:融化的数据框供参考:
>>> df.melt("year", var_name="language", value_name="count")
# year language count
# 0 2014 french 200
# 1 2015 french 170
# 2 2016 french 130
# 3 2017 french 120
# 4 2018 french 210
# 5 2014 flemish 200
# 6 2015 flemish 210
# 7 2016 flemish 220
# 8 2017 flemish 225
# 9 2018 flemish 250
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.