简体   繁体   English

将python matplotlib图形另存为pickle

[英]Save python matplotlib figure as pickle

I want to save matplotlib figures as pickle, to enable my team to easily load them and investigate anomalous events. 我想将matplotlib图形保存为泡菜,以使我的团队能够轻松地加载它们并调查异常事件。 Using a simple image format loses the ability to zoom-in and investigate the figure. 使用简单的图像格式会失去放大和研究图形的能力。 So I tried to pickle my figure: 因此,我试图腌制我的身材:

fig, axarr = plt.subplots(2, sharex='all') # creating the figure
... # plotting things
...
pickle.dump(fig, open('myfigfile.p'), 'wb'))

Then I load it. 然后我加载它。 Looks good. 看起来不错。 At first. 首先。

fig = pickle.load(open('myfigfile.p', 'rb'))
plt.show()

But then I see that sharex doesn't work. 但是后来我发现sharex不起作用。 When I zoom-in on one subplot, the other remains static. 当我放大一个子图时,其他子图保持不变。 On the original plot this works great, and zooming-in zooms on both x-axis, but after I pickle-load the figure, this doesn't work anymore. 在原始图上,这很好用,并且可以在两个x轴上放大,但是在给该图腌制之后,这不再起作用。 How can I save the plot so that it continues to share-x after loading? 如何保存绘图,以便在加载后继续共享x? Or, how can I reinstate share-x after loading the figure from the pickle? 或者,在从泡菜中加载图形后,如何恢复share-x?

Thanks! 谢谢!

In the current development version of matplotlib this should work due to the fix provided . 在当前的matplotlib开发版本中,由于提供了修复程序,因此该方法应该可以正常工作。

With the current released version of matplotlib, would need to reestablish the sharing eg like 使用最新发布的matplotlib版本,需要重新建立共享,例如

import matplotlib.pyplot as plt
import pickle

fig, axes = plt.subplots(ncols=3, sharey="row")
axes[0].plot([0,1],[0,1])
axes[1].plot([0,1],[1,2])
axes[2].plot([0,1],[2,3])

pickle.dump(fig, file('fig1.pkl', 'wb'))  

plt.close("all")

fig2 = pickle.load(file('fig1.pkl','rb'))
ax_master = fig2.axes[0]
for ax in fig2.axes:
    if ax is not ax_master:
        ax_master.get_shared_y_axes().join(ax_master, ax)

plt.show()

This has the drawback that you need to know which axes to share. 这样做的缺点是您需要知道共享哪些轴。 In the case of all axes being shared this is of course easy, as shown above. 如上所示,在共享所有轴的情况下,这当然很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM