使用数据框的两列创建一个 seaborn 直方图

Create a seaborn histogram with two columns of a 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

我想使用 gr_age 列作为 X 轴,使用 weighted_cost 作为 Y 轴。这是我正在寻找 Excel:

的示例

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

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

感谢您的想法!

您想要 barplot(x 与 y 值)而不是 histplot 来绘制数据集的分布:

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

输出: