@@ -47,6 +47,12 @@ def import_matplotlib_pyplot():
4747 return plt
4848
4949
50+ try :
51+ plt = import_matplotlib_pyplot ()
52+ except ImportError :
53+ plt = None
54+
55+
5056def _determine_extend (calc_data , vmin , vmax ):
5157 extend_min = calc_data .min () < vmin
5258 extend_max = calc_data .max () > vmax
@@ -64,7 +70,7 @@ def _build_discrete_cmap(cmap, levels, extend, filled):
6470 """
6571 Build a discrete colormap and normalization of the data.
6672 """
67- import matplotlib as mpl
73+ mpl = plt . matplotlib
6874
6975 if len (levels ) == 1 :
7076 levels = [levels [0 ], levels [0 ]]
@@ -115,8 +121,7 @@ def _build_discrete_cmap(cmap, levels, extend, filled):
115121
116122
117123def _color_palette (cmap , n_colors ):
118- import matplotlib .pyplot as plt
119- from matplotlib .colors import ListedColormap
124+ ListedColormap = plt .matplotlib .colors .ListedColormap
120125
121126 colors_i = np .linspace (0 , 1.0 , n_colors )
122127 if isinstance (cmap , (list , tuple )):
@@ -177,7 +182,7 @@ def _determine_cmap_params(
177182 cmap_params : dict
178183 Use depends on the type of the plotting function
179184 """
180- import matplotlib as mpl
185+ mpl = plt . matplotlib
181186
182187 if isinstance (levels , Iterable ):
183188 levels = sorted (levels )
@@ -285,13 +290,13 @@ def _determine_cmap_params(
285290 levels = np .asarray ([(vmin + vmax ) / 2 ])
286291 else :
287292 # N in MaxNLocator refers to bins, not ticks
288- ticker = mpl . ticker .MaxNLocator (levels - 1 )
293+ ticker = plt .MaxNLocator (levels - 1 )
289294 levels = ticker .tick_values (vmin , vmax )
290295 vmin , vmax = levels [0 ], levels [- 1 ]
291296
292297 # GH3734
293298 if vmin == vmax :
294- vmin , vmax = mpl . ticker .LinearLocator (2 ).tick_values (vmin , vmax )
299+ vmin , vmax = plt .LinearLocator (2 ).tick_values (vmin , vmax )
295300
296301 if extend is None :
297302 extend = _determine_extend (calc_data , vmin , vmax )
@@ -421,10 +426,7 @@ def _assert_valid_xy(darray, xy, name):
421426
422427
423428def get_axis (figsize = None , size = None , aspect = None , ax = None , ** kwargs ):
424- try :
425- import matplotlib as mpl
426- import matplotlib .pyplot as plt
427- except ImportError :
429+ if plt is None :
428430 raise ImportError ("matplotlib is required for plot.utils.get_axis" )
429431
430432 if figsize is not None :
@@ -437,7 +439,7 @@ def get_axis(figsize=None, size=None, aspect=None, ax=None, **kwargs):
437439 if ax is not None :
438440 raise ValueError ("cannot provide both `size` and `ax` arguments" )
439441 if aspect is None :
440- width , height = mpl .rcParams ["figure.figsize" ]
442+ width , height = plt .rcParams ["figure.figsize" ]
441443 aspect = width / height
442444 figsize = (size * aspect , size )
443445 _ , ax = plt .subplots (figsize = figsize )
@@ -454,9 +456,6 @@ def get_axis(figsize=None, size=None, aspect=None, ax=None, **kwargs):
454456
455457
456458def _maybe_gca (** kwargs ):
457-
458- import matplotlib .pyplot as plt
459-
460459 # can call gcf unconditionally: either it exists or would be created by plt.axes
461460 f = plt .gcf ()
462461
@@ -912,9 +911,7 @@ def _process_cmap_cbar_kwargs(
912911
913912
914913def _get_nice_quiver_magnitude (u , v ):
915- import matplotlib as mpl
916-
917- ticker = mpl .ticker .MaxNLocator (3 )
914+ ticker = plt .MaxNLocator (3 )
918915 mean = np .mean (np .hypot (u .to_numpy (), v .to_numpy ()))
919916 magnitude = ticker .tick_values (0 , mean )[- 2 ]
920917 return magnitude
@@ -989,7 +986,7 @@ def legend_elements(
989986 """
990987 import warnings
991988
992- import matplotlib as mpl
989+ mpl = plt . matplotlib
993990
994991 mlines = mpl .lines
995992
@@ -1126,7 +1123,6 @@ def _legend_add_subtitle(handles, labels, text, func):
11261123
11271124def _adjust_legend_subtitles (legend ):
11281125 """Make invisible-handle "subtitles" entries look more like titles."""
1129- plt = import_matplotlib_pyplot ()
11301126
11311127 # Legend title not in rcParams until 3.0
11321128 font_size = plt .rcParams .get ("legend.title_fontsize" , None )
0 commit comments