user3700386
user3700386

Reputation: 89

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()

Upvotes: 3

Views: 3745

Answers (1)

Paul H
Paul H

Reputation: 68256

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

Upvotes: 2

Related Questions