Python 在 matplotlib pyplot 中设置轴限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3645787/
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
set axis limits in matplotlib pyplot
提问by Curious2learn
I have two subplots in a figure. I want to set the axes of the second subplot such that it has the same limits as the first subplot (which changes depending on the values plotted). Can someone please help me? Here is the code:
我在图中有两个子图。我想设置第二个子图的轴,使其具有与第一个子图相同的限制(根据绘制的值而变化)。有人可以帮帮我吗?这是代码:
import matplotlib.pyplot as plt
plt.figure(1, figsize = (10, 20))
## First subplot: Mean value in each period (mean over replications)
plt.subplot(211, axisbg = 'w')
plt.plot(time,meanVector[0:xMax], color = '#340B8C',
marker = 'x', ms = 4, mec = '#87051B', markevery = (asp,
2*asp))
plt.xticks(numpy.arange(0, T+1, jump), rotation = -45)
plt.axhline(y = Results[0], color = '#299967', ls = '--')
plt.ylabel('Mean Value')
plt.xlabel('Time')
plt.grid(True)
## Second subplot: moving average for determining warm-up period
## (Welch method)
plt.subplot(212)
plt.plot(time[0:len(yBarWvector)],yBarWvector, color = '#340B8C')
plt.xticks(numpy.arange(0, T+1, jump), rotation = -45)
plt.ylabel('yBarW')
plt.xlabel('Time')
plt.xlim((0, T))
plt.grid(True)
In the second subplot, what should be the arguments for plt.ylim()function? I tried defining
在第二个子图中,plt.ylim()函数的参数应该是什么?我试着定义
ymin, ymax = plt.ylim()
in the first subplot and then set
在第一个子图中,然后设置
plt.ylim((ymin,ymax))
in the second subplot. But that did not work, because the returned value ymaxis the maximum value taken by the yvariable (mean value) in the first subplot and not the upper limit of the y-axis.
在第二个子图中。但这不起作用,因为返回的值ymax是y第一个子图中变量(平均值)所取的最大值,而不是 y 轴的上限。
Thanks in advance.
提前致谢。
采纳答案by Curious2learn
I searched some more on the matplotlib website and figured a way to do it. If anyone has a better way, please let me know.
我在 matplotlib 网站上搜索了更多内容,并找到了一种方法。如果有人有更好的方法,请告诉我。
In the first subplot replace plt.subplot(211, axisbg = 'w')by ax1 = plt.subplot(211, axisbg = 'w')
. Then, in the second subplot, add the arguments sharex = ax1and sharey = ax1to the subplot command. That is, the second subplot command will now look:
在第一个子图中替换plt.subplot(211, axisbg = 'w')为ax1 = plt.subplot(211, axisbg = 'w')
. 然后,在第二个子图中,将参数sharex = ax1和添加sharey = ax1到子图命令中。也就是说,第二个子图命令现在看起来:
plt.subplot(212, sharex = ax1, sharey = ax1)
This solves the problem. But if there are other better alternatives, please let me know.
这解决了问题。但如果有其他更好的选择,请告诉我。
回答by Amro
Your proposed solution should work, especially if the plots are interactive (they will stay in sync if one changes).
您提出的解决方案应该有效,特别是如果绘图是交互式的(如果一个更改,它们将保持同步)。
As alternative, you can manually set the y-limits of the second axis to match that of the first. Example:
或者,您可以手动设置第二个轴的 y 范围以匹配第一个轴的 y 范围。例子:
from pylab import *
x = arange(0.0, 2.0, 0.01)
y1 = 3*sin(2*pi*x)
y2 = sin(2*pi*x)
figure()
ax1 = subplot(211)
plot(x, y1, 'b')
subplot(212)
plot(x, y2, 'g')
ylim( ax1.get_ylim() ) # set y-limit to match first axis
show()



