保存交互式 Matplotlib 图形 [英] Saving interactive Matplotlib figures

查看:71
本文介绍了保存交互式 Matplotlib 图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法保存一个 Matplotlib 图形,以便它可以重新打开并恢复典型的交互?(像 MATLAB 中的 .fig 格式?)

Is there a way to save a Matplotlib figure such that it can be re-opened and have typical interaction restored? (Like the .fig format in MATLAB?)

我发现自己多次运行相同的脚本来生成这些交互式图形.或者我向我的同事发送多个静态 PNG 文件以显示绘图的不同方面.我宁愿发送图形对象并让他们自己与之交互.

I find myself running the same scripts many times to generate these interactive figures. Or I'm sending my colleagues multiple static PNG files to show different aspects of a plot. I'd rather send the figure object and have them interact with it themselves.

推荐答案

这将是一个很棒的功能,但 AFAIK 它没有在 Matplotlib 中实现,并且由于数字的方式可能很难自己实现存储.

我建议要么 (a) 将数据与生成图形(以唯一名称保存数据)分开处理,并编写图形生成脚本(加载已保存数据的指定文件)并根据需要进行编辑或 (b) 保存为 PDF/SVG/PostScript 格式并在一些花哨的图形编辑器中进行编辑,例如 Adobe Illustrator(或 Inkscape).

I'd suggest either (a) separate processing the data from generating the figure (which saves data with a unique name) and write a figure generating script (loading a specified file of the saved data) and editing as you see fit or (b) save as PDF/SVG/PostScript format and edit in some fancy figure editor like Adobe Illustrator (or Inkscape).

EDIT post Fall 2012:正如其他人在下面指出的(尽管在这里提到这是公认的答案),自 1.2 版以来的 Matplotlib 允许您腌制数字.正如发行说明,这是一项实验性功能并且不支持在一个 matplotlib 版本中保存图形并在另一个版本中打开.从不受信任的来源恢复泡菜通常也是不安全的.

EDIT post Fall 2012: As others pointed out below (though mentioning here as this is the accepted answer), Matplotlib since version 1.2 allowed you to pickle figures. As the release notes state, it is an experimental feature and does not support saving a figure in one matplotlib version and opening in another. It's also generally unsecure to restore a pickle from an untrusted source.

对于共享/稍后编辑图(需要先进行大量数据处理,可能需要在几个月后进行调整,比如在科学出版物的同行评审期间),我仍然建议 (1) 的工作流程有一个之前的数据处理脚本生成绘图将处理后的数据(进入您的绘图)保存到一个文件中,并且 (2) 有一个单独的绘图生成脚本(您可以根据需要进行调整)来重新创建绘图.通过这种方式,您可以为每个绘图快速运行脚本并重新生成它(并使用新数据快速复制绘图设置).也就是说,对图形进行酸洗可能便于短期/交互式/探索性数据分析.

For sharing/later editing plots (that require significant data processing first and may need to be tweaked months later say during peer review for a scientific publication), I still recommend the workflow of (1) have a data processing script that before generating a plot saves the processed data (that goes into your plot) into a file, and (2) have a separate plot generation script (that you adjust as necessary) to recreate the plot. This way for each plot you can quickly run a script and re-generate it (and quickly copy over your plot settings with new data). That said, pickling a figure could be convenient for short term/interactive/exploratory data analysis.

这篇关于保存交互式 Matplotlib 图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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