首页 > 解决方案 > 在直方图的每个子图中标记 bin

问题描述

我有一个数据框,df 有 29 行 x 24 列维度

                     Index             0.0      5.0     34.0 ... 22.0 
          2017-08-03 00:00:00           10        0      10       0
          2017-08-04 00:00:00           20       60    1470      20
          2017-08-05 00:00:00           0        58       0      24
          2017-08-06 00:00:00           0         0     480      24
          2017-09-07 00:00:00           0         0       0      25
                   :                    :         :       :      :
                   :                    :         :       :      :
         2017-09-30 00:00:00

我打算为表示直方图中的一列的每个子图标记箱。我已经能够使用此代码在每个子图中为每列绘制直方图

fig = plt.figure(figsize = (15,20))
ax = fig.gca()
#Initialize the figure
plt.style.use('seaborn-darkgrid')
df.hist(ax = ax) 

但是,每个子图的 bin 标签相距很远,并且 bin 标签没有由 x 轴上的范围明确指定,这很难解释。我已经在 plt.hist 中查看了 Aligning bins to xticks但是当涉及到子图时,它并没有明确解决标记 bins 的问题。任何帮助都会很棒...

我也试过这个,但我得到 ValueError: too many values to unpack (expected 2)

x=[0,40,80,120,160,200,240,280,320]
fig = plt.figure(figsize = (15,20))
ax = fig.gca()
# Initialize the figure
plt.style.use('seaborn-darkgrid')
n,bins= plt.hist(df,bins= x)
#labels & axes
plt.locator_params(nbins=8, axis='x')
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
plt.title('Daily occurrence',fontsize=16)
plt.xlabel('Number of occurrence',fontsize=12)
plt.ylabel('Frequency',fontsize=12)
plt.xticks(x)
plt.xlim(0,320)

标签: pythonpandasmatplotlib

解决方案


推荐阅读