8000 Merge pull request #26168 from oscargus/valorrc · matplotlib/matplotlib@72888a1 · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 72888a1

Browse files
authored
Merge pull request #26168 from oscargus/valorrc
Add _val_or_rc-function
2 parents 4e988f5 + 6e0a5a5 commit 72888a1

File tree

8 files changed

+49
-72
lines changed

8 files changed

+49
-72
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
@@ -127,10 +127,11 @@ def to_raster(self):
127127
# old approach and keeps baseline images backcompat.
128128
shifted = ship(self.box, (-xmin, -ymin))
129129

130+
antialiased = mpl.rcParams['text.antialiased']
130131
for ox, oy, info in shifted.glyphs:
131132
info.font.draw_glyph_to_bitmap(
132133
image, ox, oy - info.metrics.iceberg, info.glyph,
133-
antialiased=mpl.rcParams['text.antialiased'])
134+
antialiased=antialiased)
134135
for x1, y1, x2, y2 in shifted.rects:
135136
height = max(int(y2 - y1) - 1, 0)
136137
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):
@@ -742,10 +740,7 @@ def __init__(self, fps=30, codec=None, bitrate=None, extra_args=None,
742740
default_mode=self.default_mode)
743741

744742
# Save embed limit, which is given in MB
745-
if embed_limit is None:
746-
self._bytes_limit = mpl.rcParams['animation.embed_limit']
747-
else:
748-
self._bytes_limit = embed_limit
743+
self._bytes_limit = mpl._val_or_rc(embed_limit, 'animation.embed_limit')
749744
# Convert from MB to bytes
750745
self._bytes_limit *= 1024 * 1024
751746

@@ -1038,8 +1033,7 @@ def func(current_frame: int, total_frames: int) -> Any
10381033
fps = 1000. / self._interval
10391034

10401035
# Re-use the savefig DPI for ours if none is given
1041-
if dpi is None:
1042-
dpi = mpl.rcParams['savefig.dpi']
1036+
dpi = mpl._val_or_rc(dpi, 'savefig.dpi')
10431037
if dpi == 'figure':
10441038
dpi = self._fig.dpi
10451039

@@ -1277,8 +1271,7 @@ def to_html5_video(self, embed_limit=None):
12771271
# Cache the rendering of the video as HTML
12781272
if not hasattr(self, '_base64_video'):
12791273
# Save embed limit, which is given in MB
1280-
if embed_limit is None:
1281-
embed_limit = mpl.rcParams['animation.embed_limit']
1274+
embed_limit = mpl._val_or_rc(embed_limit, 'animation.embed_limit')
12821275

12831276
# Convert from MB to bytes
12841277
embed_limit *= 1024 * 1024

lib/matplotlib/axis.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,9 @@ def __init__(
144144
zorder = mlines.Line2D.zorder
145145
self._zorder = zorder
146146

147-
if grid_color is None:
148-
grid_color = mpl.rcParams["grid.color"]
149-
if grid_linestyle is None:
150-
grid_linestyle = mpl.rcParams["grid.linestyle"]
151-
if grid_linewidth is None:
152-
grid_linewidth = mpl.rcParams["grid.linewidth"]
147+
grid_color = mpl._val_or_rc(grid_color, "grid.color")
148+
grid_linestyle = mpl._val_or_rc(grid_linestyle, "grid.linestyle")
149+
grid_linewidth = mpl._val_or_rc(grid_linewidth, "grid.linewidth")
153150
if grid_alpha is None and not mcolors._has_alpha_channel(grid_color):
154151
# alpha precedence: kwarg > color alpha > rcParams['grid.alpha']
155152
# 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
@@ -222,10 +222,9 @@ def _get_tzinfo(tz=None):
222222
Generate `~datetime.tzinfo` from a string or return `~datetime.tzinfo`.
223223
If None, retrieve the preferred timezone from the rcParams dictionary.
224224
"""
225-
if tz is None:
226-
tz = mpl.rcParams['timezone']
227-
if tz == 'UTC':
228-
return UTC
225+
tz = mpl._val_or_rc(tz, 'timezone')
226+
if tz == 'UTC':
227+
return UTC
229228
if isinstance(tz, str):
230229
tzinfo = dateutil.tz.gettz(tz)
231230
if tzinfo is None:
@@ -316,8 +315,7 @@ def get_epoch():
316315
"""
317316
global _epoch
318317

319-
if _epoch is None:
320-
_epoch = mpl.rcParams['date.epoch']
318+
_epoch = mpl._val_or_rc(_epoch, 'date.epoch')
321319
return _epoch
322320

323321

@@ -645,8 +643,7 @@ def __init__(self, fmt, tz=None, *, usetex=None):
645643
"""
646644
self.tz = _get_tzinfo(tz)
647645
self.fmt = fmt
648-
self._usetex = (usetex if usetex is not None else
649-
mpl.rcParams['text.usetex'])
646+
self._usetex = mpl._val_or_rc(usetex, 'text.usetex')
650647

651648
def __call__(self, x, pos=0):
652649
result = num2date(x, self.tz).strftime(self.fmt)
@@ -783,8 +780,7 @@ def __init__(self, locator, tz=None, formats=None, offset_formats=None,
783780
'%Y-%b-%d %H:%M']
784781
self.offset_string = ''
785782
self.show_offset = show_offset
786-
self._usetex = (usetex if usetex is not None else
787-
mpl.rcParams['text.usetex'])
783+
self._usetex = mpl._val_or_rc(usetex, 'text.usetex')
788784

789785
def __call__(self, x, pos=None):
790786
formatter = DateFormatter(self.defaultfmt, self._tz,
@@ -961,8 +957,7 @@ def __init__(self, locator, tz=None, defaultfmt='%Y-%m-%d', *,
961957
self.defaultfmt = defaultfmt
962958
self._formatter = DateFormatter(self.defaultfmt, tz)
963959
rcParams = mpl.rcParams
964-
self._usetex = (usetex if usetex is not None else
965-
mpl.rcParams['text.usetex'])
960+
self._usetex = mpl._val_or_rc(usetex, 'text.usetex')
966961
self.scaled = {
967962
DAYS_PER_YEAR: rcParams['date.autoformatter.year'],
968963
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
@@ -761,10 +761,8 @@ def set_interpolation(self, s):
761761
'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric', 'catrom', \
762762
'gaussian', 'bessel', 'mitchell', 'sinc', 'lanczos', 'none'} or None
763763
"""
764-
if s is None:
765-
s = mpl.rcParams['image.interpolation']
766-
s = s.lower()
767-
_api.check_in_list(_interpd_, interpolation=s)
764+
s = mpl._val_or_rc(s, 'image.interpolation').lower()
765+
_api.check_in_list(interpolations_names, interpolation=s)
768766
self._interpolation = s
769767
self.stale = True
770768

@@ -801,8 +799,7 @@ def set_resample(self, v):
801799
v : bool or None
802800
If None, use :rc:`image.resample`.
803801
"""
804-
if v is None:
805-
v = mpl.rcParams['image.resample']
802+
v = mpl._val_or_rc(v, 'image.resample')
806803
self._resample = v
807804
self.stale = True
808805

@@ -1602,6 +1599,8 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
16021599
# size when dividing and then multiplying by dpi.
16031600
if origin is None:
16041601
origin = mpl.rcParams["image.origin"]
1602+
else:
1603+
_api.check_in_list(('upper', 'lower'), origin=origin)
16051604
if origin == "lower":
16061605
arr = arr[::-1]
16071606
if (isinstance(arr, memoryview) and arr.format == "B"

lib/matplotlib/legend.py

Lines changed: 18 additions & 31 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):
@@ -536,18 +529,15 @@ def val_or_rc(val, rc_name):
536529
# We use FancyBboxPatch to draw a legend frame. The location
537530
# and size of the box will be updated during the drawing time.
538531

539-
if facecolor is None:
540-
facecolor = mpl.rcParams["legend.facecolor"]
532+
facecolor = mpl._val_or_rc(facecolor, "legend.facecolor")
541533
if facecolor == 'inherit':
542534
facecolor = mpl.rcParams["axes.facecolor"]
543535

544-
if edgecolor is None:
545-
edgecolor = mpl.rcParams["legend.edgecolor"]
536+
edgecolor = mpl._val_or_rc(edgecolor, "legend.edgecolor")
546537
if edgecolor == 'inherit':
547538
edgecolor = mpl.rcParams["axes.edgecolor"]
548539

549-
if fancybox is None:
550-
fancybox = mpl.rcParams["legend.fancybox"]
540+
fancybox = mpl._val_or_rc(fancybox, "legend.fancybox")
551541

552542
self.legendPatch = FancyBboxPatch(
553543
xy=(0, 0), width=1, height=1,
@@ -562,8 +552,7 @@ def val_or_rc(val, rc_name):
562552
else "square,pad=0"),
563553
mutation_scale=self._fontsize,
564554
snap=True,
565-
visible=(frameon if frameon is not None
566-
else mpl.rcParams["legend.frameon"])
555+
visible=mpl._val_or_rc(frameon, "legend.frameon")
567556
)
568557
self._set_artist_props(self.legendPatch)
569558

@@ -606,11 +595,9 @@ def val_or_rc(val, rc_name):
606595
'markeredgecolor': ['get_markeredgecolor', 'get_edgecolor'],
607596
'mec': ['get_markeredgecolor', 'get_edgecolor'],
608597
}
598+
labelcolor = mpl._val_or_rc(labelcolor, 'legend.labelcolor')
609599
if labelcolor is None:
610-
if mpl.rcParams['legend.labelcolor'] is not None:
611-
labelcolor = mpl.rcParams['legend.labelcolor']
612-
else:
613-
labelcolor = mpl.rcParams['text.color']
600+
labelcolor = mpl.rcParams['text.color']
614601
if isinstance(labelcolor, str) and labelcolor in color_getters:
615602
getter_names = color_getters[labelcolor]
616603
for handle, text in zip(self.legend_handles, self.texts):

lib/matplotlib/text.py

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

0 commit comments

Comments
 (0)
0