Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd5edcf

Browse files
committed
Merge pull request #3652 from fariza/navigation-by-events
MEP22: Navigation by events
2 parents 6e3471d + 5eae4e1 commit cd5edcf

12 files changed

+2233
-27
lines changed

doc/api/backend_managers_api.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
:mod:`matplotlib.backend_managers`
3+
===================================
4+
5+
.. automodule:: matplotlib.backend_managers
6+
:members:
7+
:undoc-members:
8+
:show-inheritance:

doc/api/backend_tools_api.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
:mod:`matplotlib.backend_tools`
3+
================================
4+
5+
.. automodule:: matplotlib.backend_tools
6+
:members:
7+
:undoc-members:
8+
:show-inheritance:

doc/api/index_backend_api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ backends
55
.. toctree::
66

77
backend_bases_api.rst
8+
backend_managers_api.rst
9+
backend_tools_api.rst
810
backend_gtkagg_api.rst
911
backend_qt4agg_api.rst
1012
backend_wxagg_api.rst

doc/users/whats_new/rcparams.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ Added "figure.titlesize" and "figure.titleweight" keys to rcParams
1515
Two new keys were added to rcParams to control the default font size and weight
1616
used by the figure title (as emitted by ``pyplot.suptitle()``).
1717

18+
1819
``image.composite_image`` added to rcParams
1920
```````````````````````````````````````````
2021
Controls whether vector graphics backends (i.e. PDF, PS, and SVG) combine
2122
multiple images on a set of axes into a single composite image. Saving each
2223
image individually can be useful if you generate vector graphics files in
2324
matplotlib and then edit the files further in Inkscape or other programs.
25+
26+
27+
Added "toolmanager" to "toolbar" possible values
28+
````````````````````````````````````````````````
29+
30+
The new value enables the use of ``ToolManager``
31+

doc/users/whats_new/toolmanager.rst

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
ToolManager
2+
-----------
3+
4+
Federico Ariza wrote the new `matplotlib.backend_managers.ToolManager` that comes as replacement for `NavigationToolbar2`
5+
6+
`ToolManager` offers a new way of looking at the user interactions with the figures.
7+
Before we had the `NavigationToolbar2` with its own tools like `zoom/pan/home/save/...` and also we had the shortcuts like
8+
`yscale/grid/quit/....`
9+
`Toolmanager` relocate all those actions as `Tools` (located in `matplotlib.backend_tools`), and defines a way to `access/trigger/reconfigure` them.
10+
11+
The `Toolbars` are replaced for `ToolContainers` that are just GUI interfaces to `trigger` the tools. But don't worry the default backends include a `ToolContainer` called `toolbar`
12+
13+
14+
.. note::
15+
For the moment the `ToolManager` is working only with `GTK3` and `Tk` backends.
16+
Make sure you are using one of those.
17+
Port for the rest of the backends is comming soon.
18+
19+
To activate the `ToolManager` include the following at the top of your file:
20+
21+
>>> matplotlib.rcParams['toolbar'] = 'toolmanager'
22+
23+
24+
Interact with the ToolContainer
25+
```````````````````````````````
26+
27+
The most important feature is the ability to easily reconfigure the ToolContainer (aka toolbar).
28+
For example, if we want to remove the "forward" button we would just do.
29+
30+
>>> fig.canvas.manager.toolmanager.remove_tool('forward')
31+
32+
Now if you want to programmatically trigger the "home" button
33+
34+
>>> fig.canvas.manager.toolmanager.trigger_tool('home')
35+
36+
37+
New Tools
38+
`````````
39+
40+
It is possible to add new tools to the ToolManager
41+
42+
A very simple tool that prints "You're awesome" would be::
43+
44+
from matplotlib.backend_tools import ToolBase
45+
class AwesomeTool(ToolBase):
46+
def trigger(self, *args, **kwargs):
47+
print("You're awesome")
48+
49+
50+
To add this tool to `ToolManager`
51+
52+
>>> fig.canvas.manager.toolmanager.add_tool('Awesome', AwesomeTool)
53+
54+
If we want to add a shortcut ("d") for the tool
55+
56+
>>> fig.canvas.manager.toolmanager.update_keymap('Awesome', 'd')
57+
58+
59+
To add it to the toolbar inside the group 'foo'
60+
61+
>>> fig.canvas.manager.toolbar.add_tool('Awesome', 'foo')
62+
63+
64+
There is a second class of tools, "Toggleable Tools", this are almost the same as our basic tools, just that belong to a group, and are mutually exclusive inside that group.
65+
For tools derived from `ToolToggleBase` there are two basic methods `enable` and `disable` that are called automatically whenever it is toggled.
66+
67+
68+
A full example is located in :ref:`user_interfaces-toolmanager`
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
'''This example demonstrates how to:
2+
* Modify the Toolbar
3+
* Create tools
4+
* Add tools
5+
* Remove tools
6+
Using `matplotlib.backend_managers.ToolManager`
7+
'''
8+
9+
10+
from __future__ import print_function
11+
import matplotlib
12+
matplotlib.use('GTK3Cairo')
13+
matplotlib.rcParams['toolbar'] = 'toolmanager'
14+
import matplotlib.pyplot as plt
15+
from matplotlib.backend_tools import ToolBase, ToolToggleBase
16+
from gi.repository import Gtk, Gdk
17+
18+
19+
class ListTools(ToolBase):
20+
'''List all the tools controlled by the `ToolManager`'''
21+
# keyboard shortcut
22+
default_keymap = 'm'
23+
description = 'List Tools'
24+
25+
def trigger(self, *args, **kwargs):
26+
print('_' * 80)
27+
print("{0:12} {1:45} {2}".format('Name (id)',
28+
'Tool description',
29+
'Keymap'))
30+
print('-' * 80)
31+
tools = self.toolmanager.tools
32+
for name in sorted(tools.keys()):
33+
if not tools[name].description:
34+
continue
35+
keys = ', '.join(sorted(self.toolmanager.get_tool_keymap(name)))
36+
print("{0:12} {1:45} {2}".format(name,
37+
tools[name].description,
38+
keys))
39+
print('_' * 80)
40+
print("Active Toggle tools")
41+
print("{0:12} {1:45}".format("Group", "Active"))
42+
print('-' * 80)
43+
for group, active in self.toolmanager.active_toggle.items():
44+
print("{0:12} {1:45}".format(group, active))
45+
46+
47+
class GroupHideTool(ToolToggleBase):
48+
'''Hide lines with a given gid'''
49+
default_keymap = 'G'
50+
description = 'Hide by gid'
51+
52+
def __init__(self, *args, **kwargs):
53+
self.gid = kwargs.pop('gid')
54+
ToolToggleBase.__init__(self, *args, **kwargs)
55+
56+
def enable(self, *args):
57+
self.set_lines_visibility(False)
58+
59+
def disable(self, *args):
60+
self.set_lines_visibility(True)
61+
62+
def set_lines_visibility(self, state):
63+
gr_lines = []
64+
for ax in self.figure.get_axes():
65+
for line in ax.get_lines():
66+
if line.get_gid() == self.gid:
67+
line.set_visible(state)
68+
self.figure.canvas.draw()
69+
70+
71+
fig = plt.figure()
72+
plt.plot([1, 2, 3], gid='mygroup')
73+
plt.plot([2, 3, 4], gid='unknown')
74+
plt.plot([3, 2, 1], gid='mygroup')
75+
76+
# Add the custom tools that we created
77+
fig.canvas.manager.toolmanager.add_tool('List', ListTools)
78+
fig.canvas.manager.toolmanager.add_tool('Hide', GroupHideTool, gid='mygroup')
79+
80+
81+
# Add an existing tool to new group `foo`.
82+
# It can be added as many times as we want
83+
fig.canvas.manager.toolbar.add_tool('zoom', 'foo')
84+
85+
# Remove the forward button
86+
fig.canvas.manager.toolmanager.remove_tool('forward')
87+
88+
# To add a custom tool to the toolbar at specific location inside
89+
# the navigation group
90+
fig.canvas.manager.toolbar.add_tool('Hide', 'navigation', 1)
91+
92+
plt.show()

0 commit comments

Comments
 (0)