Python:直方图 - 创建不相等 bins/axis

Python: Histogram - Create uequal bins/axis

注意,此问题与:[Defining bin width/x-axis scale in Matplotlib histogram有关。 我有一个看起来像这样的数据

 Time               Pressure
    1/1/2017 0:00       5.8253
    ...                     ...
    3/1/2017 0:10       4.2785
    4/1/2017 0:20       5.20041
    5/1/2017 0:30       4.40774
    6/1/2017 0:40       4.03228
    7/1/2017 0:50       5.011924
    12/1/2017 1:00      3.9309888

我想绘制一个直方图,它看起来像 this.The 间隔就像- [0-40,60,65,70,75,80]

您可以使用 numpy.histogram() 生成直方图,然后使用 Axes.bar() 绘制它。然后可以使用 Axes.set_ticklabels() 调整刻度。举个例子:

import numpy as np
from matplotlib import pyplot as plt

#some fake data:
data = np.random.normal(70,20,[100])

#the histogram
dist, edges = np.histogram(data,bins=[0,40,60,65,70,75,80])

#the plot    
fig,ax = plt.subplots()    
ax.bar(
    np.arange(dist.shape[0]), dist, width=1, align = 'edge',
    color = [1,0,0,0.5], edgecolor=[1,0,0,1], lw = 2,
    )
ax.set_xticks(np.arange(edges.shape[0]))
ax.set_xticklabels(edges)

plt.show()

整个事情看起来像这样:

希望这对您有所帮助。