问题:减少地块刻度

我的图表上有太多刻度线,它们彼此碰到。

如何减少刻度线的数量?

例如,我有勾号:

1E-6, 1E-5, 1E-4, ... 1E6, 1E7

我只想要:

1E-5, 1E-3, ... 1E5, 1E7

我曾经尝试过使用LogLocator,但是还无法弄清楚。

I have too many ticks on my graph and they are running into each other.

How can I reduce the number of ticks?

For example, I have ticks:

1E-6, 1E-5, 1E-4, ... 1E6, 1E7

And I only want:

1E-5, 1E-3, ... 1E5, 1E7

I’ve tried playing with the LogLocator, but I haven’t been able to figure this out.


回答 0

另外,如果您想简单地设置刻度线的数量,同时允许matplotlib对其进行定位(目前仅使用MaxNLocator),则有pyplot.locator_params

pyplot.locator_params(nbins=4)

您可以按照以下方法在此方法中指定特定的轴,默认为两者:

# To specify the number of ticks on both or any single axes
pyplot.locator_params(axis='y', nbins=6)
pyplot.locator_params(axis='x', nbins=10)

Alternatively, if you want to simply set the number of ticks while allowing matplotlib to position them (currently only with MaxNLocator), there is pyplot.locator_params,

pyplot.locator_params(nbins=4)

You can specify specific axis in this method as mentioned below, default is both:

# To specify the number of ticks on both or any single axes
pyplot.locator_params(axis='y', nbins=6)
pyplot.locator_params(axis='x', nbins=10)

回答 1

如果仍然有人在搜索结果中获得此页面:

fig, ax = plt.subplots()

plt.plot(...)

every_nth = 4
for n, label in enumerate(ax.xaxis.get_ticklabels()):
    if n % every_nth != 0:
        label.set_visible(False)

If somebody still gets this page in search results:

fig, ax = plt.subplots()

plt.plot(...)

every_nth = 4
for n, label in enumerate(ax.xaxis.get_ticklabels()):
    if n % every_nth != 0:
        label.set_visible(False)

回答 2

要解决刻度的自定义和外观问题,请参见matplotlib网站上的“刻度定位器”指南

ax.xaxis.set_major_locator(plt.MaxNLocator(3))

将x轴上的总刻度数设置为3,并将其均匀地分布在整个轴上。

还有一个很好的教程

To solve the issue of customisation and appearance of the ticks, see the Tick Locators guide on the matplotlib website

ax.xaxis.set_major_locator(plt.MaxNLocator(3))

Would set the total number of ticks in the x-axis to 3, and evenly distribute it across the axis.

There is also a nice tutorial about this


回答 3

set_ticks()轴对象有一个功能。

There’s a set_ticks() function for axis objects.


回答 4

万一有人仍然需要它,并且因为这里没有任何东西真正适合我,我想出了一种非常简单的方法,可以将生成的图的外观保持“原样”,同时将刻度数固定为N:

import numpy as np
import matplotlib.pyplot as plt

f, ax = plt.subplots()
ax.plot(range(100))

ymin, ymax = ax.get_ylim()
ax.set_yticks(np.round(np.linspace(ymin, ymax, N), 2))

in case somebody still needs it, and since nothing here really worked for me, i came up with a very simple way that keeps the appearance of the generated plot “as is” while fixing the number of ticks to exactly N:

import numpy as np
import matplotlib.pyplot as plt

f, ax = plt.subplots()
ax.plot(range(100))

ymin, ymax = ax.get_ylim()
ax.set_yticks(np.round(np.linspace(ymin, ymax, N), 2))

回答 5

@raphael提供的解决方案很简单,也很有帮助。

不过,显示的刻度标签将不是从原始分布采样的值,而是从所返回的数组索引中采样的值np.linspace(ymin, ymax, N)

若要显示与原始刻度标签均匀隔开的N个值,请使用set_yticklabels()方法。这是y轴的代码段,带有整数标签:

import numpy as np
import matplotlib.pyplot as plt

ax = plt.gca()

ymin, ymax = ax.get_ylim()
custom_ticks = np.linspace(ymin, ymax, N, dtype=int)
ax.set_yticks(custom_ticks)
ax.set_yticklabels(custom_ticks)

The solution @raphael gave is straightforward and quite helpful.

Still, the displayed tick labels will not be values sampled from the original distribution but from the indexes of the array returned by np.linspace(ymin, ymax, N).

To display N values evenly spaced from your original tick labels, use the set_yticklabels() method. Here is a snippet for the y axis, with integer labels:

import numpy as np
import matplotlib.pyplot as plt

ax = plt.gca()

ymin, ymax = ax.get_ylim()
custom_ticks = np.linspace(ymin, ymax, N, dtype=int)
ax.set_yticks(custom_ticks)
ax.set_yticklabels(custom_ticks)

回答 6

使用对数刻度时,可以使用以下命令固定主要刻度数

import matplotlib.pyplot as plt

....

plt.locator_params(numticks=12)
plt.show()

设置为的值numticks确定要显示的轴刻度数。

@bgamari的帖子中介绍了该locator_params()功能,但是使用对nticks数刻度时,该参数会引发错误。

When a log scale is used the number of major ticks can be fixed with the following command

import matplotlib.pyplot as plt

....

plt.locator_params(numticks=12)
plt.show()

The value set to numticks determines the number of axis ticks to be displayed.

Credits to @bgamari’s post for introducing the locator_params() function, but the nticks parameter throws an error when a log scale is used.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。