将matplotlib图嵌入大图 [英] Embed matplotlib figure in larger figure

查看:111
本文介绍了将matplotlib图嵌入大图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一堆用于处理天文数据的脚本和函数.我有一组星系,我想在3面板图中绘制一些不同的属性.我这里有一个布局示例:

I am writing a bunch of scripts and functions for processing astronomical data. I have a set of galaxies, for which I want to plot some different properties in a 3-panel plot. I have an example of the layout here:

现在,这不是问题.但是有时候,我只想为一个星系创建这个图.在其他情况下,我想制作一个更大的图,其中包含每个子图,每个子图均由三层以上的窗格结构组成,例如此模型:

Now, this is not a problem. But sometimes, I want to create this plot just for a single galaxy. In other cases, I want to make a larger plot consisting of subplots that each are made up of the three+pane structure, like this mockup:

为了我的代码的模块化和可重用性,我想做一些事情,只是让我的函数返回一个matplotlib.figure.Figure对象,然后让调用者-函数或交互式会话-决定是否savefig对象或将其嵌入较大的图中.但是我似乎在文档中或其他地方都找不到任何暗示,这似乎并不是人们经常这样做的地方.

For the sake of modularity and reusability of my code, I would like to do something to the effect of just letting my function return a matplotlib.figure.Figure object and then let the caller - function or interactive session - decide whether to show() or savefig the object or embed it in a larger figure. But I cannot seem to find any hints of this in the documentation or elsewhere, it doesn't seem to be something people do that often.

关于什么是最好的道路的任何建议?我已经推测是否使用axes_grid是解决方案,但对我来说,似乎相当并不干净且与呼叫者无关.有什么建议吗?

Any suggestions as to what would be the best road to take? I have speculated whether using axes_grid would be the solution, but it doesn't seem quite clean and caller-agnostic to me. Any suggestions?

推荐答案

最好的解决方案是将图形布局逻辑与绘图逻辑分开.编写类似以下内容的绘图代码:

The best solution is to separate the figure layout logic from the plotting logic. Write your plotting code something like this:

 def three_panel_plot(data, ploting_args, ax1, ax2, ax3):
     # what you do to plot

因此,现在转换数据->图像的代码将数据以及应在何处绘制该数据作为参数.

So now the code that turns data -> images takes as arguments the data and where it should plot that data too.

如果只想做一个,那很容易,如果要做一个3x3的网格,只需要生成布局,然后在轴集+数据上循环即可.

If you want to do just one, it's easy, if you want to do a 3x3 grid, you just need to generate the layout and then loop over the axes sets + data.

在matplotlib中,您建议的方式(将对象从绘图例程中退出)非常困难,内部结构也是如此.

The way you are suggesting (returning an object out of your plotting routine) would be very hard in matplotlib, the internals are too connected.

这篇关于将matplotlib图嵌入大图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆