1

My dataset is called 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.

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.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114

1 Answers1

0
  1. You need to use a bar function, not a histogram function. Histogram functions take raw data and count them, but your data are already counted.
  2. You need to melt the french and flemish columns into "long form." Then x will be the language, and y will be the counts.
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
tdy
  • 36,675
  • 19
  • 86
  • 83