5

How would I go about adding a horizontal line to this histogram? I have tried the usual methods (that work with bar graphs) however the line does not plot due to the y axis being in terms of percentage (I assume). I am trying to plot a line that exists from hour (x cord) 11 until hour (x cord) 22 in order to portray a change in experimental conditions. Anyone know what to do? Thanks!

probability_list = np.array(probability_list, dtype=float)
x = 24
f, ax = plt.subplots(1, 1, figsize=(10, 5))
heights, bins = np.histogram(probability_list, bins=len(list(set(probability_list))))
percent = [i / len(dayammount) * 100 for i in heights]
ax.bar(bins[:-1], percent, width=.025, align="edge")
vals = ax.get_yticks()
ax.set_yticklabels(['%1.2f%%' % i for i in vals])
plt.xlim(xmin=0, xmax=24)
plt.xticks(range(0, 25))
plt.xlabel('Time (Hours)')
plt.ylabel('Probability of Sound (%)')
plt.show()
tdy
  • 36,675
  • 19
  • 86
  • 83

1 Answers1

4

however the line does not plot due to the y axis being in terms of percentage (I assume)

In general it shouldn't matter that you converted heights into percents -- matplotlib just sees a list of values either way.

It's not clear what you've tried so far, but here are a couple options:

  • Use hlines to plot a bounded horizontal line, e.g. at y=2 from xmin=11 to xmax=22:

    ax.hlines(y=2, xmin=11, xmax=22, colors='k', linestyles='--')
    
  • Or use plain plot:

    ax.plot([11, 22], [2, 2], 'k--')
    

I don't have your original data, but this is the output from some simulated data:

histogram with hlines

tdy
  • 36,675
  • 19
  • 86
  • 83