首页 > 解决方案 > 创建包含两列数据框的 seaborn 直方图

问题描述

我尝试使用此数据框显示直方图。

   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

我想使用gr_agecolumn 作为 X 轴和weighted_costY 轴。这是我正在使用 Excel 查找的示例:

直方图示例

我尝试使用以下代码和discrete=True,但它给出了另一个结果,而且我在使用 displot 时并没有做得更好。

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

谢谢你的想法!

标签: pythonpandasdataframematplotlibseaborn

解决方案


您想要一个barplot(x vs y 值)而不是histplot绘制数据集分布的 a:

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

输出:

seaborn 条形图


推荐阅读