matplotlib hist() autocropping range
53,331
Actually, it works if you specify with range
an interval shorter than [-100, 100]
. For example, this work :
import numpy as np
import matplotlib.pyplot as plt
x = np.random.uniform(-100, 100, 1000)
plt.hist(x, bins=30, range=(-50, 50))
plt.show()
If you want to plot the histogram on a range larger than [x.min(), x.max()]
you can change xlim
propertie of the plot.
import numpy as np
import matplotlib.pyplot as plt
x = np.random.uniform(-100, 100, 1000)
plt.hist(x, bins=30)
plt.xlim(-500, 500)
plt.show()
![Keith](https://i.stack.imgur.com/U5nws.jpg?s=256&g=1)
Comments
-
Keith almost 2 years
I am trying to make a histgram over a specific range but the matplotlib.pyplot.hist() function keeps cropping the range to the bins with entries in them. A toy example:
import numpy as np import matplotlib.pyplot as plt x = np.random.uniform(-100,100,1000) nbins = 100 xmin = -500 xmax = 500 fig = plt.figure(); ax = fig.add_subplot(1, 1, 1) ax.hist(x, bins=nbins,range=[xmin,xmax]) plt.show()
Gives a plot with a range [-100,100]. Why is the range not [-500,500] as specified?
(I am using the Enthought Canopy 1.4 and sorry but I do not have a high enough rep to post an image of the plot.)
-
Keith about 10 yearsOK Great. Or in a more OO way I can use ax.hist(x, bins=nbins,range=[xmin,xmax]) ax.axis(xmin=xmin,xmax=xmax)
-
Ger about 10 yearsYes. Or
ax.set_xlim(xmin=xmin, xmax=xmax)
.