【问题标题】:Move last histogram tick label to left of bar将最后一个直方图刻度标签移动到条形图的左侧
【发布时间】:2017-09-26 00:10:58
【问题描述】:

我有一个情节,其中第 5 条错误地放置在第 4 条旁边。我应该改变什么? 我的small_ax_0 pandas 数据框如下所示:

INDEX    0
0      1  5.0
1  10001  4.0
2  20001  5.0
3  30001  5.0
4  40001  5.0
5  50001  4.0
6  60001  1.0
7  70001  4.0
8  80001  0.0
9  90001  4.0

这是我的代码:

plt.hist(small_ax_0[0])
plt.tick_params(axis='both', which='major', labelsize=100)
plt.tick_params(axis='both', which='minor', labelsize=100) 
plt.xlabel('Position', fontsize=100)
plt.ylabel('Frequency', fontsize=100)
plt.title('My Plot',  fontsize = 150) ##
plt.grid(b=True, which='major', color='grey', linestyle='dotted')
plt.xticks( rotation = 45)
plt.show()

【问题讨论】:

    标签: python pandas histogram


    【解决方案1】:

    熊猫visualization

    df['0'].value_counts().sort_index().plot(kind='bar')
    

    【讨论】:

      【解决方案2】:

      默认情况下,hist 返回 10 个 bin,沿数据范围等距分布。所以在这种情况下,数据范围从 0 到 5,bin 之间的间距为 0.5。如果您只想绘制每个数字的出现次数,我建议使用np.unique() 并使用bar 绘图:

      import numpy as np
      nums, freq = np.unique(small_ax_0[0], return_counts=True)
      plt.bar(nums, freq)
      

      你会得到一个数字,其中条形围绕每个数字居中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-23
        • 1970-01-01
        • 2021-10-29
        • 1970-01-01
        • 1970-01-01
        • 2015-03-22
        相关资源
        最近更新 更多