matplotlib 和 PyQt 绘制曲面

发布于 2024-12-12 02:02:29 字数 1289 浏览 0 评论 0原文

我使用 matplotlib 创建了一个 3D 绘图,如下所示:

fig = pylab.figure()
ax = Axes3D( fig )
surf = ax.plot_surface( X, Y, Z, cmap=cm.gray_r, linewidth=0, antialiased=True )
fig.canvas.set_window_title( "Distance" )
pylab.show()

太棒了:我看到灰度的表面,我可以与它交互(转动表面,移动绘图,...)

现在我需要将此绘图放入 PyQt 形式。我创建了一个继承自 QMainWindow 的类,这样做:

class ViewerForm(QMainWindow):
   def __init__(self, p_parent=None, p_data=None):
      QMainWindow.__init__( self, parent=p_parent )

      self.main_frame = QWidget( )
      self.figure = pyplot.figure()
      self.axis = Axes3D( self.figure )
      self.canvas = FigureCanvas( self.figure )
      self.canvas.setParent( self.main_frame )
      self.mpl_toolbar = NavigationToolbar( self.canvas, self.main_frame )

      self.X, self.Y = np.meshgrid( p_data[ "axis_x" ], p_data[ "axis_y" ] )
      self.Z = p_data[ "values_z" ]

      self.surface = self.axis.plot_surface( self.X, self.Y, self.Z, cmap=cm.gray, linewidth=0, antialiased=True )

      vbox = QVBoxLayout( )
      vbox.addWidget( self.canvas )
      vbox.addWidget( self.mpl_toolbar )
      self.main_frame.setLayout( vbox )

      self.setCentralWidget( self.main_frame )

当我显示此表单时,我可以看到灰度表面作为第一个图,但我无法与之交互:我无法用鼠标单击移动表面。任何人都可以告诉我我做错了什么或者我误解了什么?

I created a 3D plot using matplotlib as this:

fig = pylab.figure()
ax = Axes3D( fig )
surf = ax.plot_surface( X, Y, Z, cmap=cm.gray_r, linewidth=0, antialiased=True )
fig.canvas.set_window_title( "Distance" )
pylab.show()

It's fantastic: I see the surface in grays-cale and I can interact with it ( turn the surface, move plot, ... )

Now I need to put this plot in a PyQt form. I created a class inherit from QMainWindow doing this:

class ViewerForm(QMainWindow):
   def __init__(self, p_parent=None, p_data=None):
      QMainWindow.__init__( self, parent=p_parent )

      self.main_frame = QWidget( )
      self.figure = pyplot.figure()
      self.axis = Axes3D( self.figure )
      self.canvas = FigureCanvas( self.figure )
      self.canvas.setParent( self.main_frame )
      self.mpl_toolbar = NavigationToolbar( self.canvas, self.main_frame )

      self.X, self.Y = np.meshgrid( p_data[ "axis_x" ], p_data[ "axis_y" ] )
      self.Z = p_data[ "values_z" ]

      self.surface = self.axis.plot_surface( self.X, self.Y, self.Z, cmap=cm.gray, linewidth=0, antialiased=True )

      vbox = QVBoxLayout( )
      vbox.addWidget( self.canvas )
      vbox.addWidget( self.mpl_toolbar )
      self.main_frame.setLayout( vbox )

      self.setCentralWidget( self.main_frame )

When I show this form I can see the surface in gray-scale as the first plot but I can't interact with this: I can't move the surface clicking with mouse. Anyone can say me what I'm doing wrong or what I misunderstood?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

芸娘子的小脾气 2024-12-19 02:02:29

我以前也遇到过这个,这让我很困惑。关于鼠标点击,Zhenya 的说法是正确的。

默认情况下,清除/重新绘制轴时,鼠标对绘图的控制将关闭。我不太清楚为什么会这样,但您需要再次启动鼠标交互:

self.surface = self.axis.plot_surface( self.X, self.Y, self.Z, cmap=cm.gray, linewidth=0, antialiased=True )
self.axis.mouse_init()

I had this before and it confused me a lot. Zhenya is right about the mouse clicking.

By default, control of the plot by mouse is turned off when the axis is cleared/replotted. I have no idea quite why this would be so, but you need to initiate mouse interactions again:

self.surface = self.axis.plot_surface( self.X, self.Y, self.Z, cmap=cm.gray, linewidth=0, antialiased=True )
self.axis.mouse_init()
一个人的夜不怕黑 2024-12-19 02:02:29

我认为您需要为鼠标点击注册一个回调函数,就像在这个食谱示例中一样: http:// /www.scipy.org/Cookbook/Matplotlib/Interactive_Plotting

I think you need to register a callback function for the mouse clicks, like in this cookbook example: http://www.scipy.org/Cookbook/Matplotlib/Interactive_Plotting

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文