【问题标题】:Create a seaborn histogram with two columns of a dataframe创建包含两列数据框的 seaborn 直方图
【发布时间】:2021-12-10 05:55:16
【问题描述】:

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

   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()

感谢您的想法!

【问题讨论】:

    标签: python pandas dataframe matplotlib seaborn


    【解决方案1】:

    您需要 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')
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-10
      • 2018-08-31
      • 2021-08-26
      • 2021-04-06
      • 1970-01-01
      • 2015-12-31
      • 2016-11-26
      • 2018-12-04
      相关资源
      最近更新 更多