Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b34682c

Browse files
mdboomtacaswell
authored andcommitted
Merge pull request #5039 from megies/sphinxext_plot_directive_switch_backend
sphinxext pot_directive: more robust backend switching
1 parent cba1283 commit b34682c

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
import sys, os, shutil, io, re, textwrap
140140
from os.path import relpath
141141
import traceback
142+
import warnings
142143

143144
if not six.PY3:
144145
import cStringIO
@@ -166,8 +167,15 @@ def format_template(template, **kw):
166167

167168
import matplotlib
168169
import matplotlib.cbook as cbook
169-
matplotlib.use('Agg')
170-
import matplotlib.pyplot as plt
170+
try:
171+
with warnings.catch_warnings(record=True):
172+
warnings.simplefilter("error", UserWarning)
173+
matplotlib.use('Agg')
174+
except UserWarning:
175+
import matplotlib.pyplot as plt
176+
plt.switch_backend("Agg")
177+
else:
178+
import matplotlib.pyplot as plt
171179
from matplotlib import _pylab_helpers
172180

173181
__version__ = 2

0 commit comments

Comments
 (0)