0

i'm trying to create simple hist plot, using plt.hist. I encountered a strange problem, as you can see in the figure - the bins just overlap each other. enter image description here

Here is my code:

    intervals = np.arange(0.5,max(data)+0.5, .5)
    data = np.array(data)

    # build labels list
    labels = [ str(intervals[i])+'-'+str(intervals[i+1]) for i in range(len(intervals)-1) ]
    labels.append(str(max(intervals))+'-'+str(max(intervals)+.5))
    
    # plot
    fig, ax = plt.subplots(figsize=(12, 9))
    plt.hist(x=data, bins=intervals, width=1)
    ax.set_xticks(range(len(intervals)))
    ax.set_xticklabels(labels=labels, rotation=45, fontsize=12)
    ax.set_title("Max wind speed ~24hr before dust emission ("+loc+")", fontsize=18)
    ax.set_xlabel('Wind Speed [m/s]', fontsize=14)
    ax.set_ylabel('No. of Events', fontsize=14)
    plt.grid(True)
    plt.tight_layout()
    plt.savefig('th_hist'+loc+'.png')
    plt.show()
    

`

I tried to change the axis size, and also played with the width value.

Doron bs
  • 11
  • 1
  • I think your width should be at most `width_max=intervals[1] - intervals[0]`. Try letting `width=0.75*width_max`. – dermen Nov 14 '22 at 19:59
  • Thanks for the reply. I tried to play with the width already, the problem is that the xticks and the values are not coordinated - although they both have the same size... – Doron bs Nov 15 '22 at 10:24
  • Perhaps you can update your question with some real data such that one can produce the exact figure you attached ? One idea is to use [`np.histogram`](https://numpy.org/doc/stable/reference/generated/numpy.histogram.html), and then feed the output of that into [`plt.bar`](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.bar.html) which allows more control over the display. – dermen Nov 15 '22 at 22:20

0 Answers0