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

Skip to content

Commit 3cfb5fc

Browse files
committed
Add _val_or_rc-function
1 parent 0b8bd96 commit 3cfb5fc

File tree

8 files changed

+49
-71
lines changed

8 files changed

+49
-71
lines changed

lib/matplotlib/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,13 @@ def is_interactive():
12941294
return rcParams['interactive']
12951295

12961296

1297+
def _val_or_rc(val, rc_name):
1298+
"""
1299+
If *val* is None, return ``mpl.rcParams[rc_name]``, otherwise return val.
1300+
"""
1301+
return val if val is not None else rcParams[rc_name]
1302+
1303+
12971304
def _init_tests():
12981305
# The version of FreeType to install locally for running the
12991306
# tests. This must match the value in `setupext.py`

lib/matplotlib/_mathtext.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,11 @@ def to_raster(self):
121121
# old approach and keeps baseline images backcompat.
122122
shifted = ship(self.box, (-xmin, -ymin))
123123

124+
antialiased = mpl.rcParams['text.antialiased']
124125
for ox, oy, info in shifted.glyphs:
125126
info.font.draw_glyph_to_bitmap(
126127
image, ox, oy - info.metrics.iceberg, info.glyph,
127-
antialiased=mpl.rcParams['text.antialiased'])
128+
antialiased=antialiased)
128129
for x1, y1, x2, y2 in shifted.rects:
129130
height = max(int(y2 - y1) - 1, 0)
130131
if height == 0:

lib/matplotlib/animation.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,8 @@ class AbstractMovieWriter(abc.ABC):
173173
def __init__(self, fps=5, metadata=None, codec=None, bitrate=None):
174174
self.fps = fps
175175
self.metadata = metadata if metadata is not None else {}
176-
self.codec = (
177-
mpl.rcParams['animation.codec'] if codec is None else codec)
178-
self.bitrate = (
179-
mpl.rcParams['animation.bitrate'] if bitrate is None else bitrate)
176+
self.codec = mpl._val_or_rc(codec, 'animation.codec')
177+
self.bitrate = mpl._val_or_rc(bitrate, 'animation.bitrate')
180178

181179
@abc.abstractmethod
182180
def setup(self, fig, outfile, dpi=None):
@@ -733,10 +731,7 @@ def __init__(self, fps=30, codec=None, bitrate=None, extra_args=None,
733731
default_mode=self.default_mode)
734732

735733
# Save embed limit, which is given in MB
736-
if embed_limit is None:
737-
self._bytes_limit = mpl.rcParams['animation.embed_limit']
738-
else:
739-
self._bytes_limit = embed_limit
734+
self._bytes_limit = mpl._val_or_rc(embed_limit, 'animation.embed_limit')
740735
# Convert from MB to bytes
741736
self._bytes_limit *= 1024 * 1024
742737

@@ -1028,8 +1023,7 @@ def func(current_frame: int, total_frames: int) -> Any
10281023
fps = 1000. / self._interval
10291024

10301025
# Re-use the savefig DPI for ours if none is given
1031-
if dpi is None:
1032-
dpi = mpl.rcParams['savefig.dpi']
1026+
dpi = mpl._val_or_rc(dpi, 'savefig.dpi')
10331027
if dpi == 'figure':
10341028
dpi = self._fig.dpi
10351029

@@ -1267,8 +1261,7 @@ def to_html5_video(self, embed_limit=None):
12671261
# Cache the rendering of the video as HTML
12681262
if not hasattr(self, '_base64_video'):
12691263
# Save embed limit, which is given in MB
1270-
if embed_limit is None:
1271-
embed_limit = mpl.rcParams['animation.embed_limit']
1264+
embed_limit = mpl._val_or_rc(embed_limit, 'animation.embed_limit')
12721265

12731266
# Convert from MB to bytes
12741267
embed_limit *= 1024 * 1024

lib/matplotlib/axis.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,9 @@ def __init__(
139139
zorder = mlines.Line2D.zorder
140140
self._zorder = zorder
141141

142-
if grid_color is None:
143-
grid_color = mpl.rcParams["grid.color"]
144-
if grid_linestyle is None:
145-
grid_linestyle = mpl.rcParams["grid.linestyle"]
146-
if grid_linewidth is None:
147-
grid_linewidth = mpl.rcParams["grid.linewidth"]
142+
grid_color = mpl._val_or_rc(grid_color, "grid.color")
143+
grid_linestyle = mpl._val_or_rc(grid_linestyle, "grid.linestyle")
144+
grid_linewidth = mpl._val_or_rc(grid_linewidth, "grid.linewidth")
148145
if grid_alpha is None and not mcolors._has_alpha_channel(grid_color):
149146
# alpha precedence: kwarg > color alpha > rcParams['grid.alpha']
150147
# Note: only resolve to rcParams if the color does not have alpha

lib/matplotlib/dates.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,9 @@ def _get_tzinfo(tz=None):
218218
Generate `~datetime.tzinfo` from a string or return `~datetime.tzinfo`.
219219
If None, retrieve the preferred timezone from the rcParams dictionary.
220220
"""
221-
if tz is None:
222-
tz = mpl.rcParams['timezone']
223-
if tz == 'UTC':
224-
return UTC
221+
tz = mpl._val_or_rc(tz, 'timezone')
222+
if tz == 'UTC':
223+
return UTC
225224
if isinstance(tz, str):
226225
tzinfo = dateutil.tz.gettz(tz)
227226
if tzinfo is None:
@@ -312,8 +311,7 @@ def get_epoch():
312311
"""
313312
global _epoch
314313

315-
if _epoch is None:
316-
_epoch = mpl.rcParams['date.epoch']
314+
_epoch = mpl._val_or_rc(_epoch, 'date.epoch')
317315
return _epoch
318316

319317

@@ -641,8 +639,7 @@ def __init__(self, fmt, tz=None, *, usetex=None):
641639
"""
642640
self.tz = _get_tzinfo(tz)
643641
self.fmt = fmt
644-
self._usetex = (usetex if usetex is not None else
645-
mpl.rcParams['text.usetex'])
642+
self._usetex = mpl._val_or_rc(usetex, 'text.usetex')
646643

647644
def __call__(self, x, pos=0):
648645
result = num2date(x, self.tz).strftime(self.fmt)
@@ -779,8 +776,7 @@ def __init__(self, locator, tz=None, formats=None, offset_formats=None,
779776
'%Y-%b-%d %H:%M']
780777
self.offset_string = ''
781778
self.show_offset = show_offset
782-
self._usetex = (usetex if usetex is not None else
783-
mpl.rcParams['text.usetex'])
779+
self._usetex = mpl._val_or_rc(usetex, 'text.usetex')
784780

785781
def __call__(self, x, pos=None):
786782
formatter = DateFormatter(self.defaultfmt, self._tz,
@@ -957,8 +953,7 @@ def __init__(self, locator, tz=None, defaultfmt='%Y-%m-%d', *,
957953
self.defaultfmt = defaultfmt
958954
self._formatter = DateFormatter(self.defaultfmt, tz)
959955
rcParams = mpl.rcParams
960-
self._usetex = (usetex if usetex is not None else
961-
mpl.rcParams['text.usetex'])
956+
self._usetex = mpl._val_or_rc(usetex, 'text.usetex')
962957
self.scaled = {
963958
DAYS_PER_YEAR: rcParams['date.autoformatter.year'],
964959
DAYS_PER_MONTH: rcParams['date.autoformatter.month'],

lib/matplotlib/image.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -773,10 +773,8 @@ def set_interpolation(self, s):
773773
'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric', 'catrom', \
774774
'gaussian', 'bessel', 'mitchell', 'sinc', 'lanczos', 'none'} or None
775775
"""
776-
if s is None:
777-
s = mpl.rcParams['image.interpolation']
778-
s = s.lower()
779-
_api.check_in_list(_interpd_, interpolation=s)
776+
s = mpl._val_or_rc(s, 'image.interpolation').lower()
777+
_api.check_in_list(interpolations_names, interpolation=s)
780778
self._interpolation = s
781779
self.stale = True
782780

@@ -813,8 +811,7 @@ def set_resample(self, v):
813811
v : bool or None
814812
If None, use :rc:`image.resample`.
815813
"""
816-
if v is None:
817-
v = mpl.rcParams['image.resample']
814+
v = mpl._val_or_rc(v, 'image.resample')
818815
self._resample = v
819816
self.stale = True
820817

@@ -1614,6 +1611,8 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
16141611
# size when dividing and then multiplying by dpi.
16151612
if origin is None:
16161613
origin = mpl.rcParams["image.origin"]
1614+
else:
1615+
_api.check_in_list(('upper', 'lower'))
16171616
if origin == "lower":
16181617
arr = arr[::-1]
16191618
if (isinstance(arr, memoryview) and arr.format == "B"

lib/matplotlib/legend.py

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,7 @@ def __init__(
427427
super().__init__()
428428

429429
if prop is None:
430-
if fontsize is not None:
431-
self.prop = FontProperties(size=fontsize)
432-
else:
433-
self.prop = FontProperties(
434-
size=mpl.rcParams["legend.fontsize"])
430+
self.prop = FontProperties(size=mpl._val_or_rc(fontsize, "legend.fontsize"))
435431
else:
436432
self.prop = FontProperties._from_any(prop)
437433
if isinstance(prop, dict) and "size" not in prop:
@@ -447,20 +443,17 @@ def __init__(
447443
#: instance.
448444
self._custom_handler_map = handler_map
449445

450-
def val_or_rc(val, rc_name):
451-
return val if val is not None else mpl.rcParams[rc_name]
452-
453-
self.numpoints = val_or_rc(numpoints, 'legend.numpoints')
454-
self.markerscale = val_or_rc(markerscale, 'legend.markerscale')
455-
self.scatterpoints = val_or_rc(scatterpoints, 'legend.scatterpoints')
456-
self.borderpad = val_or_rc(borderpad, 'legend.borderpad')
457-
self.labelspacing = val_or_rc(labelspacing, 'legend.labelspacing')
458-
self.handlelength = val_or_rc(handlelength, 'legend.handlelength')
459-
self.handleheight = val_or_rc(handleheight, 'legend.handleheight')
460-
self.handletextpad = val_or_rc(handletextpad, 'legend.handletextpad')
461-
self.borderaxespad = val_or_rc(borderaxespad, 'legend.borderaxespad')
462-
self.columnspacing = val_or_rc(columnspacing, 'legend.columnspacing')
463-
self.shadow = val_or_rc(shadow, 'legend.shadow')
446+
self.numpoints = mpl._val_or_rc(numpoints, 'legend.numpoints')
447+
self.markerscale = mpl._val_or_rc(markerscale, 'legend.markerscale')
448+
self.scatterpoints = mpl._val_or_rc(scatterpoints, 'legend.scatterpoints')
449+
self.borderpad = mpl._val_or_rc(borderpad, 'legend.borderpad')
450+
self.labelspacing = mpl._val_or_rc(labelspacing, 'legend.labelspacing')
451+
self.handlelength = mpl._val_or_rc(handlelength, 'legend.handlelength')
452+
self.handleheight = mpl._val_or_rc(handleheight, 'legend.handleheight')
453+
self.handletextpad = mpl._val_or_rc(handletextpad, 'legend.handletextpad')
454+
self.borderaxespad = mpl._val_or_rc(borderaxespad, 'legend.borderaxespad')
455+
self.columnspacing = mpl._val_or_rc(columnspacing, 'legend.columnspacing')
456+
self.shadow = mpl._val_or_rc(shadow, 'legend.shadow')
464457
# trim handles and labels if illegal label...
465458
_lab, _hand = [], []
466459
for label, handle in zip(labels, handles):
@@ -533,18 +526,15 @@ def val_or_rc(val, rc_name):
533526
# We use FancyBboxPatch to draw a legend frame. The location
534527
# and size of the box will be updated during the drawing time.
535528

536-
if facecolor is None:
537-
facecolor = mpl.rcParams["legend.facecolor"]
529+
facecolor = mpl._val_or_rc(facecolor, "legend.facecolor")
538530
if facecolor == 'inherit':
539531
facecolor = mpl.rcParams["axes.facecolor"]
540532

541-
if edgecolor is None:
542-
edgecolor = mpl.rcParams["legend.edgecolor"]
533+
edgecolor = mpl._val_or_rc(edgecolor, "legend.edgecolor")
543534
if edgecolor == 'inherit':
544535
edgecolor = mpl.rcParams["axes.edgecolor"]
545536

546-
if fancybox is None:
547-
fancybox = mpl.rcParams["legend.fancybox"]
537+
fancybox = mpl._val_or_rc(fancybox, "legend.fancybox")
548538

549539
self.legendPatch = FancyBboxPatch(
550540
xy=(0, 0), width=1, height=1,
@@ -559,8 +549,7 @@ def val_or_rc(val, rc_name):
559549
else "square,pad=0"),
560550
mutation_scale=self._fontsize,
561551
snap=True,
562-
visible=(frameon if frameon is not None
563-
else mpl.rcParams["legend.frameon"])
552+
visible=mpl._val_or_rc(frameon, "legend.frameon")
564553
)
565554
self._set_artist_props(self.legendPatch)
566555

@@ -604,9 +593,8 @@ def val_or_rc(val, rc_name):
604593
'mec': ['get_markeredgecolor', 'get_edgecolor'],
605594
}
606595
if labelcolor is None:
607-
if mpl.rcParams['legend.labelcolor'] is not None:
608-
labelcolor = mpl.rcParams['legend.labelcolor']
609-
else:
596+
labelcolor = mpl.rcParams['legend.labelcolor']
597+
if labelcolor is None:
610598
labelcolor = mpl.rcParams['text.color']
611599
if isinstance(labelcolor, str) and labelcolor in color_getters:
612600
getter_names = color_getters[labelcolor]

lib/matplotlib/text.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,10 @@ def _reset_visual_defaults(
173173
antialiased=None
174174
):
175175
self.set_text(text)
176-
self.set_color(
177-
color if color is not None else mpl.rcParams["text.color"])
176+
self.set_color(mpl._val_or_rc(color, "text.color"))
178177
self.set_fontproperties(fontproperties)
179178
self.set_usetex(usetex)
180-
self.set_parse_math(parse_math if parse_math is not None else
181-
mpl.rcParams['text.parse_math'])
179+
self.set_parse_math(mpl._val_or_rc(parse_math, 'text.parse_math'))
182180
self.set_wrap(wrap)
183181
self.set_verticalalignment(verticalalignment)
184182
self.set_horizontalalignment(horizontalalignment)

0 commit comments

Comments
 (0)