【问题标题】:Python: Histogram - Create uequal bins/axisPython:直方图 - 创建 uequal bins/axis
【发布时间】:2018-10-31 18:54:10
【问题描述】:

注意,这个问题与:[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

我想绘制一个直方图,使其看起来像这样。间隔类似于 - [0-40,60,65,70,75,80]

【问题讨论】:

标签: python matplotlib histogram clip


【解决方案1】:

您可以使用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()

整个过程是这样的:

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-11
    • 2011-10-30
    • 2022-01-21
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多