簡體   English   中英

將python matplotlib圖形另存為pickle

[英]Save python matplotlib figure as pickle

我想將matplotlib圖形保存為泡菜,以使我的團隊能夠輕松地加載它們並調查異常事件。 使用簡單的圖像格式會失去放大和研究圖形的能力。 因此,我試圖腌制我的身材:

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

然后我加載它。 看起來不錯。 首先。

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

但是后來我發現sharex不起作用。 當我放大一個子圖時,其他子圖保持不變。 在原始圖上,這很好用,並且可以在兩個x軸上放大,但是在給該圖腌制之后,這不再起作用。 如何保存繪圖,以便在加載后繼續共享x? 或者,在從泡菜中加載圖形后,如何恢復share-x?

謝謝!

在當前的matplotlib開發版本中,由於提供了修復程序,因此該方法應該可以正常工作。

使用最新發布的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()

這樣做的缺點是您需要知道共享哪些軸。 如上所示,在共享所有軸的情況下,這當然很容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM