cambiar el rango x predeterminado en el histograma matplotlib
Frecuentes
Visto 161,290 veces
49
I would like to change the default x range for the histogram plot. The range of the data is from 7 to 12. However, by default the histogram starts right at 7 and ends at 13. I want it to start at 6.5 and end at 12.5. However, the ticks should go from 7 to 12.How do I do it?
import asciitable
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import pylab
from pylab import xticks
data = asciitable.read(file)
hmag = data['col8']
visits = data['col14']
origin = data['col13']
n, bins, patches = plt.hist(hmag, 30, facecolor='gray', align='mid')
xticks(range(7,13))
pylab.rc("axes", linewidth=8.0)
pylab.rc("lines", markeredgewidth=2.0)
plt.xlabel('H mag', fontsize=14)
plt.ylabel('# of targets', fontsize=14)
pylab.xticks(fontsize=15)
pylab.yticks(fontsize=15)
plt.grid(True)
plt.savefig('hmag_histogram.eps', facecolor='w', edgecolor='w', format='eps')
plt.show()
3 Respuestas
79
plt.hist(hmag, 30, range=[6.5, 12.5], facecolor='gray', align='mid')
Respondido el 01 de diciembre de 17 a las 09:12
10
import matplotlib.pyplot as plt
...
plt.xlim(xmin=6.5, xmax = 12.5)
Respondido 29 ago 17, 17:08
1
the following code is for making the same y axis limit on two subplots
f ,ax = plt.subplots(1,2,figsize = (30, 13),gridspec_kw={'width_ratios': [5, 1]})
df.plot(ax = ax[0], linewidth = 2.5)
ylim = [lower_limit,upper_limit]
ax[0].set_ylim(ylim)
ax[1].hist(data,normed =1, bins = num_bin, color = 'yellow' ,alpha = 1)
ax[1].set_ylim(ylim)
just a reminder, plt.hist(range=[low, high])
the histogram auto crops the range if the specified range is larger than the max&min of the data points. So if you want to specify the y-axis range number, i prefer to use set_ylim
Respondido 11 Jul 19, 15:07
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas python matplotlib histogram xrange or haz tu propia pregunta.
Thanks for your reply. However, doing so I get the following error: >> n, bins, patches = plt.hist(hmag, range=[6.5, 12.5], 30, facecolor='gray', align='mid') >>SyntaxError: non-keyword arg after keyword arg - Rohit
You have to use the same order as I used above. Putting
30
after the range keyword will lead to aSyntaxerror.
- james@aging_gorrila, the yrange is just a property of the plot and doesn't require recomputing the histogram. After you do the plot, you can just call
ylim(a, b)
. - jamesIs there a way to set this automatically? Say, the range goes from xmin*-1.1 to xmax*1.1? - user989762
@user989762 you can set
range=[-xmin * 1.1, xmax * 1.1]
if you want. Just make sure thatxmin
yxmax
are something that makes sense. - james