Python matplotlib限制为整数刻度位置

Python matplotlib限制为整数刻度位置,python,matplotlib,Python,Matplotlib,我经常想做一个计数条形图。如果计数较低,我通常会得到非整数的主刻度和/或次刻度位置。我怎样才能防止这种情况?当数据被计数时,在1.5处打勾是没有意义的 这是我第一次尝试: import pylab pylab.figure() ax = pylab.subplot(2, 2, 1) pylab.bar(range(1,4), range(1,4), align='center') major_tick_locs = ax.yaxis.get_majorticklocs() if len(majo

我经常想做一个计数条形图。如果计数较低,我通常会得到非整数的主刻度和/或次刻度位置。我怎样才能防止这种情况?当数据被计数时,在1.5处打勾是没有意义的

这是我第一次尝试:

import pylab
pylab.figure()
ax = pylab.subplot(2, 2, 1)
pylab.bar(range(1,4), range(1,4), align='center')
major_tick_locs = ax.yaxis.get_majorticklocs()
if len(major_tick_locs) < 2 or major_tick_locs[1] - major_tick_locs[0] < 1:
    ax.yaxis.set_major_locator(pylab.MultipleLocator(1))
minor_tick_locs = ax.yaxis.get_minorticklocs()
if len(minor_tick_locs) < 2 or minor_tick_locs[1] - minor_tick_locs[0] < 1:
    ax.yaxis.set_minor_locator(pylab.MultipleLocator(1))
导入pylab
pylab.图()
ax=pylab.子批次(2,2,1)
pylab.bar(范围(1,4),范围(1,4),对齐并居中)
major\u tick\u locs=ax.yaxis.get\u majorticklocs()
如果len(主刻度盘)小于2或主刻度盘[1]-主刻度盘[0]<1:
ax.yaxis.set_major_定位器(pylab.MultipleLocator(1))
minor\u tick\u locs=ax.yaxis.get\u minorticklocs()
如果len(小刻度盘)小于2或小刻度盘[1]-小刻度盘[0]<1:
ax.yaxis.set_minor_定位器(pylab.MultipleLocator(1))
当计数很小时,它工作正常,但当计数很大时,我会得到许多小刻度:

import pylab
ax = pylab.subplot(2, 2, 2)
pylab.bar(range(1,4), range(100,400,100), align='center')
major_tick_locs = ax.yaxis.get_majorticklocs()
if len(major_tick_locs) < 2 or major_tick_locs[1] - major_tick_locs[0] < 1:
    ax.yaxis.set_major_locator(pylab.MultipleLocator(1))
minor_tick_locs = ax.yaxis.get_minorticklocs()
if len(minor_tick_locs) < 2 or minor_tick_locs[1] - minor_tick_locs[0] < 1:
    ax.yaxis.set_minor_locator(pylab.MultipleLocator(1))
导入pylab
ax=pylab.子批次(2,2,2)
pylab.bar(范围(1,4),范围(100400100),对齐并居中)
major\u tick\u locs=ax.yaxis.get\u majorticklocs()
如果len(主刻度盘)小于2或主刻度盘[1]-主刻度盘[0]<1:
ax.yaxis.set_major_定位器(pylab.MultipleLocator(1))
minor\u tick\u locs=ax.yaxis.get\u minorticklocs()
如果len(小刻度盘)小于2或小刻度盘[1]-小刻度盘[0]<1:
ax.yaxis.set_minor_定位器(pylab.MultipleLocator(1))

如何从第一个示例中获得所需的小计数行为,同时避免在第二个示例中发生的情况?

我认为我可以忽略小的滴答声。我将尝试一下,看看它是否在所有用例中都适用:

def ticks_restrict_to_integer(axis):
    """Restrict the ticks on the given axis to be at least integer,
    that is no half ticks at 1.5 for example.
    """
    from matplotlib.ticker import MultipleLocator
    major_tick_locs = axis.get_majorticklocs()
    if len(major_tick_locs) < 2 or major_tick_locs[1] - major_tick_locs[0] < 1:
        axis.set_major_locator(MultipleLocator(1))

def _test_restrict_to_integer():
    pylab.figure()
    ax = pylab.subplot(1, 2, 1)
    pylab.bar(range(1,4), range(1,4), align='center')
    ticks_restrict_to_integer(ax.xaxis)
    ticks_restrict_to_integer(ax.yaxis)

    ax = pylab.subplot(1, 2, 2)
    pylab.bar(range(1,4), range(100,400,100), align='center')
    ticks_restrict_to_integer(ax.xaxis)
    ticks_restrict_to_integer(ax.yaxis)

_test_restrict_to_integer()
pylab.show()
def ticks_restrict_to_integer(轴):
“”“将给定轴上的刻度限制为至少为整数,
例如,在1.5时没有半个刻度。
"""
从matplotlib.ticker导入MultipleLocator
主刻度线=轴。获取主刻度线()
如果len(主刻度盘)小于2或主刻度盘[1]-主刻度盘[0]<1:
轴设置主定位器(多路传送器(1))
def_test_restrict_to_integer():
pylab.图()
ax=pylab.子批次(1,2,1)
pylab.bar(范围(1,4),范围(1,4),对齐并居中)
刻度限制为整数(ax.xaxis)
刻度限制为整数(ax.yaxis)
ax=pylab.子批次(1,2,2)
pylab.bar(范围(1,4),范围(100400100),对齐并居中)
刻度限制为整数(ax.xaxis)
刻度限制为整数(ax.yaxis)
_测试\u将\u限制为\u整数()
pylab.show()

您可以使用
MaxNLocator
方法,如下所示:

    from pylab import MaxNLocator

    ya = axes.get_yaxis()
    ya.set_major_locator(MaxNLocator(integer=True))

在我的代码中工作。
只要使用
align
可选参数,
xticks
就能发挥神奇的作用。

我在绘制显示分数计数的直方图时遇到了类似的问题。以下是我解决问题的方法:

plt.hist(x=[Dataset being counted])

# Get your current y-ticks (loc is an array of your current y-tick elements)
loc, labels = plt.yticks()

# This sets your y-ticks to the specified range at whole number intervals
plt.yticks(np.arange(0, max(loc), step=1))

我认为
pylab.MaxNLocator()
是更好的表示法。或者,如果您不导入pylab,
matplotlib.ticker.MaxNLocator()
。(来自)什么是派拉布?是否将matplot lib例如导入matplotlib.pyplot作为plt?另一种方法是
轴.yaxis.set_major\u定位器(MaxNLocator(integer=True))
。这与pylab import MaxNLocator ya=axes.get_yaxis()ya.set_major\u定位器(MaxNLocator=True))的
有何不同
?我认为最好能弄清楚如何强制小刻度也只使用整数。
 xticks(range(1,40),range(1,40))
plt.hist(x=[Dataset being counted])

# Get your current y-ticks (loc is an array of your current y-tick elements)
loc, labels = plt.yticks()

# This sets your y-ticks to the specified range at whole number intervals
plt.yticks(np.arange(0, max(loc), step=1))