Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intervals on x axis

I want to change the size of the of the intervals between numbers. The x axis obviously goes from 10 to 26. But I want every whole number to be displayed: 10, 11, 12, 13 etc... I also want bins to have a width of .5 so that I can have a bin from 10.5 to 11 or 24 to 24.5 etc...because otherwise, python outputs the histogram with the bins random and undetermined. Here's what I have:

import random
import numpy
from matplotlib import pyplot
import numpy as np

data = np.genfromtxt('result.csv',delimiter=',',skip_header=1, dtype=float)

magg=[row[5] for row in data]
magr=[row[6] for row in data]

bins = numpy.linspace(10, 26)

pyplot.hist(magg, bins, alpha=0.5, color='g', label='mag of g')
pyplot.hist(magr, bins, alpha=0.5, color='r', label='mag of r')
pyplot.legend(loc='upper left')
pyplot.show()
like image 988
user3700386 Avatar asked Jan 21 '26 07:01

user3700386


1 Answers

Use an axes locator, in particular, MultipleLocator. Building of your example, it becomes this:

import matplotlib.pyplot as plt
import numpy as np

x = np.random.random_integers(low=10, high=27, size=37)

bins = np.linspace(10, 26)

fig, ax = plt.subplots()
hist = ax.hist(x, bins, alpha=0.5, color='g', label='mag of g')
ax.xaxis.set_major_locator(plt.MultipleLocator(1))

enter image description here

like image 168
Paul H Avatar answered Jan 22 '26 21:01

Paul H



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!