8000 FIX: be more careful about not importing pyplot early · matplotlib/matplotlib@a5a1311 · GitHub
[go: up one dir, main page]

Skip to content

Commit a5a1311

Browse files
committed
FIX: be more careful about not importing pyplot early
In matplotlib.use we import pyplot to use `switch_backend`, however if the user calls `mpl.use(...)` before importing pyplot then during the initial import of pyplot, before we set the selected backend we try to set the backend set via rcParams. This change only imports pyplot if it is already imported, otherwise it is safe to just set the rcParams and not go through the full `plt.switch_backend` path. closes #17763
1 parent 4a143b9 commit a5a1311

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

lib/matplotlib/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,12 +1140,15 @@ def use(backend, *, force=True):
11401140
# Nothing to do if the requested backend is already set
11411141
pass
11421142
else:
1143-
try:
1144-
from matplotlib import pyplot as plt
1145-
plt.switch_backend(name)
1146-
except ImportError:
1147-
if force:
1148-
raise
1143+
if 'matplotlib.pyplot' in sys.modules:
1144+
try:
1145+
from matplotlib import pyplot as plt
1146+
plt.switch_backend(name)
1147+
except ImportError:
1148+
if force:
1149+
raise
1150+
else:
1151+
rcParams['backend'] = backend
11491152

11501153

11511154
if os.environ.get('MPLBACKEND'):

0 commit comments

Comments
 (0)
0