如何更改matplotlib中的刻度线之间的间距? [英] How to change spacing between ticks in matplotlib?

查看:2216
本文介绍了如何更改matplotlib中的刻度线之间的间距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

结果图如下:

如您所见,X轴是一团糟,因为刻度线标签之间的间距太小甚至重叠.

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.

我想在每个刻度标签之间自动创建恒定的空间,而不管有多少刻度.因此,我想增加各个刻度之间的间隔,从而可能增加绘图的长度".

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.

到目前为止,我发现的全部都是关于 tick频率(我已经可以做到)和

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

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

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.

我该怎么做才能增加刻度线之间的间隔?我可以得到很长的图像.

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

推荐答案

刻度标签之间的间距专门由轴上刻度之间的间距确定.因此,在给定的刻度标签之间获取更多空间的唯一方法是使轴更大.

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.

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

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

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

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中的刻度线之间的间距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆