Skip to content Skip to sidebar Skip to footer

Pickled Matplotlib 3d Lacks Interactive Functionality

On Windows, when I save 3D matplotlib surface plots using the pickle module and reload them, the plots lack any interactive functionality, such as being able to rotate the plots or

Solution 1:

I found not a solution but a work-around. The work-around is to save all data needed to draw r=the figure plus the function that draws it in a pickle-file.

To do this, I needed to use dill instead of picke, as advised bu @JoshRosen in his answer to Is there an easy way to pickle a python function (or otherwise serialize its code)?

Create the pickle:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import dill as pickle


defplotfun(plotdata):
    ax = plt.gca()
    ax.plot_surface(plotdata['xs'], plotdata['ys'], plotdata['zs'])


xs = np.linspace(0, 1, 100)
ys = np.linspace(0, 4*np.pi, 100)
zs = np.zeros((100, 100))
for j, phase inenumerate(ys):
    zs[j, :] = xs*np.sin(phase)
xs, ys = np.meshgrid(xs, ys)

fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
plotdata = {'xs': xs, 'ys': ys, 'zs': zs, 'plotfun': plotfun}
plotdata['plotfun'](plotdata)
withopen("test.pickle", 'wb') as file:
    pickle.dump(plotdata, file)

Load the pickle

import dill as pickle
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt


plt.figure().add_subplot(111, projection='3d'),
plotdata = pickle.load(open('test.pickle', 'rb'))
plotdata['plotfun'](plotdata)
plt.show()

Post a Comment for "Pickled Matplotlib 3d Lacks Interactive Functionality"