12
12
matplotlib .use ('GTK3Cairo' )
13
13
matplotlib .rcParams ['toolbar' ] = 'toolmanager'
14
14
import matplotlib .pyplot as plt
15
- from matplotlib .backend_tools import ToolBase
15
+ from matplotlib .backend_tools import ToolBase , ToolToggleBase
16
16
from gi .repository import Gtk , Gdk
17
17
18
18
@@ -44,26 +44,39 @@ def trigger(self, *args, **kwargs):
44
44
print ("{0:12} {1:45}" .format (group , active ))
45
45
46
46
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'
52
51
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 ()
59
69
60
70
61
71
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' )
63
75
64
76
# Add the custom tools that we created
65
77
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
+
67
80
68
81
# Add an existing tool to new group `foo`.
69
82
# It can be added as many times as we want
@@ -74,6 +87,6 @@ def trigger(self, *args, **kwargs):
74
87
75
88
# To add a custom tool to the toolbar at specific location inside
76
89
# the navigation group
77
- fig .canvas .manager .toolbar .add_tool ('List ' , 'navigation' , 1 )
90
+ fig .canvas .manager .toolbar .add_tool ('Hide ' , 'navigation' , 1 )
78
91
79
92
plt .show ()
0 commit comments