本文整理汇总了Python中matplotlib.get_backend方法的典型用法代码示例。如果您正苦于以下问题:Python matplotlib.get_backend方法的具体用法?Python matplotlib.get_backend怎么用?Python matplotlib.get_backend使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类matplotlib
的用法示例。
在下文中一共展示了matplotlib.get_backend方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: args_validation
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import get_backend [as 别名]
def args_validation(args):
# # Test to see what matplotlib backend is setup
backend = matplotlib.get_backend()
if not backend == 'TkAgg':
log.warning('{} matplotlib backend in use. This graph generation was tested with "agg", bugs may lie ahead...'.format(backend))
# # Test to see if we should use defaults
if args.graphtype == 'all':
args.no_zero = __no_zero__
args.width = __width__
args.no_log = __g_log__
args.no_order = __no_order__
args.colours = __colours__
try:
args.colours[0] = matplotlib.colors.to_rgba(args.colours[0])
args.colours[1] = matplotlib.colors.to_rgba(args.colours[1])
except ValueError as e:
raise ArgValidationEx('Error parsing --colours: {}'.format(e))
示例2: switch_backend
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import get_backend [as 别名]
def switch_backend(backend):
def switch_backend_decorator(func):
@functools.wraps(func)
def backend_switcher(*args, **kwargs):
try:
prev_backend = mpl.get_backend()
matplotlib.testing.setup()
plt.switch_backend(backend)
return func(*args, **kwargs)
finally:
plt.switch_backend(prev_backend)
return backend_switcher
return switch_backend_decorator
示例3: draw
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import get_backend [as 别名]
def draw(self, filename=None, verbose=False):
self._draw_regs()
self._draw_ops(verbose)
_xl = - self._style.margin[0]
_xr = self._cond['xmax'] + self._style.margin[1]
_yb = - self._cond['ymax'] - self._style.margin[2] + 1 - 0.5
_yt = self._style.margin[3] + 0.5
self.ax.set_xlim(_xl, _xr)
self.ax.set_ylim(_yb, _yt)
# update figure size
fig_w = _xr - _xl
fig_h = _yt - _yb
if self._style.figwidth < 0.0:
self._style.figwidth = fig_w * self._scale * self._style.fs / 72 / WID
self.figure.set_size_inches(self._style.figwidth, self._style.figwidth * fig_h / fig_w)
if filename:
self.figure.savefig(filename, dpi=self._style.dpi,
bbox_inches='tight', facecolor=self.figure.get_facecolor())
if self.return_fig:
if get_backend() in ['module://ipykernel.pylab.backend_inline',
'nbAgg']:
plt.close(self.figure)
return self.figure
示例4: show_inline_matplotlib_plots
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import get_backend [as 别名]
def show_inline_matplotlib_plots():
"""Show matplotlib plots immediately if using the inline backend.
With ipywidgets 6.0, matplotlib plots don't work well with interact when
using the inline backend that comes with ipykernel. Basically, the inline
backend only shows the plot after the entire cell executes, which does not
play well with drawing plots inside of an interact function. See
https://github.com/jupyter-widgets/ipywidgets/issues/1181/ and
https://github.com/ipython/ipython/issues/10376 for more details. This
function displays any matplotlib plots if the backend is the inline backend.
"""
if 'matplotlib' not in sys.modules:
# matplotlib hasn't been imported, nothing to do.
return
try:
import matplotlib as mpl
from ipykernel.pylab.backend_inline import flush_figures
except ImportError:
return
if mpl.get_backend() == 'module://ipykernel.pylab.backend_inline':
flush_figures()
示例5: flush_inline_matplotlib_plots
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import get_backend [as 别名]
def flush_inline_matplotlib_plots():
"""
Flush matplotlib plots immediately, rather than asynchronously.
Basically, the inline backend only shows the plot after the entire
cell executes, which means we can't easily use a contextmanager to
suppress displaying it. See https://github.com/jupyter-widgets/ipywidgets/issues/1181/
and https://github.com/ipython/ipython/issues/10376 for more details. This
function displays flushes any pending matplotlib plots if we are using
the inline backend.
Stolen from https://github.com/jupyter-widgets/ipywidgets/blob/4cc15e66d5e9e69dac8fc20d1eb1d7db825d7aa2/ipywidgets/widgets/interaction.py#L35
"""
if 'matplotlib' not in sys.modules:
# matplotlib hasn't been imported, nothing to do.
return
try:
import matplotlib as mpl
from ipykernel.pylab.backend_inline import flush_figures
except ImportError:
return
if mpl.get_backend() == 'module://ipykernel.pylab.backend_inline':
flush_figures()
示例6: move_plt_figure
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import get_backend [as 别名]
def move_plt_figure(fig, x, y):
"""Move matplotlib figure to position
https://stackoverflow.com/a/37999370
Args:
fig: Figure handle
x: Position x
y: Position y
"""
plt_backend = matplotlib.get_backend()
if plt_backend == 'TkAgg':
fig.canvas.manager.window.wm_geometry("+%d+%d" % (x, y))
elif plt_backend == 'WXAgg':
fig.canvas.manager.window.SetPosition((x, y))
else:
# This works for QT and GTK
# You can also use window.setGeometry
fig.canvas.manager.window.move(x, y)
示例7: flush_inline_matplotlib_plots
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import get_backend [as 别名]
def flush_inline_matplotlib_plots():
"""
Flush matplotlib plots immediately, rather than asynchronously.
Basically, the inline backend only shows the plot after the entire
cell executes, which means we can't easily use a contextmanager to
suppress displaying it. See https://github.com/jupyter-widgets/ipywidgets/issues/1181/
and https://github.com/ipython/ipython/issues/10376 for more details. This
function displays flushes any pending matplotlib plots if we are using
the inline backend.
Stolen from https://github.com/jupyter-widgets/ipywidgets/blob/4cc15e66d5e9e69dac8fc20d1eb1d7db825d7aa2/ipywidgets/widgets/interaction.py#L35
"""
if 'matplotlib' not in sys.modules:
# matplotlib hasn't been imported, nothing to do.
return
try:
import matplotlib as mpl
from ipykernel.pylab.backend_inline import flush_figures
except ImportError:
return
# except KeyError:
# return
if mpl.get_backend() == 'module://ipykernel.pylab.backend_inline':
flush_figures()
示例8: switch_backend
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import get_backend [as 别名]
def switch_backend(backend):
# Local import to avoid a hard nose dependency and only incur the
# import time overhead at actual test-time.
def switch_backend_decorator(func):
@functools.wraps(func)
def backend_switcher(*args, **kwargs):
try:
prev_backend = mpl.get_backend()
matplotlib.testing.setup()
plt.switch_backend(backend)
result = func(*args, **kwargs)
finally:
plt.switch_backend(prev_backend)
return result
return _copy_metadata(func, backend_switcher)
return switch_backend_decorator
示例9: show
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import get_backend [as 别名]
def show(self, **kwargs):
self.plot_if_not_done_yet()
# in a notebook the plot method need not to be called explicitly
if not in_notebook() and matplotlib.get_backend() != "agg":
plt.show(**kwargs)
plt.close()
示例10: get_compatible_pyplot
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import get_backend [as 别名]
def get_compatible_pyplot(backend=None, debug=True):
"""Make the backend of MPL compatible.
In Travis Mac distributions, python is not installed as a framework. This
means that using the TkAgg backend is the best solution (so it doesn't
try to use the mac OS backend by default).
Parameters
----------
backend : str, optional (default="TkAgg")
The backend to default to.
debug : bool, optional (default=True)
Whether to log the existing backend to stderr.
"""
import matplotlib
# If the backend provided is None, just default to
# what's already being used.
existing_backend = matplotlib.get_backend()
if backend is not None:
# Can this raise?...
matplotlib.use(backend)
# Print out the new backend
if debug:
sys.stderr.write("Currently using '%s' MPL backend, "
"switching to '%s' backend%s"
% (existing_backend, backend, os.linesep))
# If backend is not set via env variable, but debug is
elif debug:
sys.stderr.write("Using '%s' MPL backend%s"
% (existing_backend, os.linesep))
from matplotlib import pyplot as plt
return plt
示例11: on_new_train_step
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import get_backend [as 别名]
def on_new_train_step(self, sample_id, data_buffer):
""" This is the listener main function, which gives it the ability to
'listen' for the caller. Whenever the EvaluationVisualiser should
be aware of some new data, the caller will invoke this function,
passing the new data buffer.
Parameters
----------
sample_id: int
The current sample id.
data_buffer: EvaluationDataBuffer
A buffer containing evaluation data for a single training / visualization step.
Raises
------
ValueError: If an exception is raised during the draw operation.
"""
try:
current_time = time.time()
self._clear_annotations()
self._update_plots(sample_id, data_buffer)
# To mitigate re-drawing overhead for fast models use frame counter (default = 5 frames).
# To avoid slow refresh rate in slow models use a time limit (default = 1 sec).
if (self._frame_cnt == 5) or (current_time - self._last_draw_timestamp > 1):
plt.subplots_adjust(right=0.72, bottom=0.22) # Adjust subplots to include metrics annotations
if get_backend() == 'nbAgg':
self.fig.canvas.draw() # Force draw in'notebook' backend
plt.pause(1e-9)
self._frame_cnt = 0
self._last_draw_timestamp = current_time
else:
self._frame_cnt += 1
except BaseException as exception:
raise ValueError('Failed when trying to draw plot. Exception: {} | Type: {}'.
format(exception, type(exception).__name__))
示例12: _import_matplotlib
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import get_backend [as 别名]
def _import_matplotlib():
"""Import matplotlib safely."""
# make sure that the Agg backend is set before importing any
# matplotlib
import matplotlib
matplotlib.use('agg')
matplotlib_backend = matplotlib.get_backend().lower()
filterwarnings("ignore", category=UserWarning,
message='Matplotlib is currently using agg, which is a'
' non-GUI backend, so cannot show the figure.')
if matplotlib_backend != 'agg':
raise ExtensionError(
"Sphinx-Gallery relies on the matplotlib 'agg' backend to "
"render figures and write them to files. You are "
"currently using the {} backend. Sphinx-Gallery will "
"terminate the build now, because changing backends is "
"not well supported by matplotlib. We advise you to move "
"sphinx_gallery imports before any matplotlib-dependent "
"import. Moving sphinx_gallery imports at the top of "
"your conf.py file should fix this issue"
.format(matplotlib_backend))
import matplotlib.pyplot as plt
return matplotlib, plt
示例13: switch_backend
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import get_backend [as 别名]
def switch_backend(backend):
def switch_backend_decorator(func):
def backend_switcher(*args, **kwargs):
try:
prev_backend = mpl.get_backend()
mpl.rcdefaults()
plt.switch_backend(backend)
result = func(*args, **kwargs)
finally:
plt.switch_backend(prev_backend)
return result
return nose.tools.make_decorator(func)(backend_switcher)
return switch_backend_decorator
示例14: move_figure
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import get_backend [as 别名]
def move_figure(f, x, y):
"""Move figure's upper left corner to pixel (x, y)"""
backend = matplotlib.get_backend()
if backend == 'TkAgg':
f.canvas.manager.window.wm_geometry("+%d+%d" % (x, y))
elif backend == 'WXAgg':
f.canvas.manager.window.SetPosition((x, y))
else:
# This works for QT and GTK
# You can also use window.setGeometry
f.canvas.manager.window.move(x, y)
示例15: _set_mpl_backend
# 需要导入模块: import matplotlib [as 别名]
# 或者: from matplotlib import get_backend [as 别名]
def _set_mpl_backend():
"""Make sure that we don't get DISPLAY problems when running without X
on unices
Code imported from nilearn (nilearn/nilearn/plotting/__init__.py)
"""
# We are doing local imports here to avoid polluting our namespace
import matplotlib
import os
import sys
# Set the backend to a non-interactive one for unices without X
if ((os.name == 'posix' and 'DISPLAY' not in os.environ
and not (sys.platform == 'darwin'
and matplotlib.get_backend() == 'MacOSX'))
or 'DISPLAY' in os.environ and os.environ['DISPLAY'] == '-1'):
matplotlib.use('Agg')