From c0834489d35ca788551c6626e7da93f985178a34 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sun, 15 Mar 2020 17:57:04 +0100 Subject: [PATCH] Don't import rcParams but rather use mpl.rcParams --- lib/matplotlib/axes/_base.py | 41 +++++++++++++------------ lib/matplotlib/backends/qt_compat.py | 8 ++--- lib/matplotlib/dates.py | 2 +- lib/matplotlib/figure.py | 38 +++++++++++------------ lib/matplotlib/sankey.py | 4 +-- lib/matplotlib/tests/test_backend_qt.py | 5 ++- 6 files changed, 49 insertions(+), 49 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index f2f51e0c1e21..109e2a5b1c03 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -10,7 +10,7 @@ import numpy as np import matplotlib as mpl -from matplotlib import cbook, rcParams +from matplotlib import cbook from matplotlib.cbook import _OrderedSet, _check_1d, index_of from matplotlib import docstring import matplotlib.colors as mcolors @@ -106,7 +106,7 @@ def _process_plot_format(fmt): 'Unrecognized character %c in format string' % c) if linestyle is None and marker is None: - linestyle = rcParams['lines.linestyle'] + linestyle = mpl.rcParams['lines.linestyle'] if linestyle is None: linestyle = 'None' if marker is None: @@ -142,7 +142,7 @@ def __setstate__(self, state): def set_prop_cycle(self, *args, **kwargs): # Can't do `args == (None,)` as that crashes cycler. if not (args or kwargs) or (len(args) == 1 and args[0] is None): - prop_cycler = rcParams['axes.prop_cycle'] + prop_cycler = mpl.rcParams['axes.prop_cycle'] else: prop_cycler = cycler(*args, **kwargs) @@ -453,10 +453,10 @@ def __init__(self, fig, rect, # this call may differ for non-sep axes, e.g., polar self._init_axis() if facecolor is None: - facecolor = rcParams['axes.facecolor'] + facecolor = mpl.rcParams['axes.facecolor'] self._facecolor = facecolor self._frameon = frameon - self.set_axisbelow(rcParams['axes.axisbelow']) + self.set_axisbelow(mpl.rcParams['axes.axisbelow']) self._rasterization_zorder = None self.cla() @@ -483,6 +483,7 @@ def __init__(self, fig, rect, self._ycid = self.yaxis.callbacks.connect( 'units finalize', lambda: self._on_units_changed(scaley=True)) + rcParams = mpl.rcParams self.tick_params( top=rcParams['xtick.top'] and rcParams['xtick.minor.top'], bottom=rcParams['xtick.bottom'] and rcParams['xtick.minor.bottom'], @@ -697,7 +698,7 @@ def get_xaxis_text1_transform(self, pad_points): class, and is meant to be overridden by new kinds of projections that may need to place axis elements in different locations. """ - labels_align = rcParams["xtick.alignment"] + labels_align = mpl.rcParams["xtick.alignment"] return (self.get_xaxis_transform(which='tick1') + mtransforms.ScaledTranslation(0, -1 * pad_points / 72, self.figure.dpi_scale_trans), @@ -723,7 +724,7 @@ def get_xaxis_text2_transform(self, pad_points): class, and is meant to be overridden by new kinds of projections that may need to place axis elements in different locations. """ - labels_align = rcParams["xtick.alignment"] + labels_align = mpl.rcParams["xtick.alignment"] return (self.get_xaxis_transform(which='tick2') + mtransforms.ScaledTranslation(0, pad_points / 72, self.figure.dpi_scale_trans), @@ -773,7 +774,7 @@ def get_yaxis_text1_transform(self, pad_points): class, and is meant to be overridden by new kinds of projections that may need to place axis elements in different locations. """ - labels_align = rcParams["ytick.alignment"] + labels_align = mpl.rcParams["ytick.alignment"] return (self.get_yaxis_transform(which='tick1') + mtransforms.ScaledTranslation(-1 * pad_points / 72, 0, self.figure.dpi_scale_trans), @@ -799,7 +800,7 @@ def get_yaxis_text2_transform(self, pad_points): class, and is meant to be overridden by new kinds of projections that may need to place axis elements in different locations. """ - labels_align = rcParams["ytick.alignment"] + labels_align = mpl.rcParams["ytick.alignment"] return (self.get_yaxis_transform(which='tick2') + mtransforms.ScaledTranslation(pad_points / 72, 0, self.figure.dpi_scale_trans), @@ -1005,18 +1006,18 @@ def cla(self): except TypeError: pass # update the minor locator for x and y axis based on rcParams - if rcParams['xtick.minor.visible']: + if mpl.rcParams['xtick.minor.visible']: self.xaxis.set_minor_locator(mticker.AutoMinorLocator()) - if rcParams['ytick.minor.visible']: + if mpl.rcParams['ytick.minor.visible']: self.yaxis.set_minor_locator(mticker.AutoMinorLocator()) if self._sharex is None: self._autoscaleXon = True if self._sharey is None: self._autoscaleYon = True - self._xmargin = rcParams['axes.xmargin'] - self._ymargin = rcParams['axes.ymargin'] + self._xmargin = mpl.rcParams['axes.xmargin'] + self._ymargin = mpl.rcParams['axes.ymargin'] self._tight = None self._use_sticky_edges = True self._update_transScale() # needed? @@ -1024,7 +1025,7 @@ def cla(self): self._get_lines = _process_plot_var_args(self) self._get_patches_for_fill = _process_plot_var_args(self, 'fill') - self._gridOn = rcParams['axes.grid'] + self._gridOn = mpl.rcParams['axes.grid'] self.lines = [] self.patches = [] self.texts = [] @@ -1039,11 +1040,11 @@ def cla(self): self.containers = [] self.grid(False) # Disable grid on init to use rcParameter - self.grid(self._gridOn, which=rcParams['axes.grid.which'], - axis=rcParams['axes.grid.axis']) + self.grid(self._gridOn, which=mpl.rcParams['axes.grid.which'], + axis=mpl.rcParams['axes.grid.axis']) props = font_manager.FontProperties( - size=rcParams['axes.titlesize'], - weight=rcParams['axes.titleweight']) + size=mpl.rcParams['axes.titlesize'], + weight=mpl.rcParams['axes.titleweight']) self.title = mtext.Text( x=0.5, y=1.0, text='', @@ -1062,7 +1063,7 @@ def cla(self): verticalalignment='baseline', horizontalalignment='right', ) - title_offset_points = rcParams['axes.titlepad'] + title_offset_points = mpl.rcParams['axes.titlepad'] # refactor this out so it can be called in ax.set_title if # pad argument used... self._set_title_offset_trans(title_offset_points) @@ -1121,7 +1122,7 @@ def set_facecolor(self, color): def _set_title_offset_trans(self, title_offset_points): """ - Set the offset for the title either from rcParams['axes.titlepad'] + Set the offset for the title either from :rc:`axes.titlepad` or from set_title kwarg ``pad``. """ self.titleOffsetTrans = mtransforms.ScaledTranslation( diff --git a/lib/matplotlib/backends/qt_compat.py b/lib/matplotlib/backends/qt_compat.py index 82fa1ed450cc..ed270941f461 100644 --- a/lib/matplotlib/backends/qt_compat.py +++ b/lib/matplotlib/backends/qt_compat.py @@ -15,7 +15,7 @@ import os import sys -from matplotlib import rcParams +import matplotlib as mpl QT_API_PYQT5 = "PyQt5" @@ -41,12 +41,12 @@ # Otherwise, check the QT_API environment variable (from Enthought). This can # only override the binding, not the backend (in other words, we check that the # requested backend actually matches). -elif rcParams["backend"] in ["Qt5Agg", "Qt5Cairo"]: +elif mpl.rcParams["backend"] in ["Qt5Agg", "Qt5Cairo"]: if QT_API_ENV in ["pyqt5", "pyside2"]: QT_API = _ETS[QT_API_ENV] else: QT_API = None -elif rcParams["backend"] in ["Qt4Agg", "Qt4Cairo"]: +elif mpl.rcParams["backend"] in ["Qt4Agg", "Qt4Cairo"]: if QT_API_ENV in ["pyqt4", "pyside"]: QT_API = _ETS[QT_API_ENV] else: @@ -147,7 +147,7 @@ def is_pyqt5(): elif QT_API in [QT_API_PYQTv2, QT_API_PYSIDE, QT_API_PYQT]: _setup_pyqt4() elif QT_API is None: - if rcParams["backend"] == "Qt4Agg": + if mpl.rcParams["backend"] == "Qt4Agg": _candidates = [(_setup_pyqt4, QT_API_PYQTv2), (_setup_pyqt4, QT_API_PYSIDE), (_setup_pyqt4, QT_API_PYQT), diff --git a/lib/matplotlib/dates.py b/lib/matplotlib/dates.py index dd136658e7fb..42203f1982d0 100644 --- a/lib/matplotlib/dates.py +++ b/lib/matplotlib/dates.py @@ -147,7 +147,6 @@ import numpy as np import matplotlib -from matplotlib import rcParams import matplotlib.units as units import matplotlib.cbook as cbook import matplotlib.ticker as ticker @@ -828,6 +827,7 @@ def __init__(self, locator, tz=None, defaultfmt='%Y-%m-%d'): self._tz = tz self.defaultfmt = defaultfmt self._formatter = DateFormatter(self.defaultfmt, tz) + rcParams = matplotlib.rcParams self.scaled = { DAYS_PER_YEAR: rcParams['date.autoformatter.year'], DAYS_PER_MONTH: rcParams['date.autoformatter.month'], diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 1bcf8195ffa4..b4141c8668f7 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -13,7 +13,7 @@ import numpy as np -from matplotlib import rcParams +import matplotlib as mpl from matplotlib import docstring, projections from matplotlib import __version__ as _mpl_version @@ -187,7 +187,7 @@ def __init__(self, left=None, bottom=None, right=None, top=None, """ self.validate = True for key in ["left", "bottom", "right", "top", "wspace", "hspace"]: - setattr(self, key, rcParams[f"figure.subplot.{key}"]) + setattr(self, key, mpl.rcParams[f"figure.subplot.{key}"]) self.update(left, bottom, right, top, wspace, hspace) def update(self, left=None, bottom=None, right=None, top=None, @@ -307,15 +307,15 @@ def __init__(self, self.callbacks = cbook.CallbackRegistry() if figsize is None: - figsize = rcParams['figure.figsize'] + figsize = mpl.rcParams['figure.figsize'] if dpi is None: - dpi = rcParams['figure.dpi'] + dpi = mpl.rcParams['figure.dpi'] if facecolor is None: - facecolor = rcParams['figure.facecolor'] + facecolor = mpl.rcParams['figure.facecolor'] if edgecolor is None: - edgecolor = rcParams['figure.edgecolor'] + edgecolor = mpl.rcParams['figure.edgecolor'] if frameon is None: - frameon = rcParams['figure.frameon'] + frameon = mpl.rcParams['figure.frameon'] if not np.isfinite(figsize).all() or (np.array(figsize) <= 0).any(): raise ValueError('figure size must be positive finite not ' @@ -467,7 +467,7 @@ def set_tight_layout(self, tight): default paddings. """ if tight is None: - tight = rcParams['figure.autolayout'] + tight = mpl.rcParams['figure.autolayout'] self._tight = bool(tight) self._tight_parameters = tight if isinstance(tight, dict) else {} self.stale = True @@ -483,7 +483,7 @@ def get_constrained_layout(self): def set_constrained_layout(self, constrained): """ Set whether ``constrained_layout`` is used upon drawing. If None, - the rcParams['figure.constrained_layout.use'] value will be used. + :rc:`figure.constrained_layout.use` value will be used. When providing a dict containing the keys `w_pad`, `h_pad` the default ``constrained_layout`` paddings will be @@ -502,7 +502,7 @@ def set_constrained_layout(self, constrained): self._constrained_layout_pads['wspace'] = None self._constrained_layout_pads['hspace'] = None if constrained is None: - constrained = rcParams['figure.constrained_layout.use'] + constrained = mpl.rcParams['figure.constrained_layout.use'] self._constrained = bool(constrained) if isinstance(constrained, dict): self.set_constrained_layout_pads(**constrained) @@ -544,7 +544,7 @@ def set_constrained_layout_pads(self, **kwargs): self._constrained_layout_pads[td] = kwargs[td] else: self._constrained_layout_pads[td] = ( - rcParams['figure.constrained_layout.' + td]) + mpl.rcParams['figure.constrained_layout.' + td]) def get_constrained_layout_pads(self, relative=False): """ @@ -713,9 +713,9 @@ def suptitle(self, t, **kwargs): if 'fontproperties' not in kwargs: if 'fontsize' not in kwargs and 'size' not in kwargs: - kwargs['size'] = rcParams['figure.titlesize'] + kwargs['size'] = mpl.rcParams['figure.titlesize'] if 'fontweight' not in kwargs and 'weight' not in kwargs: - kwargs['weight'] = rcParams['figure.titleweight'] + kwargs['weight'] = mpl.rcParams['figure.titleweight'] sup = self.text(x, y, t, **kwargs) if self._suptitle is not None: @@ -1840,7 +1840,7 @@ def text(self, x, y, s, fontdict=None, **kwargs): fontdict : dict, optional A dictionary to override the default text properties. If not given, - the defaults are determined by `.rcParams`. Properties passed as + the defaults are determined by :rc:`font.*`. Properties passed as *kwargs* override the corresponding ones given in *fontdict*. Other Parameters @@ -2164,9 +2164,9 @@ def savefig(self, fname, *, transparent=None, **kwargs): `PIL.Image.Image.save` when saving the figure. """ - kwargs.setdefault('dpi', rcParams['savefig.dpi']) + kwargs.setdefault('dpi', mpl.rcParams['savefig.dpi']) if transparent is None: - transparent = rcParams['savefig.transparent'] + transparent = mpl.rcParams['savefig.transparent'] if transparent: kwargs.setdefault('facecolor', 'none') @@ -2179,8 +2179,8 @@ def savefig(self, fname, *, transparent=None, **kwargs): patch.set_facecolor('none') patch.set_edgecolor('none') else: - kwargs.setdefault('facecolor', rcParams['savefig.facecolor']) - kwargs.setdefault('edgecolor', rcParams['savefig.edgecolor']) + kwargs.setdefault('facecolor', mpl.rcParams['savefig.facecolor']) + kwargs.setdefault('edgecolor', mpl.rcParams['savefig.edgecolor']) self.canvas.print_figure(fname, **kwargs) @@ -2734,7 +2734,7 @@ def figaspect(arg): arr_ratio = arg # Height of user figure defaults - fig_height = rcParams['figure.figsize'][1] + fig_height = mpl.rcParams['figure.figsize'][1] # New size for the figure, keeping the aspect ratio of the caller newsize = np.array((fig_height / arr_ratio, fig_height)) diff --git a/lib/matplotlib/sankey.py b/lib/matplotlib/sankey.py index 1934af3d5816..b11206b9446a 100644 --- a/lib/matplotlib/sankey.py +++ b/lib/matplotlib/sankey.py @@ -7,11 +7,11 @@ import numpy as np +import matplotlib as mpl from matplotlib.path import Path from matplotlib.patches import PathPatch from matplotlib.transforms import Affine2D from matplotlib import docstring -from matplotlib import rcParams _log = logging.getLogger(__name__) @@ -718,7 +718,7 @@ def _get_angle(a, r): vertices = translate(rotate(vertices)) kwds = dict(s=patchlabel, ha='center', va='center') text = self.ax.text(*offset, **kwds) - if rcParams['_internal.classic_mode']: + if mpl.rcParams['_internal.classic_mode']: fc = kwargs.pop('fc', kwargs.pop('facecolor', '#bfd1d4')) lw = kwargs.pop('lw', kwargs.pop('linewidth', 0.5)) else: diff --git a/lib/matplotlib/tests/test_backend_qt.py b/lib/matplotlib/tests/test_backend_qt.py index a4d704228fb6..1f4798e25498 100644 --- a/lib/matplotlib/tests/test_backend_qt.py +++ b/lib/matplotlib/tests/test_backend_qt.py @@ -5,7 +5,6 @@ import matplotlib from matplotlib import pyplot as plt -from matplotlib import rcParams from matplotlib._pylab_helpers import Gcf import pytest @@ -246,8 +245,8 @@ def test_double_resize(): w, h = 3, 2 fig.set_size_inches(w, h) - assert fig.canvas.width() == w * rcParams['figure.dpi'] - assert fig.canvas.height() == h * rcParams['figure.dpi'] + assert fig.canvas.width() == w * matplotlib.rcParams['figure.dpi'] + assert fig.canvas.height() == h * matplotlib.rcParams['figure.dpi'] old_width = window.width() old_height = window.height()