Home > Mobile >  Create a seaborn histogram with two columns of a dataframe
Create a seaborn histogram with two columns of a dataframe

Time:10-24

I try to display a histogram with this dataframe.

   gr_age  weighted_cost
0       1    2272.985462
1       2    2027.919360
2       3    1417.617779
3       4     946.568598
4       5     715.731002
5       6     641.716770

I want to use gr_age column as the X axis and weighted_cost as the Y axis. Here is an example of what I am looking for with Excel:

Histogram example

I tried with the following code, and with discrete=True, but it gives another result, and I didn't do better with displot.

sns.histplot(data=df, x="gr_age", y="weighted_cost")
plt.show()

Thanking you for your ideas!

CodePudding user response:

You want a barplot (x vs y values) not a histplot which plots the distribution of a dataset:

import seaborn as sns
ax = sns.barplot(data=df, x='gr_age', y='weighted_cost', color='#4473C5')
ax.set_title('Values by age group')

output:

seaborn barplot

  • Related