Skip to content

Commit 2e3614c

Browse files
committed
When importing mpl do not rely on submodules being loaded
That ``` import matplotlib as mpl mpl.[submodule].[class] ``` works is an implementation detail because the top-level namespace has imported these submodules. There's no guarantee this will continue to work. Also move the imports of the two relevant classes close to their usage. This minimizes our top-level import to the canonical `import matplotlib.pyplot as plt`.
1 parent d45dfbb commit 2e3614c

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ per-file-ignores =
4747
lib/mpl_toolkits/axisartist/angle_helper.py: E221
4848

4949
doc/conf.py: E402
50+
galleries/users_explain/quick_start.py: E402
5051
galleries/users_explain/artists/paths.py: E402
5152
galleries/users_explain/artists/patheffects_guide.py: E402
5253
galleries/users_explain/artists/transforms_tutorial.py: E402, E501

galleries/users_explain/quick_start.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import numpy as np
1818

1919
# sphinx_gallery_thumbnail_number = 3
20-
import matplotlib as mpl
2120

2221
# %%
2322
#
@@ -446,13 +445,14 @@ def my_plotter(ax, data1, data2, param_dict):
446445
# well as floating point numbers. These get special locators and formatters
447446
# as appropriate. For dates:
448447

448+
from matplotlib.dates import ConciseDateFormatter
449+
449450
fig, ax = plt.subplots(figsize=(5, 2.7), layout='constrained')
450451
dates = np.arange(np.datetime64('2021-11-15'), np.datetime64('2021-12-25'),
451452
np.timedelta64(1, 'h'))
452453
data = np.cumsum(np.random.randn(len(dates)))
453454
ax.plot(dates, data)
454-
cdf = mpl.dates.ConciseDateFormatter(ax.xaxis.get_major_locator())
455-
ax.xaxis.set_major_formatter(cdf)
455+
ax.xaxis.set_major_formatter(ConciseDateFormatter(ax.xaxis.get_major_locator()))
456456

457457
# %%
458458
# For more information see the date examples
@@ -506,6 +506,8 @@ def my_plotter(ax, data1, data2, param_dict):
506506
# Often we want to have a third dimension in a plot represented by a colors in
507507
# a colormap. Matplotlib has a number of plot types that do this:
508508

509+
from matplotlib.colors import LogNorm
510+
509511
X, Y = np.meshgrid(np.linspace(-3, 3, 128), np.linspace(-3, 3, 128))
510512
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)
511513

@@ -518,8 +520,7 @@ def my_plotter(ax, data1, data2, param_dict):
518520
fig.colorbar(co, ax=axs[0, 1])
519521
axs[0, 1].set_title('contourf()')
520522

521-
pc = axs[1, 0].imshow(Z**2 * 100, cmap='plasma',
522-
norm=mpl.colors.LogNorm(vmin=0.01, vmax=100))
523+
pc = axs[1, 0].imshow(Z**2 * 100, cmap='plasma', norm=LogNorm(vmin=0.01, vmax=100))
523524
fig.colorbar(pc, ax=axs[1, 0], extend='both')
524525
axs[1, 0].set_title('imshow() with LogNorm()')
525526

0 commit comments

Comments
 (0)