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()
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.
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.
plt.hist(hmag, 30, range=[6.5, 12.5], facecolor='gray', align='mid')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With