在Windows系统上,当我使用pickle模块保存并重新加载3D matplotlib表面图时,这些图缺乏任何交互功能,例如旋转或缩放。然而,如果我保存并重新加载2D的matplotlib图,我仍然能够与它们进行交互,至少可以使用缩放工具。以下代码重现了成功的2D图案例。
下面的代码是一个小例子,重新创建了3D绘图问题。
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()
下面的代码是一个小例子,重新创建了3D绘图问题。
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()
我该如何修改代码,使得我的3D plot有交互式的旋转和缩放功能?如果3D plot无法实现此功能,是否有其他方法可以保存(而不必在窗口中显示图形)并重新加载交互式的3D matplotlib曲面图?