我试图修复python如何绘制我的数据。 说:

x = [0,5,9,10,15]
y = [0,1,2,3,4]

matplotlib.pyplot.plot(x,y)
matplotlib.pyplot.show()

x轴的刻度以5为间隔绘制。有没有办法让它显示1的间隔?


当前回答

这有点俗气,但到目前为止,这是我找到的最干净/最容易理解的例子。这句话来自这里SO的回答:

最干净的方法隐藏每n勾标签在matplotlib色条?

for label in ax.get_xticklabels()[::2]:
    label.set_visible(False)

然后你可以循环标签,根据你想要的密度将它们设置为可见或不可见。

编辑:注意,有时matplotlib设置标签== ",所以它可能看起来像一个标签不存在,而实际上它是,只是没有显示任何东西。为了确保你在循环实际可见的标签,你可以尝试:

visible_labels = [lab for lab in ax.get_xticklabels() if lab.get_visible() is True and lab.get_text() != '']
plt.setp(visible_labels[::2], visible=False)

其他回答

xmarks=[i for i in range(1,length+1,1)]

plt.xticks(xmarks)

这对我很有效

如果你想要[1,5](1到5包括在内)之间的刻度,那么替换

length = 5

如果你只是想把间距设置为一个简单的一行和最小的样板:

plt.gca().xaxis.set_major_locator(plt.MultipleLocator(1))

对小蜱虫也很有效:

plt.gca().xaxis.set_minor_locator(plt.MultipleLocator(1))

有点满口,但很紧凑

另一种方法是设置轴定位器:

import matplotlib.ticker as plticker

loc = plticker.MultipleLocator(base=1.0) # this locator puts ticks at regular intervals
ax.xaxis.set_major_locator(loc)

根据您的需要,有几种不同类型的定位器。

下面是一个完整的例子:

import matplotlib.pyplot as plt
import matplotlib.ticker as plticker

x = [0,5,9,10,15]
y = [0,1,2,3,4]
fig, ax = plt.subplots()
ax.plot(x,y)
loc = plticker.MultipleLocator(base=1.0) # this locator puts ticks at regular intervals
ax.xaxis.set_major_locator(loc)
plt.show()

因为上面的解决方案都不适用于我的用例,所以在这里我提供了一个使用None的解决方案,它可以适用于各种各样的场景。

下面是一个示例代码,它在X轴和Y轴上都产生了混乱的刻度。

# Note the super cluttered ticks on both X and Y axis.

# inputs
x = np.arange(1, 101)
y = x * np.log(x) 

fig = plt.figure()     # create figure
ax = fig.add_subplot(111)
ax.plot(x, y)
ax.set_xticks(x)        # set xtick values
ax.set_yticks(y)        # set ytick values

plt.show()

现在,我们用一个新的图来清理混乱,它只在x和y轴上显示一组稀疏的值作为刻度。

# inputs
x = np.arange(1, 101)
y = x * np.log(x)

fig = plt.figure()       # create figure
ax = fig.add_subplot(111)
ax.plot(x, y)

ax.set_xticks(x)
ax.set_yticks(y)

# which values need to be shown?
# here, we show every third value from `x` and `y`
show_every = 3

sparse_xticks = [None] * x.shape[0]
sparse_xticks[::show_every] = x[::show_every]

sparse_yticks = [None] * y.shape[0]
sparse_yticks[::show_every] = y[::show_every]

ax.set_xticklabels(sparse_xticks, fontsize=6)   # set sparse xtick values
ax.set_yticklabels(sparse_yticks, fontsize=6)   # set sparse ytick values

plt.show()

根据使用情况,可以简单地修改show_every并使用它为X或Y或两个轴取样刻度值,从而适应上面的代码。

如果这种基于步长的解决方案不适合,那么还可以以不规则的间隔填充sparse_xticks或sparse_yticks的值(如果需要的话)。

这是一个老话题了,但我偶尔会遇到这个问题,然后做了这个功能。非常方便:

import matplotlib.pyplot as pp
import numpy as np

def resadjust(ax, xres=None, yres=None):
    """
    Send in an axis and I fix the resolution as desired.
    """

    if xres:
        start, stop = ax.get_xlim()
        ticks = np.arange(start, stop + xres, xres)
        ax.set_xticks(ticks)
    if yres:
        start, stop = ax.get_ylim()
        ticks = np.arange(start, stop + yres, yres)
        ax.set_yticks(ticks)

像这样控制刻度的一个警告是,一个人不再享受在添加一行后最大刻度的交互式自动更新。然后做

gca().set_ylim(top=new_top) # for example

并再次运行resadjust函数。