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

查看:1078
本文介绍了如何在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(<a Button object>);
       tbar.remove_button(a reference to a button);
       tbar.edit_button(a reference to a 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天全站免登陆