Python 为 matplotlib 中的所有子图设置相同的轴限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31006971/
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-19 09:19:18  来源:igfitidea点击:

Setting the same axis limits for all subplots in matplotlib

pythonmatplotlib

提问by MrArsGravis

This problem seems simple enough, but I can't find a pythonic way to solve it. I have several (four) subplots that are supposed to have the same xlimand ylim. Iterating over all subplots à la

这个问题看起来很简单,但我找不到解决它的pythonic方法。我有几个(四个)子图应该具有相同的xlimylim。迭代所有子图 à la

f, axarr = plt.subplots(4)
for x in range(n):
    axarr[x].set_xlim(xval1, xval2)
    axarr[x].set_ylim(yval1, yval2)

isn't the nicest way of doing things, especially for 2x2 subplots – which is what I'm actually dealing with. I'm looking for something like plt.all_set_xlim(xval1, xval2).

不是最好的做事方式,尤其是对于 2x2 子图——这正是我真正要处理的。我正在寻找类似的东西plt.all_set_xlim(xval1, xval2)

Note that I don't want anything else to change (ticks and labels should be controlled separately).

请注意,我不想更改任何其他内容(刻度和标签应单独控制)。

EDIT: I'm using the plt.subplots(2, 2)wrapper. Following dienzs answer, I tried plt.subplots(2, 2,sharex=True, sharey=True)– almost right, but now the ticks are gone except for the left and bottom row.

编辑:我正在使用plt.subplots(2, 2)包装器。按照 dienzs 的回答,我尝试了plt.subplots(2, 2,sharex=True, sharey=True)- 几乎是正确的,但是现在除了左行和底行之外,刻度线都消失了。

回答by Daniel Lenz

You could use shared axeswhich will share the x- and/or y-limits, but allow to customise the axes in any other way.

您可以使用共享轴来共享 x 和/或 y 限制,但允许以任何其他方式自定义轴。

回答by xiaozhu123

You can try this.

你可以试试这个。

#set same x,y limits for all subplots
fig, ax = plt.subplots(2,3)
for (m,n), subplot in numpy.ndenumerate(ax):
    subplot.set_xlim(xval1,xval2)
    subplot.set_ylim(yval1,yval2)

回答by BenS

This is not at all elegant, but it worked for me...

这一点都不优雅,但它对我有用......

fig, axes = plt.subplots(6, 3, sharex=True)
axes[0, 0].set_xlim(right=10000) # sharex=True propagates it to all plots
for i in range(6):
    for j in range(3):
        axes[i, j].plot('Seconds', df.columns[2+3*i+j], data=df)  # your plot instructions
plt.subplots_adjust(wspace=.5, hspace=0.2)

回答by AZarketa

Set the xlimand ylimproperties on the Artist object by matplotlib.pyplot.setp()https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.pyplot.setp.html

通过https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.pyplot.setp.html在 Artist 对象上设置xlimylim属性matplotlib.pyplot.setp()

# Importing matplotlib.pyplot package.
import matplotlib.pyplot as plt

# Assigning 'fig', 'ax' variables.
fig, ax = plt.subplots(2, 2)

# Defining custom 'xlim' and 'ylim' values.
custom_xlim = (0, 100)
custom_ylim = (-100, 100)

# Setting the values for all axes.
plt.setp(ax, xlim=custom_xlim, ylim=custom_ylim)

回答by Tajni

If you have multiple subplots, i.e.

如果你有多个子图,即

fig, ax = plt.subplots(4, 2)

You can use it. It gets limits of y ax from first plot. If you want other subplot just change index of ax[0,0].

你可以使用它。它从第一个图中获得 y 轴的限制。如果您想要其他子图,只需更改ax[0,0].

plt.setp(ax, ylim=ax[0,0].get_ylim())