matplotlib中共享轴正方形子块的新pythonic样式?

2024-05-13 19:47:39 发布

您现在位置:Python中文网/ 问答频道 /正文

相关:plotting autoscaled subplots with fixed limits in matplotlib

我想使用subplots新的紧凑样式(如http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.subplots)创建一组相同比例的子块,并使它们为正方形。

我试过:

fig, axes = subplots(numplots, 1, sharex=True, sharey=True, adjustable='box', aspect='equal')

但是我发现这些关键字参数没有在subplots包装器中实现。怎么做?

重申一下,我们的目标只是让共享轴,这样所有数据都在同一比例上,并且图都是正方形的。


Tags: inapitruehttpmatplotlibwith样式plotting
2条回答

只需使用adjustable='box-forced'而不是adjustable='box'

正如@cronos所提到的,您可以使用subplot_kwkwarg传递它(到subplots的附加关键字参数传递给Figure,而不是Axes,因此需要subplot_kw)。

相反,我将使用setp,这基本上就是for item in sequence: item.set(**kwargs)。(所有matplotlib艺术家都有一个set方法,可以使用类似于matlab的set。)

哪一种方法更好取决于你在做什么。很多人会说setp是非常“不通俗的”,但我不认为有问题。

举个简单的例子:

import matplotlib.pyplot as plt

fig, axes = plt.subplots(ncols=3, sharex=True, sharey=True)
plt.setp(axes.flat, aspect=1.0, adjustable='box-forced')

axes[0].plot(range(50))

plt.show()

enter image description here

我暂时忘记了两种不同的可调盒型的原因。我记得当我第一次遇到它的时候,我发现它真的很混乱,我翻遍了代码,有一些明显的原因。。。不过,我不记得当时是什么原因。

你所参考的文件表明

fig, axes = subplots(numplots, 1, sharex=True, sharey=True, subplot_kw=dict(adjustable='datalim', aspect='equal'))

然而,共享轴似乎需要datalim作为可调参数,图的缩放是正确的,但不是正方形的。如果省略共享轴,则“box”起作用。你的电话。

相关问题 更多 >