I think you'll find it easier to save the data to a file from which you can later generate new plots. You could even use np.savez
to save not only the data, but also the plot method and its arguments in one file. Here is how you could later load those files to generate "joined" plots in a new figure:
import matplotlib.pyplot as plt
import numpy as np
def join(ax, files):
data = [np.load(filename) for filename in files]
for datum in data:
method = getattr(ax, datum['method'].item())
args = tuple(datum['args'])
kwargs = datum['kwargs'].item()
method(*args, **kwargs)
x = np.linspace(-3, 3, 100)
y = np.exp(-x**2/2)/np.sqrt(2*np.pi)
a = np.random.normal(size=10000)
fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()
np.savez('/tmp/a.npz', method='plot', args=(x, y), kwargs=dict())
fig, ax = plt.subplots()
ax.hist(a, bins=100, density=True)
plt.show()
np.savez('/tmp/b.npz', method='hist', args=(a,),
kwargs=dict(bins=100, density=True))
fig, ax = plt.subplots()
join(ax, ['/tmp/a.npz', '/tmp/b.npz'])
plt.show()
Above I used np.savez
and np.load
instead of pickle
to save and restore the data. Alternatively, you could pickle a dict, tuple or list containing the data, the method and its arguments. However, since the data is mainly numeric, using np.savez
is more efficient and less of a security risk than pickle.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…