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

Skip to content

Commit def3a52

Browse files
committed
rename 2 -> to, example without gtk only code
1 parent f09b9ef commit def3a52

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
lines changed

examples/user_interfaces/toolmanager.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
matplotlib.use('GTK3Cairo')
1313
matplotlib.rcParams['toolbar'] = 'toolmanager'
1414
import matplotlib.pyplot as plt
15-
from matplotlib.backend_tools import ToolBase
15+
from matplotlib.backend_tools import ToolBase, ToolToggleBase
1616
from gi.repository import Gtk, Gdk
1717

1818

@@ -44,26 +44,39 @@ def trigger(self, *args, **kwargs):
4444
print("{0:12} {1:45}".format(group, active))
4545

4646

47-
# ref: at https://github.com/matplotlib/matplotlib/issues/1987
48-
class CopyToolGTK3(ToolBase):
49-
'''Copy canvas to clipboard'''
50-
default_keymap = 'ctrl+c'
51-
description = 'Copy canvas'
47+
class GroupHideTool(ToolToggleBase):
48+
'''Hide lines with a given gid'''
49+
default_keymap = 'G'
50+
description = 'Hide by gid'
5251

53-
def trigger(self, *args, **kwargs):
54-
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
55-
window = self.figure.canvas.get_window()
56-
x, y, width, height = window.get_geometry()
57-
pb = Gdk.pixbuf_get_from_window(window, x, y, width, height)
58-
clipboard.set_image(pb)
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()
5969

6070

6171
fig = plt.figure()
62-
plt.plot([1, 2, 3])
72+
plt.plot([1, 2, 3], gid='mygroup')
73+
plt.plot([2, 3, 4], gid='unknown')
74+
plt.plot([3, 2, 1], gid='mygroup')
6375

6476
# Add the custom tools that we created
6577
fig.canvas.manager.toolmanager.add_tool('List', ListTools)
66-
fig.canvas.manager.toolmanager.add_tool('copy', CopyToolGTK3)
78+
fig.canvas.manager.toolmanager.add_tool('Hide', GroupHideTool, gid='mygroup')
79+
6780

6881
# Add an existing tool to new group `foo`.
6982
# It can be added as many times as we want
@@ -74,6 +87,6 @@ def trigger(self, *args, **kwargs):
7487

7588
# To add a custom tool to the toolbar at specific location inside
7689
# the navigation group
77-
fig.canvas.manager.toolbar.add_tool('List', 'navigation', 1)
90+
fig.canvas.manager.toolbar.add_tool('Hide', 'navigation', 1)
7891

7992
plt.show()

lib/matplotlib/backend_tools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ def _mouse_move(self, event):
954954
"""Default tools in the toolbar"""
955955

956956

957-
def add_tools_2_toolmanager(toolmanager, tools=default_tools):
957+
def add_tools_to_manager(toolmanager, tools=default_tools):
958958
"""
959959
Add multiple tools to `ToolManager`
960960
@@ -971,7 +971,7 @@ def add_tools_2_toolmanager(toolmanager, tools=default_tools):
971971
toolmanager.add_tool(name, tool)
972972

973973

974-
def add_tools_2_container(container, tools=default_toolbar_tools):
974+
def add_tools_to_container(container, tools=default_toolbar_tools):
975975
"""
976976
Add multiple tools to the container.
977977

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,8 @@ def add_widget(child, expand, fill, padding):
432432
return size_request.height
433433

434434
if matplotlib.rcParams['toolbar'] == 'toolmanager':
435-
backend_tools.add_tools_2_toolmanager(self.toolmanager)
436-
backend_tools.add_tools_2_container(self.toolbar)
435+
backend_tools.add_tools_to_manager(self.toolmanager)
436+
backend_tools.add_tools_to_container(self.toolbar)
437437
self.statusbar = StatusbarGTK3(self.toolmanager)
438438
h += add_widget(self.statusbar, False, False, 0)
439439
h += add_widget(Gtk.HSeparator(), False, False, 0)

lib/matplotlib/backends/backend_tkagg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,8 @@ def __init__(self, canvas, num, window):
540540
self.statusbar = None
541541

542542
if matplotlib.rcParams['toolbar'] == 'toolmanager':
543-
backend_tools.add_tools_2_toolmanager(self.toolmanager)
544-
backend_tools.add_tools_2_container(self.toolbar)
543+
backend_tools.add_tools_to_manager(self.toolmanager)
544+
backend_tools.add_tools_to_container(self.toolbar)
545545
self.statusbar = StatusbarTk(self.window, self.toolmanager)
546546

547547
self._shown = False

0 commit comments

Comments
 (0)