61

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 Answers 3

98
plt.hist(hmag, 30, range=[6.5, 12.5], facecolor='gray', align='mid')
5
  • 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
    Aug 26, 2012 at 17:56
  • 2
    You have to use the same order as I used above. Putting 30 after the range keyword will lead to a Syntaxerror.
    – tiago
    Aug 27, 2012 at 4:40
  • 2
    @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).
    – tiago
    Mar 11, 2013 at 15:57
  • Is there a way to set this automatically? Say, the range goes from xmin*-1.1 to xmax*1.1?
    – user989762
    Aug 21, 2014 at 2:30
  • @user989762 you can set range=[-xmin * 1.1, xmax * 1.1] if you want. Just make sure that xmin and xmax are something that makes sense.
    – tiago
    Aug 21, 2014 at 14:49
12
import matplotlib.pyplot as plt


...


plt.xlim(xmin=6.5, xmax = 12.5)
2
  • This does not keep the number of bins defined in the bins parameter.
    – mountrix
    Aug 31, 2017 at 13:37
  • This works in python3 (assuming your xmin/xmax are large enough) can you give an example where it does not keep the bins count?
    – Erich
    Aug 31, 2017 at 16:41
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.