Python Tkinter 3d Plots Cannot Pan Or Zoom
I've been trying to include 2d/3d plots using matplotlib in my Python GUI application using TKinter and have had success with 2d plots, but not 3d plots. My issue is that none of
Solution 1:
First, don't use pyplot to create the figure that you want to embedd in tkinter (or any other GUI), because having the same figure managed by pyplot as well as your custom GUI may lead to all kinds of problems. Using the matplotlib.figure.Figure
(as shown in the "Embedding in tk" example), would in this case have the additional benefit of emitting a warning about the problem:
UserWarning: Axes3D.figure.canvas is 'None', mouse rotation disabled. Set canvas then call Axes3D.mouse_init().
This essenitally means that you need to either call mouse_init()
, or simply create the 3D axes after setting the canvas. The latter is shown below.
import tkinter
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
root = tkinter.Tk()
root.wm_title("Embedding in Tk")
fig = Figure(figsize=(5, 4), dpi=100)
canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
canvas.draw()
ax = fig.add_subplot(111, projection="3d")
t = np.arange(0, 3, .01)
ax.plot(t, 2 * np.sin(2 * np.pi * t))
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
tkinter.mainloop()
Post a Comment for "Python Tkinter 3d Plots Cannot Pan Or Zoom"