存储并重新加载 matplotlib.pyplot 对象

发布于 2024-12-02 21:28:58 字数 222 浏览 0 评论 0原文

我在一个伪操作环境中工作,我们在收到数据后制作新图像。 ,当新数据进来时,我们需要重新打开图像并更新该图像,以便创建合成、添加叠加等。除了添加到图像之外,这还需要修改标题、图例等。

有时 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

-残月青衣踏尘吟 2024-12-09 21:28:58

从 1.2 开始,matplotlib 附带了实验性酸洗支持。如果您遇到任何问题,请在 mpl 邮件列表上告知我们,或者在 github.com/matplotlib/matplotlib 上提出问题

HTH

编辑:添加了一个简单的示例,

import matplotlib.pyplot as plt
import numpy as np
import pickle

ax = plt.subplot(111)
x = np.linspace(0, 10)
y = np.exp(x)
ax.plot(x, y)
pickle.dump(ax, open('myplot.pickle', 'w'))

然后在单独的会话中:

import matplotlib.pyplot as plt
import pickle

ax = pickle.load(open('myplot.pickle'))
plt.show()

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

import matplotlib.pyplot as plt
import numpy as np
import pickle

ax = plt.subplot(111)
x = np.linspace(0, 10)
y = np.exp(x)
ax.plot(x, y)
pickle.dump(ax, open('myplot.pickle', 'w'))

Then in a separate session:

import matplotlib.pyplot as plt
import pickle

ax = pickle.load(open('myplot.pickle'))
plt.show()
灼疼热情 2024-12-09 21:28:58

对 Pelson 针对在 Jupyterhub 上工作的人员的答案进行了一个小修改,

在加载 pickle 之前使用 %matplotlib notebook 。在 jupyterhub 或 jupyter 笔记本中使用 %matplotlib inline 对我来说不起作用。并给出以结尾的回溯
AttributeError:“模块”对象没有属性“new_figure_manager_given_figure”。

import matplotlib.pyplot as plt
import numpy as np
import pickle

%matplotlib notebook

ax = plt.subplot(111)
x = np.linspace(0, 10)
y = np.exp(x)
plt.plot(x, y)
with open('myplot.pkl','wb') as fid:
    pickle.dump(ax, fid)

然后在一个单独的会话中:

import matplotlib.pyplot as plt
import pickle

%matplotlib notebook

with open('myplot.pkl','rb') as fid:
    ax = pickle.load(fid)
plt.show()

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 in
AttributeError: 'module' object has no attribute 'new_figure_manager_given_figure'.

import matplotlib.pyplot as plt
import numpy as np
import pickle

%matplotlib notebook

ax = plt.subplot(111)
x = np.linspace(0, 10)
y = np.exp(x)
plt.plot(x, y)
with open('myplot.pkl','wb') as fid:
    pickle.dump(ax, fid)

Then in a separate session:

import matplotlib.pyplot as plt
import pickle

%matplotlib notebook

with open('myplot.pkl','rb') as fid:
    ax = pickle.load(fid)
plt.show()
请你别敷衍 2024-12-09 21:28:58

我使用 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.

任谁 2024-12-09 21:28:58

在 jupyter 笔记本上测试。

import matplotlib.pyplot as plt
import numpy as np
import pickle

fig, axes = plt.subplots(figsize=(20, 5),nrows=1, ncols=2) 
x,y = np.arange(10), np.random.random(10)
axes[0].plot(x,y)
axes[1].plot(x,y)
plt.show()

# myfname = input("Save figure? [enter filename]: ") 
myfname = 'test'
if (myfname!=''):
    fig.savefig(f'./{myfname}.png')
    print(f'file saved to ./{myfname}.png')
    with open(f'./{myfname}.png.pkl','wb') as fid:
        pickle.dump(fig, fid)
        print(f'pickled to ./{myfname}.png.pkl') 

###################################
####### in a separate session
myfname = 'test'
with open(f'./{myfname}.png.pkl', 'rb') as fh:
    fig_loaded = pickle.load(fh)

print(fig_loaded.axes[0].lines[0].get_data()) # get data
fig_loaded # show fig

Tested in jupyter notebook.

import matplotlib.pyplot as plt
import numpy as np
import pickle

fig, axes = plt.subplots(figsize=(20, 5),nrows=1, ncols=2) 
x,y = np.arange(10), np.random.random(10)
axes[0].plot(x,y)
axes[1].plot(x,y)
plt.show()

# myfname = input("Save figure? [enter filename]: ") 
myfname = 'test'
if (myfname!=''):
    fig.savefig(f'./{myfname}.png')
    print(f'file saved to ./{myfname}.png')
    with open(f'./{myfname}.png.pkl','wb') as fid:
        pickle.dump(fig, fid)
        print(f'pickled to ./{myfname}.png.pkl') 

###################################
####### in a separate session
myfname = 'test'
with open(f'./{myfname}.png.pkl', 'rb') as fh:
    fig_loaded = pickle.load(fh)

print(fig_loaded.axes[0].lines[0].get_data()) # get data
fig_loaded # show fig
や莫失莫忘 2024-12-09 21:28:58

你尝试过pickle模块吗?它序列化一个对象,将其转储到文件中,并可以稍后从文件中重新加载它。

Did you try the pickle module? It serialises an object, dumps it to a file, and can reload it from the file later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文