我如何以编程方式在Matplotlib中选择特定的子图? [英] How can I programmatically select a specific subplot in Matplotlib?
问题描述
因此,在图
中,已添加了三个垂直子图,其中 add_subplot
,我该如何选择让我们说中间的?
现在我做这个列表理解:
[r [0] ([self.figure.get_axes()],key = itemgetter(1))]中的[[ax,ax.get_geometry()[2]]]
我可以简单地选择我想要的索引,以及相应的轴
。有没有更直接的方式做到这一点? matplotlib文档:
如果这个数字已经有一个带有键(args,kwargs)的子图,那么它只会使该子图成为当前的并返回它。
一个例子:
pre $ import matplotlib.pyplot as plt
fig = plt.figure()
for vplot in [1,2,3]:
ax = fig.add_subplot(3,1,vplot)
ax.plot(范围(10),范围(10))
ax_again = fig.add_subplot(3,1,2)
ax_again.annotate(中间一个,xy =(7,5),xytext =(7,5))
plt.show()
再次调用中间图表,以便它可以注释。
如果我使用原始电话设置背景,当第二次获取子图时,是否需要再次设置它?
是的。原始调用的参数和关键字用于创建唯一标识符。因此,为了再次生成此唯一标识符,您需要再次传递相同的参数(网格定义,位置)和关键字。例如:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(2,1,1,axisbg ='red')
ax.plot(范围(10),范围(10))
ax = fig.add_subplot(2, 1,2)
ax.plot(范围(10),范围(10))
ax_again = fig.add_subplot(2,1,1,axisbg ='red')
ax_again.annotate(最上面的一个,xy =(7,5),xytext =(7,5))
plt.show()
如果我使用 ax_again.change_geometry()
?
你会想到change_geometry,例如从312到422,会改变你使用add_subplot的方式,但它不会。当您调用change_geometry时,似乎存在错误或未定义的行为。使用参数和关键字生成的第一个add_subplot调用的原始唯一键不会更新。因此,如果要使用add_subplot调用返回轴,则需要使用原始参数和关键字调用add_subplot。欲了解更多信息,请按照以下问题报告:
https://github.com/matplotlib / matplotlib / issues / 429
我现在的猜测是,如果您在使用add_subplot调用生成副图后更改了子图的任何属性,进行调整。所以只要使用原始的参数和关键字,并希望这会奏效。
So in a figure
where three vertical subplots have been added with add_subplot
, how can I select let's say the middle one?
Right now I do this list comprehension:
[r[0] for r in sorted([[ax, ax.get_geometry()[2]] for ax in self.figure.get_axes()], key=itemgetter(1))]
where I can simply select the index I want, with the corresponding axes
. Is there a more straightforward way of doing this?
From the matplotlib documentation:
If the figure already has a subplot with key (args, kwargs) then it will simply make that subplot current and return it.
Here's an example:
import matplotlib.pyplot as plt
fig = plt.figure()
for vplot in [1,2,3]:
ax = fig.add_subplot(3,1,vplot)
ax.plot(range(10),range(10))
ax_again = fig.add_subplot(3,1,2)
ax_again.annotate("The middle one",xy=(7,5),xytext=(7,5))
plt.show()
The middle plot is called again so that it can be annotated.
What if I set the background with my original call, do I need to set it again when I get the subplot the second time?
Yes. The arguments and keywords for the original call are used to make a unique identifier. So for the figure to generate this unique identifier again, you need to pass the same arguments (grid definition, position) and keywords again. For example:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(2,1,1,axisbg='red')
ax.plot(range(10),range(10))
ax = fig.add_subplot(2,1,2)
ax.plot(range(10),range(10))
ax_again = fig.add_subplot(2,1,1,axisbg='red')
ax_again.annotate("The top one",xy=(7,5),xytext=(7,5))
plt.show()
What if I use ax_again.change_geometry()
?
You would think change_geometry, e.g. from a 312 to a 422, would change how you use add_subplot, but it doesn't. There appears to be a bug or undefined behavior when you call change_geometry. The unique key that was original generated using the arguments and keywords, to the first add_subplot call, does not get updated. Therefore, if you want to get an axis back with an add_subplot call, you need to call add_subplot with the original arguments and keywords. For more info, follow this issue report: https://github.com/matplotlib/matplotlib/issues/429
My guess for now is that if you change any property of the subplot after generating it with add_subplot call, the unique will not be adjusted. So just use the original arguments and keywords, and hopefully this will work out.
这篇关于我如何以编程方式在Matplotlib中选择特定的子图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!