存储并重新加载 matplotlib.pyplot 对象
我在一个伪操作环境中工作,我们在收到数据后制作新图像。 ,当新数据进来时,我们需要重新打开图像并更新该图像,以便创建合成、添加叠加等。除了添加到图像之外,这还需要修改标题、图例等。
有时 matplotlib 中内置的东西可以让我存储和重新加载 matplotlib.pyplot 对象以供以后使用?它需要维护对所有关联对象的访问,包括图形、线条、图例等。也许 pickle 就是我正在寻找的,但我对此表示怀疑。
I work in an psudo-operational environment where we make new imagery on receipt of data. Sometimes when new data comes in, we need to re-open an image and update that image in order to create composites, add overlays, etc. In addition to adding to the image, this requires modification of titles, legends, etc.
Is there something built into matplotlib that would let me store and reload my matplotlib.pyplot object for later use? It would need to maintain access to all associated objects including figures, lines, legends, etc. Maybe pickle is what I'm looking for, but I doubt it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从 1.2 开始,matplotlib 附带了实验性酸洗支持。如果您遇到任何问题,请在 mpl 邮件列表上告知我们,或者在 github.com/matplotlib/matplotlib 上提出问题
HTH
编辑:添加了一个简单的示例,
然后在单独的会话中:
As of 1.2 matplotlib ships with experimental pickling support. If you come across any issues with it, please let us know on the mpl mailing list or by opening an issue on github.com/matplotlib/matplotlib
HTH
EDIT: Added a simple example
Then in a separate session:
对 Pelson 针对在 Jupyterhub 上工作的人员的答案进行了一个小修改,
在加载 pickle 之前使用
%matplotlib notebook
。在 jupyterhub 或 jupyter 笔记本中使用%matplotlib inline
对我来说不起作用。并给出以结尾的回溯AttributeError:“模块”对象没有属性“new_figure_manager_given_figure”。
然后在一个单独的会话中:
A small modification to Pelson's answer for people working on a Jupyterhub
Use
%matplotlib notebook
before loading the pickle. Using%matplotlib inline
did not work for me in either jupyterhub or jupyter notebook. and gives a traceback ending inAttributeError: 'module' object has no attribute 'new_figure_manager_given_figure'.
Then in a separate session:
我使用 matplotlib 为多篇论文生成了图表。我不会考虑保存图形(如在 MATLAB 中),而是编写一个脚本来绘制数据,然后格式化并保存图形。如果我想保留数据的本地副本(特别是如果我想能够再次使用它),我发现 numpy.savez() 和 numpy.load() 非常有用。
起初,我怀念在 MATLAB 中保存图形的收缩包装感觉,但一段时间后,我开始喜欢这种方法,因为它包含可用于进一步分析的格式的数据。
I produced figures for a number of papers using matplotlib. Rather than thinking of saving the figure (as in MATLAB), I would write a script that plotted the data then formatted and saved the figure. In cases where I wanted to keep a local copy of the data (especially if I wanted to be able to play with it again) I found numpy.savez() and numpy.load() to be very useful.
At first I missed the shrink-wrapped feel of saving a figure in MATLAB, but after a while I have come to prefer this approach because it includes the data in a format that is available for further analysis.
在 jupyter 笔记本上测试。
Tested in jupyter notebook.
你尝试过pickle模块吗?它序列化一个对象,将其转储到文件中,并可以稍后从文件中重新加载它。
Did you try the pickle module? It serialises an object, dumps it to a file, and can reload it from the file later.