python - 旋转嵌入的 matplotlib 图

标签 python python-3.x matplotlib plot tkinter

我使用 Basemap 创建了一个相当简单的 matplotlib 绘图,然后使用中的轮廓将其嵌入到 tkinter 框架中matplotlib 文档并引用此处发布的问题。我查看了有关将 matplotlib 绘图嵌入 tkinter 框架中发现的其他问题,但仍然找不到此问题的解决方案。或者,我尝试了不同的方法来创建 Canvas /框架,因为我怀疑通过将框架创建移到类和其他方法之外,这才是真正的问题所在。当我使用下面的方法并且包含一个使用 exit()destroy() 来关闭窗口的 tkinter 按钮时,我得到了最大递归深度错误。

但是,由于某种原因,我仍然无法旋转嵌入的绘图,更奇怪的是,如果我取消注释下面在代码块中注释的行,我可以旋转绘图。这对我来说没有意义,因为它似乎完全多余,它只是创建了另一个空白窗口,然后我可以出于某种我不明白的原因再次旋转绘图。

作为另一个尝试的解决方案,我尝试使用 Axes3D.init_mouse 因为我已经看到它作为类似问题的答案发布,但这也不起作用。

以下是相关代码块

class GUI(tk.Tk):
    def __init__(self, parent):
        tk.Tk.__init__(self, parent)
        self.parent = parent
        self.fig = None
        self.map = None
        self.axes = None
        self.polygon_border = "black"


    def init_plot(self):

        self.fig = plot.figure()
        self.map = Basemap()
        self.axes = Axes3D(self.fig)
        self.axes.azim = 270
        self.axes.elev = 50
        self.axes.dist = 10
        self.axes.add_collection3d(self.map.drawcoastlines(linewidth = 0.25))
        self.axes.add_collection3d(self.map.drawcountries(linewidth = 0.35))
        self.axes.add_collection3d(self.map.drawstates(linewidth = 0.1))

        self.create_polygons()
        self.fig = self.axes.get_figure()

        #uncommenting the below line allows for rotation inside of tkitner albeit very slowly and somewhat crudely
        #plot.show()

   def init_frame(self):

        self.frame = tk.Frame(self)
        self.frame.pack()
        self.canvas = FigureCanvasTkAgg(self.fig, master = self.frame)
        self.canvas.show()
        #Axes3D.mouse_init(self.fig)
        self.canvas.get_tk_widget().pack(fill = "both", expand = True)
        self.toolbar = NavigationToolbar2TkAgg(self.canvas, self)
        self.toolbar.update()
        self.toolbar.pack()
        self.canvas._tkcanvas.pack(side = tk.TOP, fill = tk.BOTH, 
                                    expand = True)

最佳答案

以下是我最终的解决方案,采用简化形式。我的结构存在问题,其中一些是 self.canvas ,需要在 3D 轴添加到 Canvas 之前创建它,否则会导致 Axes3D.mouse_init > 由于找不到 Canvas 而不允许旋转的错误。我假设这就是为什么注释掉的冗余行在未注释时允许旋转。

此外,您需要使用 pyplot 中的 plot 而不是 figure,否则将显示 Basemap 中的多边形错误。

不能 100% 确定上述所有内容是否准确,但这是我在尝试和测试后发现的。

from mpl_toolkits.mplot3d import Axes3D #For plotting 3D Axes
from mpl_toolkits.basemap import Basemap #For plotting polygonal surfaces
from matplotlib.collections import PolyCollection #Polygons
import matplotlib.pyplot as plot
import matplotlib, sys     
matplotlib.use("TkAgg")
from matplotlib.backnds.backend_tkagg import FigureCanvasTkAgg,\
    NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import tkinter as tk


root = tk.Tk()
f = plot.figure(figsize = (5, 5), dpi = 100)
canvas = FigureCanvasTkAgg(f, master = root)
axes = Axes3D(f)

map = Basemap()

axes.add_collection3d(map.drawcoastlines(linewidth = 0.3, color = "white"))
axes.add_collection3d(map.drawcountries(linewidth = 0.3, color = "white"))
axes.add_collection3d(map.drawstates(linewidth = 0.3, color = "white"))


polygons = []
for polygon in map.landpolygons:
    polygons.append(polygon.get_coords())
    collection = PolyCollection(polygons, edgecolor = "white",
                               facecolor = "black", 
                            closed = True)

axes.add_collection3d(collection)


canvas.show()
canvas.get_tk_widget().pack(side = tk.BOTTOM, fill = tk.BOTH, expand = 1)
canvas._tkcanvas.pack(side = tk.BOTTOM, fill = tk.BOTH, expand = 1)
button = tk.Button(master = root, text = "Exit", command = sys.exit)
button.pack(side = tk.TOP)

tk.mainloop()

关于python - 旋转嵌入的 matplotlib 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30330912/

相关文章:

java - 有人可以帮我解决这个 JAVA SAXParser 吗?

python - 需要 int 的最大值

python - 我如何通过单行或命令行选项获得我的 Python 解释器的详细版本?

python - Pandas DF 列标题与列不对齐

Python 3.7 : How to avoid stackoverflow for this recursive approach?

python - 使用 matplotlib python 将表与 x 轴对齐

python - 是否可以在Python中使用Combobox更改FigureCanvas?

Python 认为我传递的参数比我传递的要多?

python - python 中 stub 文件 (.pyi) 有什么用?

python-3.x - MovieWriter (ffmpeg) 不可用 PyCharm (Windows)