我可以创建 AxesSubplot 对象,然后将它们添加到 Figure 实例吗?
查看matplotlib
文档,似乎将AxesSubplot
添加到Figure
的标准方法是使用Figure.add_subplot
code>:
from matplotlib import pyplot
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
ax.hist( some params .... )
我希望能够独立于图形创建类似于 AxesSubPlot 的对象,这样我就可以在不同的图形中使用它们。像
fig = pyplot.figure()
histoA = some_axes_subplot_maker.hist( some params ..... )
histoA = some_axes_subplot_maker.hist( some other params ..... )
# make one figure with both plots
fig.add_subaxes(histo1, 211)
fig.add_subaxes(histo1, 212)
fig2 = pyplot.figure()
# make a figure with the first plot only
fig2.add_subaxes(histo1, 111)
这样在matplotlib
中可能吗?如果可以,我该怎么做?
更新:我还没有成功地解耦轴和图形的创建,但是按照下面答案中的示例,可以轻松地在新的或 olf 图实例中重复使用以前创建的轴。这可以用一个简单的函数来说明:
def plot_axes(ax, fig=None, geometry=(1,1,1)):
if fig is None:
fig = plt.figure()
if ax.get_geometry() != geometry :
ax.change_geometry(*geometry)
ax = fig.axes.append(ax)
return fig
Looking at the matplotlib
documentation, it seems the standard way to add an AxesSubplot
to a Figure
is to use Figure.add_subplot
:
from matplotlib import pyplot
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
ax.hist( some params .... )
I would like to be able to create AxesSubPlot
-like objects independently of the figure, so I can use them in different figures. Something like
fig = pyplot.figure()
histoA = some_axes_subplot_maker.hist( some params ..... )
histoA = some_axes_subplot_maker.hist( some other params ..... )
# make one figure with both plots
fig.add_subaxes(histo1, 211)
fig.add_subaxes(histo1, 212)
fig2 = pyplot.figure()
# make a figure with the first plot only
fig2.add_subaxes(histo1, 111)
Is this possible in matplotlib
and if so, how can I do this?
Update: I have not managed to decouple creation of Axes and Figures, but following examples in the answers below, can easily re-use previously created axes in new or olf Figure instances. This can be illustrated with a simple function:
def plot_axes(ax, fig=None, geometry=(1,1,1)):
if fig is None:
fig = plt.figure()
if ax.get_geometry() != geometry :
ax.change_geometry(*geometry)
ax = fig.axes.append(ax)
return fig
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通常,您只需将坐标区实例传递给函数即可。
例如:
要回答您的问题,您始终可以执行以下操作:
此外,您可以简单地将轴实例添加到另一个图形:
调整其大小以匹配其他子图“形状”也是可能的,但它很快就会变得更多麻烦大于其价值。根据我的经验,对于复杂的情况,仅传递图形或轴实例(或实例列表)的方法要简单得多......
Typically, you just pass the axes instance to a function.
For example:
To respond to your question, you could always do something like this:
Also, you can simply add an axes instance to another figure:
Resizing it to match other subplot "shapes" is also possible, but it's going to quickly become more trouble than it's worth. The approach of just passing around a figure or axes instance (or list of instances) is much simpler for complex cases, in my experience...
下面显示了如何将轴从一个图形“移动”到另一个图形。这是 @JoeKington 的最后一个示例 的预期功能,在较新的 matplotlib 版本中不再起作用,因为轴不能存在于同时出现几个数字。
您首先需要从第一个图中删除轴,然后将其附加到下一个图中并为其提供一些居住位置。
The following shows how to "move" an axes from one figure to another. This is the intended functionality of @JoeKington's last example, which in newer matplotlib versions is not working anymore, because axes cannot live in several figures at once.
You would first need to remove the axes from the first figure, then append it to the next figure and give it some position to live in.
对于线图,您可以处理 Line2D 对象本身:
For line plots, you can deal with the
Line2D
objects themselves:TL;DR部分基于Joe很好的答案。
Opt.1:
fig.add_subplot()
Opt.2:将
ax[index]
传递给返回ax[index].plot()
的函数code>输出尊重。
注意:Opt.1
plt.title()
在 opt.2 中更改为ax[index].set_title()
。查找更多 Van der Plas 书中的 Matplotlib 陷阱。TL;DR based partly on Joe nice answer.
Opt.1:
fig.add_subplot()
Opt.2: pass
ax[index]
to a function that returnsax[index].plot()
Outputs respect.
Note: Opt.1
plt.title()
changed in opt.2 toax[index].set_title()
. Find more Matplotlib Gotchas in Van der Plas book.想要深入兔子洞。扩展我之前的答案,可以返回整个 ax,而不仅仅是 ax.plot()。例如,
如果数据帧有 20 种类型的 100 个测试(此处为 id):
以及绘图函数(这是整个答案的关键):
我们可以实现:
To go deeper in the rabbit hole. Extending my previous answer, one could return a whole
ax
, and notax.plot()
only. E.g.If dataframe had 100 tests of 20 types (here id):
And the plot function (this is the key of this whole answer):
We can achieve: