Skip to content

Commit

Permalink
When importing mpl do not rely on submodules being loaded
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
timhoffm committed Apr 24, 2024
1 parent d45dfbb commit 2e3614c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ per-file-ignores =
lib/mpl_toolkits/axisartist/angle_helper.py: E221

doc/conf.py: E402
galleries/users_explain/quick_start.py: E402
galleries/users_explain/artists/paths.py: E402
galleries/users_explain/artists/patheffects_guide.py: E402
galleries/users_explain/artists/transforms_tutorial.py: E402, E501
Expand Down
11 changes: 6 additions & 5 deletions galleries/users_explain/quick_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import numpy as np

# sphinx_gallery_thumbnail_number = 3
import matplotlib as mpl

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

from matplotlib.dates import ConciseDateFormatter

fig, ax = plt.subplots(figsize=(5, 2.7), layout='constrained')
dates = np.arange(np.datetime64('2021-11-15'), np.datetime64('2021-12-25'),
np.timedelta64(1, 'h'))
data = np.cumsum(np.random.randn(len(dates)))
ax.plot(dates, data)
cdf = mpl.dates.ConciseDateFormatter(ax.xaxis.get_major_locator())
ax.xaxis.set_major_formatter(cdf)
ax.xaxis.set_major_formatter(ConciseDateFormatter(ax.xaxis.get_major_locator()))

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

from matplotlib.colors import LogNorm

X, Y = np.meshgrid(np.linspace(-3, 3, 128), np.linspace(-3, 3, 128))
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)

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

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

Expand Down

0 comments on commit 2e3614c

Please sign in to comment.