Python 如何在matplotlib中更改刻度之间的间距?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44863375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:32:31  来源:igfitidea点击:

How to change spacing between ticks in matplotlib?

pythonmatplotlibplot

提问by ForceBru

I want to plot a graph with a lot of ticks on the X axis using the following code:

我想使用以下代码在 X 轴上绘制一个带有很多刻度的图形:

import pylab

N = 100
data = pylab.np.linspace(0, N, N)

pylab.plot(data)

pylab.xticks(range(N)) # add loads of ticks
pylab.grid()
pylab.tight_layout()
pylab.show()

pylab.close()

The resulting plot looks like this:

结果图如下所示:

plot

阴谋

As you can see, the X axis is a mess because the tick labels are plotted with too few space between them or even overlap.

正如您所看到的,X 轴是一团糟,因为刻度标签之间的空间太小甚至重叠

I would like to create constant space between each tick label automatically, no matter how many ticks there are. So, I'd like to increase the space between individual ticks, thus potentially increasing the 'length' of a plot.

我想自动在每个刻度标签之间创建恒定空间,无论有多少刻度。因此,我想增加单个刻度之间的空间,从而可能增加绘图的“长度”。

Note that the tick labels may be strings of variable length.

请注意,刻度标签可能是可变长度的字符串

What I have found so far is all about spacing between the axis and labels(which is not what I want), tick frequency(which I can already do) and tick parameters(which don't seem to have any options for spacing).

到目前为止,我发现的都是关于轴和标签之间的间距(这不是我想要的)、刻度频率(我已经可以做到)和刻度参数(似乎没有任何间距选项)。

I can change the size of a figure manually with matplotlib.pyplot.figure(figsize=(a, b)), but that would require knowledge of the default spacing between ticks (there's none, as far as I can tell) and the greatest width (in inches) of a tick label, which I have no clue how to measure, so this is not an option, to my mind.

我可以使用 手动更改图形的大小matplotlib.pyplot.figure(figsize=(a, b)),但这需要了解刻度之间的默认间距(据我所知没有)和刻度标签的最大宽度(以英寸为单位),我没有知道如何测量,所以在我看来这不是一个选择。

What can I do to increase spacing between ticks? I'm OK with getting a pretty lengthy image.

我能做些什么来增加刻度之间的间距?我可以得到一个很长的图像。

采纳答案by ImportanceOfBeingErnest

The spacing between ticklabels is exclusively determined by the space between ticks on the axes. Therefore the only way to obtain more space between given ticklabels is to make the axes larger.

刻度标签之间的间距完全由轴上刻度之间的间距决定。因此,在给定刻度标签之间获得更多空间的唯一方法是使轴更大。

In order to determine the space needed for the labels not to overlap, one may find out the largest label and multiply its length by the number of ticklabels. One may then adapt the margin around the axes and set the calculated size as a new figure size.

为了确定标签不重叠所需的空间,可以找出最大的标签并将其长度乘以刻度标签的数量。然后可以调整轴周围的边距并将计算的大小设置为新的图形大小。

import numpy as np
import matplotlib.pyplot as plt

N = 150
data = np.linspace(0, N, N)

plt.plot(data)

plt.xticks(range(N)) # add loads of ticks
plt.grid()

plt.gca().margins(x=0)
plt.gcf().canvas.draw()
tl = plt.gca().get_xticklabels()
maxsize = max([t.get_window_extent().width for t in tl])
m = 0.2 # inch margin
s = maxsize/plt.gcf().dpi*N+2*m
margin = m/plt.gcf().get_size_inches()[0]

plt.gcf().subplots_adjust(left=margin, right=1.-margin)
plt.gcf().set_size_inches(s, plt.gcf().get_size_inches()[1])

plt.savefig(__file__+".png")
plt.show()

enter image description here

enter image description here

Note that if the figure shown in the plotting window is larger than the screen, it will be shrunk again, so the resized figure is only shown in its new size when saved. Or, one may choose to incorporate it in some window with scrollbars as shown in this question: Scrollbar on Matplotlib showing page

请注意,如果绘图窗口中显示的图形大于屏幕,它将再次缩小,因此调整大小的图形在保存时仅以其新大小显示。或者,可以选择将其合并到某个带有滚动条的窗口中,如以下问题所示:Matplotlib 显示页面上的滚动条

回答by seralouk

You could use the following to rotate the labels and make the font smaller:

您可以使用以下方法旋转标签并使字体变小:

ax.set_xticklabels(rotation = (45), fontsize = 10, va='bottom', ha='left')