在 matplotlib 中创建方形子图(高度和宽度相等)

发布于 2024-09-08 09:30:18 字数 1219 浏览 5 评论 0原文

当我运行这段代码时,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无畏 2024-09-15 09:30:19

当您使用 sharex 和 sharey 时,设置绘图方面的问题就会出现。

一种解决方法是不使用共享轴。例如,您可以这样做:

from pylab import *

figure()
subplot(121, aspect='equal')
plot([1, 2, 3], [1, 2, 3])
subplot(122, aspect='equal')
plot([1, 2, 3], [1, 2, 3])
show()

但是,更好的解决方法是更改​​“可调整”keywarg...您想要可调整='box',但是当您使用共享轴时,它必须是可调整='datalim' (并将其设置回“box”会产生错误)。

不过,adjustable 还有第三个选项可以准确处理这种情况:adjustable="box-forced"

例如:

from pylab import *

figure()
ax1 = subplot(121, aspect='equal', adjustable='box-forced')
plot([1, 2, 3], [1, 2, 3])
subplot(122, aspect='equal', adjustable='box-forced', sharex=ax1, sharey=ax1)
plot([1, 2, 3], [1, 2, 3])
show()

或者采用更现代的风格(注意:这部分答案在 2010 年是行不通的):

import matplotlib.pyplot as plt

fig, axes = plt.subplots(ncols=2, sharex=True, sharey=True)
for ax in axes:
    ax.plot([1, 2, 3], [1, 2, 3])
    ax.set(adjustable='box-forced', aspect='equal')

plt.show()

无论哪种方式,您都会得到类似的内容:

在此处输入图像描述

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:

from pylab import *

figure()
subplot(121, aspect='equal')
plot([1, 2, 3], [1, 2, 3])
subplot(122, aspect='equal')
plot([1, 2, 3], [1, 2, 3])
show()

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:

from pylab import *

figure()
ax1 = subplot(121, aspect='equal', adjustable='box-forced')
plot([1, 2, 3], [1, 2, 3])
subplot(122, aspect='equal', adjustable='box-forced', sharex=ax1, sharey=ax1)
plot([1, 2, 3], [1, 2, 3])
show()

Or in more modern style (note: this part of the answer wouldn't have worked in 2010):

import matplotlib.pyplot as plt

fig, axes = plt.subplots(ncols=2, sharex=True, sharey=True)
for ax in axes:
    ax.plot([1, 2, 3], [1, 2, 3])
    ax.set(adjustable='box-forced', aspect='equal')

plt.show()

Either way, you'll get something similar to:

enter image description here

柳若烟 2024-09-15 09:30:19

尝试一下:

from pylab import *

figure()
ax1 = subplot(121, autoscale_on=False, aspect='equal', xlim=[1,3], ylim=[1,3])
plot([1, 2, 3], [1, 2, 3])
##axes().set_aspect('equal')
ax2 = subplot(122, autoscale_on=False, aspect='equal', xlim=[1,3], ylim=[1,3])
plot([1, 2, 3], [1, 2, 3])
draw()
show()

我注释掉了axes()行,因为这会在任意位置创建一个新的axes,而不是预制的subplot< /code> 具有计算出的位置。

调用 subplot 实际上会创建一个 Axes 实例,这意味着它可以使用与 Axes 相同的属性。

我希望这有帮助:)

Give this a try:

from pylab import *

figure()
ax1 = subplot(121, autoscale_on=False, aspect='equal', xlim=[1,3], ylim=[1,3])
plot([1, 2, 3], [1, 2, 3])
##axes().set_aspect('equal')
ax2 = subplot(122, autoscale_on=False, aspect='equal', xlim=[1,3], ylim=[1,3])
plot([1, 2, 3], [1, 2, 3])
draw()
show()

I commented out the axes() line as that would create a new axes at an arbitrary location, rather than a pre-fabricated subplot with a calculated position.

Calling subplot actually creates an Axes instance, which means it can use the same properties as that of an Axes.

I hope this helps :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文