【问题标题】:How to use mouse to rotate matplotlib 3D plots in wxPython?如何使用鼠标在 wxPython 中旋转 matplotlib 3D 图?
【发布时间】:2015-02-06 21:41:11
【问题描述】:

这是我用来在 wxPython 应用程序中显示 2D matplotlib 图的 sn-p 代码:

import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
from matplotlib.figure import Figure
import wx

[wxPython application and class code snipped]

    figure = Figure()
    axes = figure.add_subplot(111)
    canvas = FigureCanvasWxAgg(self, wx.ID_ANY, figure)
    plotSizer = wx.BoxSizer(wx.VERTICAL)
    plotSizer.Add(self, canvas, proportion=1, flag=wx.EXPAND)
    plotPanel = wx.Panel(self, wx.ID_ANY, size=DEFAULT_PLOT_SIZE)
    plotPanel.SetSizer(plotSizer)

我可以绘制坐标轴、重绘画布以及平移和缩放。当我尝试使用 3D 进行等效操作时,会显示 3D 绘图,但我无法旋转/平移/缩放。这段代码的唯一区别是额外的 3D 导入和向 add_subplot() 添加投影参数。

import matplotlib
matplotlib.use('WXAgg')
from mpl_toolkits.mplot3d import axes3d
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
from matplotlib.figure import Figure
import wx

[wxPython application and class code snipped]

    figure = Figure()
    axes = figure.add_subplot(111, projection="3d")
    canvas = FigureCanvasWxAgg(self, wx.ID_ANY, figure)
    plotSizer = wx.BoxSizer(wx.VERTICAL)
    plotSizer.Add(self, canvas, proportion=1, flag=wx.EXPAND)
    plotPanel = wx.Panel(self, wx.ID_ANY, size=DEFAULT_PLOT_SIZE)
    plotPanel.SetSizer(plotSizer)

我收到此警告:

...\site-packages\mpl_toolkits\mplot3d\axes3d.py:1009: UserWarning: Axes3D.figure.canvas is 'None', mouse rotation disabled.  Set canvas then call Axes3D.mouse_init().

所以我尝试在调用 FigureCanvasWxAgg() 后使用此代码设置 Axes3D.figure.canvas:

axes.figure.canvas = canvas
axes.mouse_init()

但这不起作用;我仍然无法使用鼠标旋转 3D 绘图。

http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html 的演示源代码使用独立的 matplotlib 工作;我可以用鼠标来旋转那里的地块。使用 wxPython 时如何让鼠标旋转工作?

【问题讨论】:

  • 你能提供一个小的可运行样本吗?

标签: python matplotlib wxpython


【解决方案1】:

原来我只需要交换画布和轴的创建顺序。应该先创建画布并将其添加到图形中,然后才能创建 3D 轴。

figure = Figure()
canvas = FigureCanvasWxAgg(self, wx.ID_ANY, figure)
axes = figure.add_subplot(111, projection="3d")
plotSizer = wx.BoxSizer(wx.VERTICAL)
plotSizer.Add(self, canvas, proportion=1, flag=wx.EXPAND)
plotPanel = wx.Panel(self, wx.ID_ANY, size=DEFAULT_PLOT_SIZE)
plotPanel.SetSizer(plotSizer)

【讨论】:

  • 你真的应该在画布之前实例化面板并将画布添加到面板中:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-01
  • 2012-02-20
相关资源
最近更新 更多