14
14
15
15
from matplotlib import rcParams
16
16
from matplotlib ._pylab_helpers import Gcf
17
+ from matplotlib .table import Table
18
+ import textwrap
17
19
import matplotlib .cbook as cbook
18
20
from weakref import WeakKeyDictionary
19
21
import six
@@ -1018,6 +1020,68 @@ def _mouse_move(self, event):
1018
1020
self .toolmanager .canvas .draw_idle ()
1019
1021
1020
1022
1023
+ class HelpTool (ToolToggleBase ):
1024
+ description = 'Print tool list, shortcuts and description'
1025
+ default_keymap = rcParams ['keymap.help' ]
1026
+ image = 'help.png'
1027
+
1028
+ def __init__ (self , * args ):
1029
+ ToolToggleBase .__init__ (self , * args )
1030
+ self .text_axes = None
1031
+
1032
+ def enable (self , * args ):
1033
+ self .text_axes = self .figure .add_axes ((0 , 0 , 1 , 1 ))
1034
+ table = Table (self .text_axes , bbox = [0 , 0 , 1 , 1 ])
1035
+ table .edges = 'B'
1036
+ self .text_axes .add_table (table )
1037
+ chars_in_width = self ._find_chars_in_width (table .FONTSIZE )
1038
+
1039
+ table .auto_set_font_size (False )
1040
+ col_chars_width = int (chars_in_width / 4 ) - 2
1041
+ content = self ._get_content (col_chars_width , col_chars_width ,
1042
+ 2 * col_chars_width )
1043
+ for i , v in enumerate (content ):
1044
+ h = v [0 ]
1045
+ table .add_cell (i , 0 , text = v [1 ], width = 1 , height = h , loc = 'left' ,
1046
+ fontproperties = 'monospace' )
1047
+ table .add_cell (i , 1 , text = v [2 ], width = 1 , height = h , loc = 'left' ,
1048
+ fontproperties = 'monospace' )
1049
+ table .add_cell (i , 2 , text = v [3 ], width = 2 , height = h , loc = 'left' ,
1050
+ fontproperties = 'monospace' )
1051
+ self .figure .canvas .draw_idle ()
1052
+
1053
+ def _find_chars_in_width (self , fontsize ):
1054
+ """Number of characters in figure width approx"""
1055
+ # https://web.archive.org/web/20010717031241/plainlanguagenetwork.org/type/utbo211.htmhttps://web.archive.org/web/20010717031241/plainlanguagenetwork.org/type/utbo211.htm
1056
+ # Approximately width = 60% of height for monospace fonts
1057
+ charwidth = 0.6 * fontsize / 72.0 * self .figure .dpi
1058
+ figwidth = self .figure .get_figwidth () * self .figure .dpi
1059
+ return figwidth / charwidth
1060
+
1061
+ def disable (self , * args ):
1062
+ self .text_axes .remove ()
1063
+ self .figure .canvas .draw_idle ()
1064
+
1065
+ def _get_content (self , w0 , w1 , w2 ):
1066
+ rows = [(1 , 'NAME' , 'KEYS' , 'DESCRIPTION' )]
1067
+ tools = self .toolmanager .tools
1068
+ for name in sorted (tools ):
1069
+ if not tools [name ].description :
1070
+ continue
1071
+
1072
+ keys = ', ' .join (sorted (self .toolmanager .get_tool_keymap (name )))
1073
+ name_lines = textwrap .wrap (name , w0 )
1074
+ keys_lines = textwrap .wrap (keys , w1 )
1075
+ desc_lines = textwrap .wrap (tools [name ].description , w2 )
1076
+ # Height of the row is the maximum number of lines
1077
+ height = max (len (name_lines ), len (keys_lines ), len (desc_lines ))
1078
+ rows .append ((height ,
1079
+ '\n ' .join (name_lines ),
1080
+ '\n ' .join (keys_lines ),
1081
+ '\n ' .join (desc_lines )))
1082
+ return rows
1083
+
1084
+
1021
1085
default_tools = {'home' : ToolHome , 'back' : ToolBack , 'forward' : ToolForward ,
1022
1086
'zoom' : ToolZoom , 'pan' : ToolPan ,
1023
1087
'subplots' : 'ToolConfigureSubplots' ,
@@ -1035,12 +1099,13 @@ def _mouse_move(self, event):
1035
1099
_views_positions : ToolViewsPositions ,
1036
1100
'cursor' : 'ToolSetCursor' ,
1037
1101
'rubberband' : 'ToolRubberband' ,
1102
+ 'help' : HelpTool
1038
1103
}
1039
1104
"""Default tools"""
1040
1105
1041
1106
default_toolbar_tools = [['navigation' , ['home' , 'back' , 'forward' ]],
1042
1107
['zoompan' , ['pan' , 'zoom' , 'subplots' ]],
1043
- ['io' , ['save' ]]]
1108
+ ['io' , ['save' , 'help' ]]]
1044
1109
"""Default tools in the toolbar"""
1045
1110
1046
1111
0 commit comments