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

Skip to content

Commit b1bd86c

Browse files
anntzerissamarabi
authored andcommitted
Minor cleanups.
Re: the rewriting of AsinhLocator formulas: The previous implementation added 1e-6 to the logs in the case where xs=0 in order to avoid a warning with log(0), but the result of the log was then zeroed out by multiplication by xs anyways. Instead, here we just rely on `10**log(0) = 10**-inf = 0`.
1 parent eaab89f commit b1bd86c

File tree

7 files changed

+48
-96
lines changed

7 files changed

+48
-96
lines changed

lib/matplotlib/axis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def _apply_tickdir(self, tickdir):
208208
"""Set tick direction. Valid values are 'out', 'in', 'inout'."""
209209
# This method is responsible for updating `_pad`, and, in subclasses,
210210
# for setting the tick{1,2}line markers as well. From the user
211-
# perspective this should always be called though _apply_params, which
211+
# perspective this should always be called through _apply_params, which
212212
# further updates ticklabel positions using the new pads.
213213
if tickdir is None:
214214
tickdir = mpl.rcParams[f'{self.__name__}.direction']

lib/matplotlib/backend_bases.py

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,64 +2391,47 @@ def key_press_handler(event, canvas=None, toolbar=None):
23912391
back-compatibility, but, if set, should always be equal to
23922392
``event.canvas.toolbar``.
23932393
"""
2394-
# these bindings happen whether you are over an Axes or not
2395-
23962394
if event.key is None:
23972395
return
23982396
if canvas is None:
23992397
canvas = event.canvas
24002398
if toolbar is None:
24012399
toolbar = canvas.toolbar
24022400

2403-
# Load key-mappings from rcParams.
2404-
fullscreen_keys = rcParams['keymap.fullscreen']
2405-
home_keys = rcParams['keymap.home']
2406-
back_keys = rcParams['keymap.back']
2407-
forward_keys = rcParams['keymap.forward']
2408-
pan_keys = rcParams['keymap.pan']
2409-
zoom_keys = rcParams['keymap.zoom']
2410-
save_keys = rcParams['keymap.save']
2411-
quit_keys = rcParams['keymap.quit']
2412-
quit_all_keys = rcParams['keymap.quit_all']
2413-
grid_keys = rcParams['keymap.grid']
2414-
grid_minor_keys = rcParams['keymap.grid_minor']
2415-
toggle_yscale_keys = rcParams['keymap.yscale']
2416-
toggle_xscale_keys = rcParams['keymap.xscale']
2417-
2418-
# toggle fullscreen mode ('f', 'ctrl + f')
2419-
if event.key in fullscreen_keys:
2401+
# toggle fullscreen mode (default key 'f', 'ctrl + f')
2402+
if event.key in rcParams['keymap.fullscreen']:
24202403
try:
24212404
canvas.manager.full_screen_toggle()
24222405
except AttributeError:
24232406
pass
24242407

24252408
# quit the figure (default key 'ctrl+w')
2426-
if event.key in quit_keys:
2409+
if event.key in rcParams['keymap.quit']:
24272410
Gcf.destroy_fig(canvas.figure)
2428-
if event.key in quit_all_keys:
2411+
if event.key in rcParams['keymap.quit_all']:
24292412
Gcf.destroy_all()
24302413

24312414
if toolbar is not None:
24322415
# home or reset mnemonic (default key 'h', 'home' and 'r')
2433-
if event.key in home_keys:
2416+
if event.key in rcParams['keymap.home']:
24342417
toolbar.home()
24352418
# forward / backward keys to enable left handed quick navigation
24362419
# (default key for backward: 'left', 'backspace' and 'c')
2437-
elif event.key in back_keys:
2420+
elif event.key in rcParams['keymap.back']:
24382421
toolbar.back()
24392422
# (default key for forward: 'right' and 'v')
2440-
elif event.key in forward_keys:
2423+
elif event.key in rcParams['keymap.forward']:
24412424
toolbar.forward()
24422425
# pan mnemonic (default key 'p')
2443-
elif event.key in pan_keys:
2426+
elif event.key in rcParams['keymap.pan']:
24442427
toolbar.pan()
24452428
toolbar._update_cursor(event)
24462429
# zoom mnemonic (default key 'o')
2447-
elif event.key in zoom_keys:
2430+
elif event.key in rcParams['keymap.zoom']:
24482431
toolbar.zoom()
24492432
toolbar._update_cursor(event)
24502433
# saving current figure (default key 's')
2451-
elif event.key in save_keys:
2434+
elif event.key in rcParams['keymap.save']:
24522435
toolbar.save_figure()
24532436

24542437
if event.inaxes is None:
@@ -2458,19 +2441,16 @@ def key_press_handler(event, canvas=None, toolbar=None):
24582441
def _get_uniform_gridstate(ticks):
24592442
# Return True/False if all grid lines are on or off, None if they are
24602443
# not all in the same state.
2461-
if all(tick.gridline.get_visible() for tick in ticks):
2462-
return True
2463-
elif not any(tick.gridline.get_visible() for tick in ticks):
2464-
return False
2465-
else:
2466-
return None
2444+
return (True if all(tick.gridline.get_visible() for tick in ticks) else
2445+
False if not any(tick.gridline.get_visible() for tick in ticks) else
2446+
None)
24672447

24682448
ax = event.inaxes
24692449
# toggle major grids in current Axes (default key 'g')
24702450
# Both here and below (for 'G'), we do nothing if *any* grid (major or
24712451
# minor, x or y) is not in a uniform state, to avoid messing up user
24722452
# customization.
2473-
if (event.key in grid_keys
2453+
if (event.key in rcParams['keymap.grid']
24742454
# Exclude minor grids not in a uniform state.
24752455
and None not in [_get_uniform_gridstate(ax.xaxis.minorTicks),
24762456
_get_uniform_gridstate(ax.yaxis.minorTicks)]):
@@ -2489,7 +2469,7 @@ def _get_uniform_gridstate(ticks):
24892469
ax.grid(y_state, which="major" if y_state else "both", axis="y")
24902470
canvas.draw_idle()
24912471
# toggle major and minor grids in current Axes (default key 'G')
2492-
if (event.key in grid_minor_keys
2472+
if (event.key in rcParams['keymap.grid_minor']
24932473
# Exclude major grids not in a uniform state.
24942474
and None not in [_get_uniform_gridstate(ax.xaxis.majorTicks),
24952475
_get_uniform_gridstate(ax.yaxis.majorTicks)]):
@@ -2507,7 +2487,7 @@ def _get_uniform_gridstate(ticks):
25072487
ax.grid(y_state, which="both", axis="y")
25082488
canvas.draw_idle()
25092489
# toggle scaling of y-axes between 'log and 'linear' (default key 'l')
2510-
elif event.key in toggle_yscale_keys:
2490+
elif event.key in rcParams['keymap.yscale']:
25112491
scale = ax.get_yscale()
25122492
if scale == 'log':
25132493
ax.set_yscale('linear')
@@ -2520,7 +2500,7 @@ def _get_uniform_gridstate(ticks):
25202500
ax.set_yscale('linear')
25212501
ax.figure.canvas.draw_idle()
25222502
# toggle scaling of x-axes between 'log and 'linear' (default key 'k')
2523-
elif event.key in toggle_xscale_keys:
2503+
elif event.key in rcParams['keymap.xscale']:
25242504
scalex = ax.get_xscale()
25252505
if scalex == 'log':
25262506
ax.set_xscale('linear')

lib/matplotlib/font_manager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,8 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
14481448
# actually raised.
14491449
return _ExceptionProxy(
14501450
ValueError,
1451-
f"Failed to find font {prop}, and fallback to the default font was disabled"
1451+
f"Failed to find font {prop}, and fallback to the default font was "
1452+
f"disabled"
14521453
)
14531454
else:
14541455
_log.debug('findfont: Matching %s to %s (%r) with score of %f.',

lib/matplotlib/projections/polar.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -524,24 +524,16 @@ def get_matrix(self):
524524
if self._invalid:
525525
if self.mode == 'rlabel':
526526
angle = (
527-
np.deg2rad(self.axes.get_rlabel_position()) *
528-
self.axes.get_theta_direction() +
529-
self.axes.get_theta_offset()
527+
np.deg2rad(self.axes.get_rlabel_position()
528+
* self.axes.get_theta_direction())
529+
+ self.axes.get_theta_offset()
530+
- np.pi / 2
530531
)
531-
else:
532-
if self.mode == 'min':
533-
angle = self.axes._realViewLim.xmin
534-
elif self.mode == 'max':
535-
angle = self.axes._realViewLim.xmax
536-
537-
if self.mode in ('rlabel', 'min'):
538-
padx = np.cos(angle - np.pi / 2)
539-
pady = np.sin(angle - np.pi / 2)
540-
else:
541-
padx = np.cos(angle + np.pi / 2)
542-
pady = np.sin(angle + np.pi / 2)
543-
544-
self._t = (self.pad * padx / 72, self.pad * pady / 72)
532+
elif self.mode == 'min':
533+
angle = self.axes._realViewLim.xmin - np.pi / 2
534+
elif self.mode == 'max':
535+
angle = self.axes._realViewLim.xmax + np.pi / 2
536+
self._t = (self.pad * np.cos(angle) / 72, self.pad * np.sin(angle) / 72)
545537
return super().get_matrix()
546538

547539

lib/matplotlib/quiver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
of these components (in data or in screen space) depends on *angles*.
7373
7474
*U* and *V* must have the same number of elements, matching the number of
75-
arrow locations in *X*, *Y*. *U* and *V* may be masked. Locations masked
75+
arrow locations in *X*, *Y*. *U* and *V* may be masked. Locations masked
7676
in any of *U*, *V*, and *C* will not be drawn.
7777
7878
C : 1D or 2D array-like, optional
@@ -90,7 +90,7 @@
9090
symbolize a quantity that is not based on *X*, *Y* data coordinates.
9191
9292
If *U* == *V* the orientation of the arrow on the plot is 45 degrees
93-
counter-clockwise from the horizontal axis (positive to the right).
93+
counter-clockwise from the horizontal axis (positive to the right).
9494
9595
- 'xy': Arrow direction in data coordinates, i.e. the arrows point from
9696
(x, y) to (x+u, y+v). Use this e.g. for plotting a gradient field.

lib/matplotlib/ticker.py

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,51 +2661,32 @@ def __call__(self):
26612661
return self.tick_values(vmin, vmax)
26622662

26632663
def tick_values(self, vmin, vmax):
2664-
# Construct a set of "on-screen" locations
2665-
# that are uniformly spaced:
2664+
# Construct a set of uniformly-spaced "on-screen" locations.
26662665
ymin, ymax = self.linear_width * np.arcsinh(np.array([vmin, vmax])
2667-
/ self.linear_width)
2666+
/ self.linear_width)
26682667
ys = np.linspace(ymin, ymax, self.numticks)
2669-
zero_dev = np.abs(ys / (ymax - ymin))
2670-
if (ymin * ymax) < 0:
2671-
# Ensure that the zero tick-mark is included,
2672-
# if the axis straddles zero
2668+
zero_dev = abs(ys / (ymax - ymin))
2669+
if ymin * ymax < 0:
2670+
# Ensure that the zero tick-mark is included, if the axis straddles zero.
26732671
ys = np.hstack([ys[(zero_dev > 0.5 / self.numticks)], 0.0])
26742672

26752673
# Transform the "on-screen" grid to the data space:
26762674
xs = self.linear_width * np.sinh(ys / self.linear_width)
26772675
zero_xs = (ys == 0)
26782676

2679-
# Round the data-space values to be intuitive base-n numbers,
2680-
# keeping track of positive and negative values separately,
2681-
# but giving careful treatment to the zero value:
2682-
if self.base > 1:
2683-
log_base = math.log(self.base)
2684-
powers = (
2685-
np.where(zero_xs, 0, np.sign(xs)) *
2686-
np.power(self.base,
2687-
np.where(zero_xs, 0.0,
2688-
np.floor(np.log(np.abs(xs) + zero_xs*1e-6)
2689-
/ log_base)))
2690-
)
2691-
if self.subs:
2692-
qs = np.outer(powers, self.subs).flatten()
2693-
else:
2694-
qs = powers
2695-
else:
2696-
powers = (
2697-
np.where(xs >= 0, 1, -1) *
2698-
np.power(10, np.where(zero_xs, 0.0,
2699-
np.floor(np.log10(np.abs(xs)
2700-
+ zero_xs*1e-6))))
2701-
)
2702-
qs = powers * np.round(xs / powers)
2677+
# Round the data-space values to be intuitive base-n numbers, keeping track of
2678+
# positive and negative values separately and carefully treating the zero value.
2679+
with np.errstate(divide="ignore"): # base ** log(0) = base ** -inf = 0.
2680+
if self.base > 1:
2681+
pows = (np.sign(xs)
2682+
* self.base ** np.floor(np.log(abs(xs)) / math.log(self.base)))
2683+
qs = np.outer(pows, self.subs).flatten() if self.subs else pows
2684+
else: # No need to adjust sign(pows), as it cancels out when computing qs.
2685+
pows = np.where(zero_xs, 1, 10**np.floor(np.log10(abs(xs))))
2686+
qs = pows * np.round(xs / pows)
27032687
ticks = np.array(sorted(set(qs)))
27042688

2705-
if len(ticks) >= 2:
2706-
return ticks
2707-
else:
2708-
return np.linspace(vmin, vmax, self.numticks)
2689+
return ticks if len(ticks) >= 2 else np.linspace(vmin, vmax, self.numticks)
27092690

27102691

27112692
class LogitLocator(MaxNLocator):

lib/mpl_toolkits/axisartist/grid_finder.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,7 @@ def get_grid_info(self, x1, y1, x2, y2):
188188
lon_min, lon_max,
189189
lat_min, lat_max)
190190

191-
ddx = (x2-x1)*1.e-10
192-
ddy = (y2-y1)*1.e-10
193-
bb = Bbox.from_extents(x1-ddx, y1-ddy, x2+ddx, y2+ddy)
191+
bb = Bbox.from_extents(x1, y1, x2, y2).expanded(1 + 2e-10, 1 + 2e-10)
194192

195193
grid_info = {
196194
"extremes": extremes,

0 commit comments

Comments
 (0)