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

Skip to content

Commit 6fa0fd1

Browse files
committed
Add keymap (default: G) to toggle minor grid.
If either minor grid is on, turn both of them off. Otherwise, turn all major and minor grids on (it usually doesn't make sense to have the minor grids only). Also change the behavior of `keymap.grid` ("g") to synchronize the grid state of both axes. Otherwise, if starting from only grids in one direction (`plt.plot(); plt.grid(axis="x")`), "g" switches between only `x` grid and only `y` grid, which is unlikely to be the expected behavior.
1 parent 0cfc7a8 commit 6fa0fd1

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

doc/users/navigation_toolbar.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ Close all plots **shift** + **w**
9999
Constrain pan/zoom to x axis hold **x** when panning/zooming with mouse
100100
Constrain pan/zoom to y axis hold **y** when panning/zooming with mouse
101101
Preserve aspect ratio hold **CONTROL** when panning/zooming with mouse
102-
Toggle grid **g** when mouse is over an axes
102+
Toggle major grid **g** when mouse is over an axes
103+
Toggle major and minor grid **G** when mouse is over an axes
103104
Toggle x axis scale (log/linear) **L** or **k** when mouse is over an axes
104105
Toggle y axis scale (log/linear) **l** when mouse is over an axes
105106
================================== =================================================

lib/matplotlib/backend_bases.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2482,6 +2482,7 @@ def key_press_handler(event, canvas, toolbar=None):
24822482
save_keys = rcParams['keymap.save']
24832483
quit_keys = rcParams['keymap.quit']
24842484
grid_keys = rcParams['keymap.grid']
2485+
grid_minor_keys = rcParams['keymap.grid_minor']
24852486
toggle_yscale_keys = rcParams['keymap.yscale']
24862487
toggle_xscale_keys = rcParams['keymap.xscale']
24872488
all = rcParams['keymap.all_axes']
@@ -2525,13 +2526,28 @@ def key_press_handler(event, canvas, toolbar=None):
25252526

25262527
# these bindings require the mouse to be over an axes to trigger
25272528

2529+
ax = event.inaxes
25282530
# switching on/off a grid in current axes (default key 'g')
25292531
if event.key in grid_keys:
2530-
event.inaxes.grid()
2532+
# If either major grid is on, turn all major and minor grids off.
2533+
if any(tick.gridOn
2534+
for tick in ax.xaxis.majorTicks + ax.yaxis.majorTicks):
2535+
ax.grid(False, which="both")
2536+
# Otherwise, turn the major grids on.
2537+
else:
2538+
ax.grid(True)
2539+
canvas.draw()
2540+
if event.key in grid_minor_keys:
2541+
# If either minor grid is on, turn all minor grids off.
2542+
if any(tick.gridOn
2543+
for tick in ax.xaxis.minorTicks + ax.yaxis.minorTicks):
2544+
ax.grid(False, which="minor")
2545+
# Otherwise, turn all major and minor grids on.
2546+
else:
2547+
ax.grid(True, which="both")
25312548
canvas.draw()
25322549
# toggle scaling of y-axes between 'log and 'linear' (default key 'l')
25332550
elif event.key in toggle_yscale_keys:
2534-
ax = event.inaxes
25352551
scale = ax.get_yscale()
25362552
if scale == 'log':
25372553
ax.set_yscale('linear')
@@ -2541,7 +2557,6 @@ def key_press_handler(event, canvas, toolbar=None):
25412557
ax.figure.canvas.draw()
25422558
# toggle scaling of x-axes between 'log and 'linear' (default key 'k')
25432559
elif event.key in toggle_xscale_keys:
2544-
ax = event.inaxes
25452560
scalex = ax.get_xscale()
25462561
if scalex == 'log':
25472562
ax.set_xscale('linear')

lib/matplotlib/rcsetup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,7 @@ def validate_animation_writer_path(p):
13131313
'keymap.quit': [['ctrl+w', 'cmd+w', 'q'], validate_stringlist],
13141314
'keymap.quit_all': [['W', 'cmd+W', 'Q'], validate_stringlist],
13151315
'keymap.grid': [['g'], validate_stringlist],
1316+
'keymap.grid_both': [['G'], validate_stringlist],
13161317
'keymap.yscale': [['l'], validate_stringlist],
13171318
'keymap.xscale': [['k', 'L'], validate_stringlist],
13181319
'keymap.all_axes': [['a'], validate_stringlist],

matplotlibrc.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ backend : $TEMPLATE_BACKEND
585585
#keymap.save : s # saving current figure
586586
#keymap.quit : ctrl+w, cmd+w # close the current figure
587587
#keymap.grid : g # switching on/off a grid in current axes
588+
#keymap.grid_minor : G # switching on/off a grid in current axes
588589
#keymap.yscale : l # toggle scaling of y-axes ('log'/'linear')
589590
#keymap.xscale : L, k # toggle scaling of x-axes ('log'/'linear')
590591
#keymap.all_axes : a # enable all axes

0 commit comments

Comments
 (0)