代码之家  ›  专栏  ›  技术社区  ›  Unknown

python:柱状图-创建相等的箱/轴

  •  0
  • Unknown  · 技术社区  · 6 年前

    注意,此问题涉及:[ 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]

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  0
  •   Thomas Kühn    6 年前

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

    整个过程如下:

    result of the above code

    希望这有帮助。