如何在 matplotlib 图形窗口中轻松修改导航工具栏? [英] How to modify the navigation toolbar easily in a matplotlib figure window?

查看:104
本文介绍了如何在 matplotlib 图形窗口中轻松修改导航工具栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以做如下类似的事情来修改 matplotlib 中的导航工具栏?

Is it possible to do something like the following to modify the navigation toolbar in matplotlib?

  1. 生成图形窗口,使用:fig = figure()
  2. 获取导航工具栏的引用,使用:tbar = fig.get_navigation_toolbar(),或者更好的是,只需:tbar = fig.navtbar
  3. 通过参考tbar修改工具栏,例如删除/添加/编辑一个按钮,如下所示:
      tbar.add_button();
      tbar.remove_button(对按钮的引用);
      tbar.edit_button(对按钮的引用);
  4. 更新图形:fig.canvas.draw()
  1. Generate a figure window, with: fig = figure()
  2. Get a reference of the navigation tool-bar, with: tbar = fig.get_navigation_toolbar(), or better yet, just by: tbar = fig.navtbar
  3. Modify the tool-bar through the reference tbar, such as delete/add/edit a button with something like this:
       tbar.add_button(<a Button object>);
       tbar.remove_button(a reference to a button);
       tbar.edit_button(a reference to a button);
  4. Update the figure with: fig.canvas.draw()

非常感谢.

推荐答案

我发现删除不需要的工具栏项目的方法是创建一个子类,它被实例化并在 GTK 应用程序中使用.无论如何,由于我手动创建了 Figure、FigureCanvas 和 NavigationToolbar 对象,这是最简单的方法.

The way I found to remove unwanted toolbar items is making a subclass, which is instantiated and used in a GTK application. As I manually create Figure, FigureCanvas and NavigationToolbar objects anyway, this was the easiest way.

class NavigationToolbar(NavigationToolbar2GTKAgg):
    # only display the buttons we need
    toolitems = [t for t in NavigationToolbar2GTKAgg.toolitems if
                 t[0] in ('Home', 'Pan', 'Zoom', 'Save')]

如果您想创建自定义按钮,您应该查看 backend_bases 中 NavigationToolbar2 的定义.您可以轻松地将自己的条目添加到 toolitems 列表中,并在您的工具栏子类中定义适当的回调函数.

If you want to create custom buttons, you should take a look on the definition of NavigationToolbar2 in backend_bases. You can easily add your own entries to the toolitems list and define appropriate callback functions in your toolbar subclass.

这篇关于如何在 matplotlib 图形窗口中轻松修改导航工具栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆