Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing default x range in histogram matplotlib

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() 
like image 534
Rohit Avatar asked Aug 25 '12 21:08

Rohit


People also ask

How do I scale the x-axis in Matplotlib?

Import matplotlib. To set x-axis scale to log, use xscale() function and pass log to it. To plot the graph, use plot() function. To set the limits of the x-axis, use xlim() function and pass max and min value to it. To set the limits of the y-axis, use ylim() function and pass top and bottom value to it.

How do you normalize a histogram in Matplotlib?

We can normalize a histogram in Matplotlib using the density keyword argument and setting it to True . By normalizing a histogram, the sum of the bar area equals 1.


1 Answers

plt.hist(hmag, 30, range=[6.5, 12.5], facecolor='gray', align='mid') 
like image 187
tiago Avatar answered Sep 21 '22 02:09

tiago