在 matplotlib 中创建方形子图(高度和宽度相等)
当我运行这段代码时,
from pylab import *
figure()
ax1 = subplot(121)
plot([1, 2, 3], [1, 2, 3])
subplot(122, sharex=ax1, sharey=ax1)
plot([1, 2, 3], [1, 2, 3])
draw()
show()
我得到两个在 X 维度上被“压扁”的子图。如何获得这些子图,使两个子图的 Y 轴高度等于 X 轴宽度?
我在 Ubuntu 10.04 上使用 matplotlib v.0.99.1.2。
更新 2010-07-08:让我们看看一些不起作用的事情。
经过一整天的谷歌搜索后,我认为这可能与自动缩放有关。所以我尝试摆弄它。
from pylab import *
figure()
ax1 = subplot(121, autoscale_on=False)
plot([1, 2, 3], [1, 2, 3])
subplot(122, sharex=ax1, sharey=ax1)
plot([1, 2, 3], [1, 2, 3])
draw()
show()
matplotlib 坚持自动缩放。
from pylab import *
figure()
ax1 = subplot(121, autoscale_on=False)
plot([1, 2, 3], [1, 2, 3])
subplot(122, sharex=ax1, sharey=ax1, autoscale_on=False)
plot([1, 2, 3], [1, 2, 3])
draw()
show()
在这一过程中,数据完全消失。 WTF,matplotlib?只是 WTF?
好吧,也许我们可以调整纵横比?
from pylab import *
figure()
ax1 = subplot(121, autoscale_on=False)
plot([1, 2, 3], [1, 2, 3])
axes().set_aspect('equal')
subplot(122, sharex=ax1, sharey=ax1)
plot([1, 2, 3], [1, 2, 3])
draw()
show()
这导致第一个子图完全消失。太搞笑了!谁想出了这个?
说实话,现在……这真的是一件很难完成的事情吗?
When I run this code
from pylab import *
figure()
ax1 = subplot(121)
plot([1, 2, 3], [1, 2, 3])
subplot(122, sharex=ax1, sharey=ax1)
plot([1, 2, 3], [1, 2, 3])
draw()
show()
I get two subplots which are "squished" in the X-dimension. How do I get these subplots such that the height of the Y-axis equals the width of the X-axis, for both subplots?
I am using matplotlib v.0.99.1.2 on Ubuntu 10.04.
Update 2010-07-08: Let's look at some things that don't work.
After Googling around all day, I thought that it might be related to auto-scaling. So I tried fiddling with that.
from pylab import *
figure()
ax1 = subplot(121, autoscale_on=False)
plot([1, 2, 3], [1, 2, 3])
subplot(122, sharex=ax1, sharey=ax1)
plot([1, 2, 3], [1, 2, 3])
draw()
show()
matplotlib insists on auto-scaling.
from pylab import *
figure()
ax1 = subplot(121, autoscale_on=False)
plot([1, 2, 3], [1, 2, 3])
subplot(122, sharex=ax1, sharey=ax1, autoscale_on=False)
plot([1, 2, 3], [1, 2, 3])
draw()
show()
In this one, the data completely disappears. WTF, matplotlib? Just WTF?
Okay, well maybe if we fix the aspect ratio?
from pylab import *
figure()
ax1 = subplot(121, autoscale_on=False)
plot([1, 2, 3], [1, 2, 3])
axes().set_aspect('equal')
subplot(122, sharex=ax1, sharey=ax1)
plot([1, 2, 3], [1, 2, 3])
draw()
show()
This one causes the first subplot to disappear entirely. That's hilarious! Who came up with that one?
In all seriousness, now... should this really be such a hard thing to accomplish?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用 sharex 和 sharey 时,设置绘图方面的问题就会出现。
一种解决方法是不使用共享轴。例如,您可以这样做:
但是,更好的解决方法是更改“可调整”keywarg...您想要可调整='box',但是当您使用共享轴时,它必须是可调整='datalim' (并将其设置回“box”会产生错误)。
不过,
adjustable
还有第三个选项可以准确处理这种情况:adjustable="box-forced"
。例如:
或者采用更现代的风格(注意:这部分答案在 2010 年是行不通的):
无论哪种方式,您都会得到类似的内容:
Your problem in setting the aspect of the plots is coming in when you're using sharex and sharey.
One workaround is to just not used shared axes. For example, you could do this:
However, a better workaround is to change the "adjustable" keywarg... You want adjustable='box', but when you're using shared axes, it has to be adjustable='datalim' (and setting it back to 'box' gives an error).
However, there's a third option for
adjustable
to handle exactly this case:adjustable="box-forced"
.For example:
Or in more modern style (note: this part of the answer wouldn't have worked in 2010):
Either way, you'll get something similar to:
尝试一下:
我注释掉了axes()行,因为这会在任意位置创建一个新的axes,而不是预制的subplot< /code> 具有计算出的位置。
调用
subplot
实际上会创建一个Axes
实例,这意味着它可以使用与Axes
相同的属性。我希望这有帮助:)
Give this a try:
I commented out the
axes()
line as that would create a newaxes
at an arbitrary location, rather than a pre-fabricatedsubplot
with a calculated position.Calling
subplot
actually creates anAxes
instance, which means it can use the same properties as that of anAxes
.I hope this helps :)