1212matplotlib .use ('GTK3Cairo' )
1313matplotlib .rcParams ['toolbar' ] = 'toolmanager'
1414import matplotlib .pyplot as plt
15- from matplotlib .backend_tools import ToolBase
15+ from matplotlib .backend_tools import ToolBase , ToolToggleBase
1616from 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
6171fig = 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
6577fig .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
7992plt .show ()
0 commit comments