4

How can I remove the bottom line that closes the path of a step histogram?

enter image description here

enter image description here

import numpy as np
import matplotlib.pyplot as plt

mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
fig = plt.figure()
ax = fig.add_subplot(111)
n, bins, patches = ax.hist(x, 50, normed=1, histtype='step')
plt.ylim(-.005, plt.ylim()[1])
plt.show()

UPDATE: reported and now fixed:

https://github.com/matplotlib/matplotlib/pull/2113

ndawe
  • 338
  • 3
  • 14

1 Answers1

2

The easiest way is to just plot it your self:

import numpy as np
import matplotlib.pyplot as plt

mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
fig = plt.figure()
ax = fig.add_subplot(111)
bins, edges = np.histogram(x, 50, normed=1)
ax.step(edges[:-1], bins, where='post')
plt.ylim(-.005, plt.ylim()[1])
plt.show()

See doc or Step function in matplotlib to understand where=post. You need to chop the last entry off edges because histogram returns [left_edege_0, left_edge_1, ..., left_edge_(n-1), right_edge_(n-1)] (see doc)

This has been deemed a regression, and will be fixed in at least 1.3. Relevant PR: https://github.com/matplotlib/matplotlib/pull/2113

Community
  • 1
  • 1
tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • Thanks, works very well! Would mpl devs ever consider adding an option to turn off the bottom line in pyplot.hist? As @tom10 mentioned above this was the previous behaviour. – ndawe Jun 01 '13 at 09:41
  • 2
    @ndawe There was a bunch of work done on histograms recently, this might be a regression. I suspect this is a consequence of the work ot add filled step plots. You should create an issue on github. – tacaswell Jun 01 '13 at 15:03