x轴上的间隔

时间:2014-06-02 21:19:34

标签: python numpy histogram axis

我想更改数字间隔的大小。 x轴显然从10到26.但是我想要显示每个整数:10,11,12,13等...... 我也希望垃圾箱的宽度为.5,这样我就可以有一个从10.5到11或24到24.5之间的垃圾箱等等......因为否则,python会随机输出柱状图并且未确定。 这就是我所拥有的:

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

1 个答案:

答案 0 :(得分:1)

使用轴定位器,尤其是MultipleLocator。建立你的例子,它变成了这个:

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