You may already know, that in matplotlib 1.2.0 there is a new experimental feature, that figures are pickable (they can be saved with pickle module).
However, it doesn’t work when one uses logscale, eg.
import matplotlib.pyplot as plt
import numpy as np
import pickle
ax = plt.subplot(111)
x = np.linspace(0, 10)
y = np.exp(x)
plt.plot(x, y)
ax.set_yscale('log')
pickle.dump(ax, file('myplot.pickle', 'w'))
results in:
PicklingError: Can't pickle <class 'matplotlib.scale.Log10Transform'>: attribute lookup matplotlib.scale.Log10Transform failed
Anybody knows any solution/workaround to this?
I’ve opened this as a bug report on matplotlib’s github issue tracker. Its a fairly easy fix to implement on the matplotlib repository side (simply don’t nest the
Log10Transformclass inside theLogScaleclass), but that doesn’t really help you in being able to use this with mpl 1.2.0…There is a solution to getting this to work for you in 1.2.0, but I warn you – its not pretty!
Based on my answer to a pickling question it is possible to pickle nested classes (as
Log10Transformis). All we need to do is to tellLog10Transformhow to “reduce” itself:You might also decide to do this for other Log based transforms/classes, but for your example, you can now pickle (and successfully unpickle) your example figure:
I’m going to get on and fix this for mpl 1.3.x so that this nasty workaround isn’t needed in the future 🙂 .
HTH,