问题描述
是否可以执行以下操作来修改matplotlib中的导航工具栏?
Is it possible to do something like the following to modify the navigation toolbar in matplotlib?
- 生成一个图形窗口,其中包含:
fig = figure()
- 使用以下内容获取导航工具栏的参考:
tbar = fig.get_navigation_toolbar()
,或更好,只是通过:tbar = fig.navtbar
- 通过参考
tbar
修改工具栏,例如使用以下内容删除/添加/编辑按钮:
tbar.add_button(<a Button object>);
tbar.remove_button(a reference to a button);
tbar.edit_button(a reference to a button);
- 通过以下方式更新图形:
fig.canvas.draw()
- Generate a figure window, with:
fig = figure()
- Get a reference of the navigation tool-bar, with:
tbar = fig.get_navigation_toolbar()
,or better yet, just by:tbar = fig.navtbar
- 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);
- 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图形窗口中轻松修改导航工具栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!