我修改了一些我发现的代码,用于生成包含Matplotlib图形的GUI。
这是我构建GUI的第一个阶段,我还需要为要更改的用户添加参数和更新按钮。但首先我想让这个工作。我目前有6个子图,所有子图都投影到3D轴上。现在我希望能够使用鼠标理想地旋转轴,或者单独旋转每个轴,或者将所有6个轴作为一组旋转。我目前无法旋转任何东西
这可能吗?你能帮我更新一下这段代码吗?
我在Windows 10中使用Spyder上的Python 3.5 Anaconda发行版
由于
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
# implement the default mpl key bindings
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import sys
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
root = Tk.Tk()
root.wm_title("Embedding in TK")
fig = Figure(figsize=(5, 4), dpi=100)
a = fig.add_subplot(231, projection = '3d')
b = fig.add_subplot(232, projection = '3d')
c = fig.add_subplot(233, projection = '3d')
d = fig.add_subplot(234, projection = '3d')
e = fig.add_subplot(235, projection = '3d')
f = fig.add_subplot(236, projection = '3d')
x = np.arange(0.0, 3.0, 0.01)
y = np.arange(3.0, 0.0, -0.01)
s = np.sin(2*np.pi*x)
cos = np.cos(2*np.pi*x)
t = np.tan(2*np.pi*x)
a.plot(x, y, s)
b.plot(x, y, cos)
c.plot(x, y, t)
# a tk.DrawingArea
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
toolbar = NavigationToolbar2TkAgg(canvas, root)
toolbar.update()
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
def on_key_event(event):
print('you pressed %s' % event.key)
key_press_handler(event, canvas, toolbar)
canvas.mpl_connect('key_press_event', on_key_event)
def _quit():
root.quit() # stops mainloop
root.destroy() # this is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL tstate
button = Tk.Button(master=root, text='Quit', command=_quit)
button.pack(side=Tk.BOTTOM)
Tk.mainloop()