【问题标题】:Fail to draw a Histogram in sns.histplot在 sns.histplot 中绘制直方图失败
【发布时间】:2021-11-10 11:09:14
【问题描述】:
data = {'year':  ['2010', '2011','2012','2013','2014','2015','2016','2017','2018','2019','2020','2021'],
        'points': [2260, 1499, 1834,3712,6830,7139.0,8085,6928,6903,8631,5476,1271]}
df = pd.DataFrame(data)
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="darkgrid")
sns.histplot(data=df['points'],x=df['year'],color="blue")
plt.show()

这是我的代码,但图表看起来不像我希望的那样。我希望它看起来像 x 轴被标记为年份,而 y 轴显示不同的点高度。如何解决?

【问题讨论】:

    标签: python histogram


    【解决方案1】:

    我认为你使用了错误的情节类型,barplot 怎么样?

    sns.set(style="darkgrid")
    ax = sns.barplot(x="year", y="points", data=data)
    plt.show()
    

    【讨论】:

      【解决方案2】:

      为了能够计算 bin,您需要一个数值为 x:

      sns.histplot(data=df['points'], x=df['year'].astype(int), color="blue")
      

      或者您可能根本不想要一个 histplot(显示分布),而是一个 barplot:

      df.plot.bar(x='year', y='points')
      

      【讨论】:

        猜你喜欢
        • 2011-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-13
        相关资源
        最近更新 更多