Pickled Matplotlib 3D Lacks Interactive Functionality

痴心易碎 提交于 2019-12-24 01:36:18

问题


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 zoom in on the plots. However, if I save and reload 2D pickled matplotlib plots, I am still able to interact with the plots, at least with the zoom tools. The following code recreates the successful 2D plot case.

import matplotlib.pyplot as plt
import numpy as np
import pickle


xs = np.linspace(0, 4*np.pi, 100)
ys = np.sin(xs)

fig, ax = plt.subplots()
ax.plot(xs, ys)

with open("plot.pickle", 'wb') as file:
    pickle.dump(fig, file)

plt.show()

plt.close(fig)

with open("plot.pickle", 'rb') as file:
    fig = pickle.load(file)

plt.show()

The following code is a small example that recreates the 3D plot problem.

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


xs = np.linspace(0, 1, 100)
ys = np.linspace(0, 4*np.pi, 100)
zs = np.zeros((100, 100))

for i, amp in enumerate(xs):
    for j, phase in enumerate(ys):
        zs[j, i] = amp*np.sin(phase)

xs, ys = np.meshgrid(xs, ys)
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
ax.plot_surface(xs, ys, zs)

with open("plot.pickle", 'wb') as file:
    pickle.dump(fig, file)

plt.close(fig)

with open("plot.pickle", 'rb') as file:
    fig = pickle.load(file)

plt.show()

What changes can I make so that my 3D pickled plots will have interactive rotate and zoom functionality? If such functionality is not possible with 3D pickled plots, are they any alternative methods for saving (without having to originally show the plot in a window) and reloading interactive 3D matplotlib surface plots?

来源:https://stackoverflow.com/questions/51511698/pickled-matplotlib-3d-lacks-interactive-functionality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!