def fg(fig=None): """Raise figure to foreground.""" plt.figure((fig or plt.gcf()).number) if plt.get_backend()[0:2].lower() == 'qt': plt.get_current_fig_manager().window.activateWindow() plt.get_current_fig_manager().window.raise_() elif plt.get_backend()[0:2].lower() == 'wx': plt.get_current_fig_manager().window.Raise()
def __init__(self, calibrationfile, addon_path=None): # the plotting backend switch is usually required try: self.load_calibration(calibrationfile) except FileNotFoundError: self.rect = None # needed to reference the CommandCam.exe program on Windows self.addon_path = addon_path print('Plotting backend:', plt.get_backend()) print('Switching to Agg...') plt.switch_backend('Agg') print('Plotting backend:', plt.get_backend())
def radio_overlay_b1555(): """ Plots the NIR AO image in greyscale, and then overlays contours from two radio data sets: MERLIN and VLBA """ """ Set the input file names """ aoim = '../data/B1555_nirc2_n_Kp_6x6.fits' merlin_im = '../data/1555_merlin_5ghz.fits' vlbi_im = '../data/B1555_vlbi_fix_astrom.fits' """ Hardwire rms levels if needed """ rms_vlbi = 0.0001 """ Set the image center, origin location, and size """ racent = 239.29968 deccent = 37.359921 zeropos = (0.2276,0.2194) imsize = 1.2 # Value in arcsec """ Make the overlay plot """ imf.overlay_contours(aoim,merlin_im,racent,deccent,imsize, showradec=False,fmax=6.,zeropos=zeropos, infile3=vlbi_im,rms3=rms_vlbi,ccolor3='b') """ Set up the font """ if plt.get_backend() == 'MacOSX': font = {'style' : 'normal', 'color' : 'black', 'weight' : 'bold', 'size' : 24, } else: print 'Using backend %s' % (plt.get_backend()) font = {'family' : 'serif', 'style' : 'normal', 'color' : 'black', 'weight' : 'bold', 'size' : 24, } """ Label the lensed images, taking into account a possible shift in origin, which would be set by the zeropos position """ labx = n.array([0.33, 0.17, -0.25, 0.16]) laby = n.array([0.30, 0.36, 0.25, -0.24]) labx -= zeropos[0] laby -= zeropos[1] labt = ['A', 'B', 'C', 'D'] for i in range(len(labx)): plt.text(labx[i],laby[i],labt[i],fontdict=font)
def test(show=False, coverage=False): """Test all code examples in docstrings using pytest.""" try: import pytest except ImportError: raise ImportError("pytest is required to run test suite. " + "Try 'sudo pip install pytest'.") from matplotlib import pyplot as plt from pygimli.utils import opt_import pc = opt_import("pytest_cov", "create a coverage report") old_backend = plt.get_backend() if not show: plt.switch_backend("Agg") cwd = __path__[0] cfg = os.path.join(cwd, "../tests/setup.cfg") cmd = "" if os.path.exists(cfg): cmd += "-c %s " % cfg if pc and coverage: cmd += "--cov pygimli --cov-report coveralls --cov-report html " + "--cov-config %s " % cfg.replace( "setup.cfg", ".coveragerc" ) cmd += "%s" % cwd try: pytest.main(cmd) finally: plt.switch_backend(old_backend)
def __init__(self, fig, event_source=None, blit=False): self._fig = fig if blit and plt.get_backend().lower() == 'macosx': raise BackendError('''The current backend is 'MacOSX' and may go into an infinite loop with blit turned on. Either turn off blit or use an alternate backend, for example, like 'TKAgg', using the following prepended to your source code: import matplotlib matplotlib.use('TKAgg') ''') self._blit = blit # These are the basics of the animation. The frame sequence represents # information for each frame of the animation and depends on how the # drawing is handled by the subclasses. The event source fires events # that cause the frame sequence to be iterated. self.frame_seq = self.new_frame_seq() self.event_source = event_source # Clear the initial frame self._init_draw() # Instead of starting the event source now, we connect to the figure's # draw_event, so that we only start once the figure has been drawn. self._first_draw_id = fig.canvas.mpl_connect('draw_event', self._start) # Connect to the figure's close_event so that we don't continue to # fire events and try to draw to a deleted figure. self._close_id = self._fig.canvas.mpl_connect('close_event', self._stop) if blit: self._setup_blit()
def matplotlib_config(): """Configure matplotlib for viz tests.""" import matplotlib # "force" should not really be necessary but should not hurt kwargs = dict() if 'warn' in _get_args(matplotlib.use): kwargs['warn'] = False matplotlib.use('agg', force=True, **kwargs) # don't pop up windows import matplotlib.pyplot as plt assert plt.get_backend() == 'agg' # overwrite some params that can horribly slow down tests that # users might have changed locally (but should not otherwise affect # functionality) plt.ioff() plt.rcParams['figure.dpi'] = 100 try: from traits.etsconfig.api import ETSConfig except Exception: pass else: ETSConfig.toolkit = 'qt4' try: with warnings.catch_warnings(record=True): # traits from mayavi import mlab except Exception: pass else: mlab.options.backend = 'test'
def start_jobs(): """ Restores the plots if requested and if the persistent files exist and starts the qt timer of the 1st plot. """ for plot in _plots: if plot.persistentName: plot.restore_plots() plot.fig.canvas.set_window_title(plot.title) runCardVals.iteration = np.long(0) noTimer = len(_plots) == 0 or\ (plt.get_backend().lower() in (x.lower() for x in mpl.rcsetup.non_interactive_bk)) if noTimer: print("The job is running... ") while True: msg = '{0} of {1}'.format( runCardVals.iteration+1, runCardVals.repeats) if os.name == 'posix': sys.stdout.write("\r\x1b[K " + msg) else: sys.stdout.write("\r ") print(msg+' ') sys.stdout.flush() res = dispatch_jobs() if res: return else: plot = _plots[0] plot.areProcessAlreadyRunning = False plot.timer = plot.fig.canvas.new_timer() plot.timer.add_callback(plot.timer_callback) plot.timer.start()
def my_qunt_plot(qx): fig = plt.figure(figsize=(10, 6)) mpl_agg = plt.get_backend().lower() if 'tk' in mpl_agg: # Option 2 # TkAgg backend manager = plt.get_current_fig_manager() manager.resize(*manager.window.maxsize()) elif 'qt' in mpl_agg: # Option 1 # QT backend manager = plt.get_current_fig_manager() manager.window.showMaximized() elif 'wx' in mpl_agg: # Option 3 # WX backend manager = plt.get_current_fig_manager() manager.frame.Maximize(True) df = pd.read_csv(qx.fn_qxLib, index_col=0, parse_dates=[0]) # ---top.plt # fig = plt.figure(figsize=(20, 15)) ax1 = fig.add_subplot(111) ax1.plot(df['dret'],color='green',label='dret',linewidth=0.5) ax1.legend(loc='upper left') ax2 = ax1.twinx() ax2.plot(df['val'], color='red', label='val', linewidth=2) ax2.legend(loc='upper right') plt.tight_layout() plt.show()
def set_up_plot(self, settings=None): self.plot = Plot() if settings is not None: for key in settings: self.plot.settings[key] = settings[key] self.settings['plot'] = self.plot.settings n_rows = self.plot.settings['n_rows'] n_cols = self.plot.settings['n_cols'] if n_rows is not None and n_cols is not None: print '\nSetting up {0:d}x{1:d} plot.'.format(n_rows, n_cols) else: e_str = 'Number of {0:s} must be an integer > 0.' n_rows, n_cols = (0, 0) while n_rows < 1 or n_cols < 1: n_rows = utils.get_input_integer( \ '\nNumber of subplot rows?\n> ', error_text=e_str.format('rows'))[0] n_cols = utils.get_input_integer( \ 'Number of subplot columns?\n> ', error_text=e_str.format('columns'))[0] if n_rows < 1 or n_cols < 1: print 'Must have > 0 rows and columns.' self.plot.set_up_plot_grid(n_rows, n_cols) #self.plot.plot_grid.tight_layout(self.plot.figure) self.plot.figure.set_tight_layout(True) plt.show(block=False) print '(If you cannot see the plot, try changing the ' print 'matplotlib backend. Current backend is ' + \ plt.get_backend() + '.)'
def _set_backend(): """Choose the backend and get the GUI elements needed for it.""" backend = _p.get_backend() if not backend.startswith('GTK'): if _p.rcParams['backend_fallback']: if backend.endswith('Agg'): backend = 'GTKAgg' elif backend.endswith('Cairo'): backend = 'GTKCairo' else: backend = 'GTK' else: raise NotImplementedError, """ You must use a GTK-based backend with refigure. Adjust your matplotlibrc file, or before importing refigure run >>> from matplotlib import use >>> use( < 'GTK' | 'GTKAgg' | 'GTKCairo' > ) """ gui_elements = ['FigureCanvas'+backend, 'NavigationToolbar2'+backend] if backend == 'GTKCairo': gui_elements[1] = 'NavigationToolbar2GTK' temp = __import__('matplotlib.backends.backend_' + backend.lower(), globals(), locals(), gui_elements) canvas = getattr(temp, gui_elements[0]) toolbar = getattr(temp, gui_elements[1]) return backend, canvas, toolbar
def on_window_close(figure, function): """Connects a close figure signal to a given function Parameters ---------- figure : mpl figure instance function : function """ window = figure.canvas.manager.window backend = plt.get_backend() if backend == 'GTKAgg': def function_wrapper(*args): function() window.connect('destroy', function_wrapper) elif backend == 'WXAgg': # In linux the following code produces a segmentation fault # so it is enabled only for Windows import wx def function_wrapper(event): function() plt.close(figure) window.Bind(wx.EVT_CLOSE, function_wrapper) elif backend == 'TkAgg': def function_wrapper(*args): function() figure.canvas.manager.window.bind("<Destroy>", function_wrapper) elif backend == 'Qt4Agg': from PyQt4.QtCore import SIGNAL window = figure.canvas.manager.window window.connect(window, SIGNAL('destroyed()'), function)
def movie(self,di=10, coord='latlon',land="nice", heatmap=False): curr_backend = plt.get_backend() plt.switch_backend('Agg') FFMpegWriter = animation.writers['ffmpeg'] metadata = dict(title='%s %s' % (self.projname, self.casename), artist='pytraj', comment='https://github.com/TRACMASS/pytraj') writer = FFMpegWriter(fps=15, metadata=metadata) fig = plt.figure() with writer.saving(fig, "traj_test.mp4", 200): for part in self.partvec: self.load(part=part) jdvec = np.sort(self.jdvec) for jd in jdvec: print part, jdvec[-1] - jd, len(self.jd[self.jd==jd]) if len(self.jd[self.jd==jd]) <= 1: continue if jd/di == float(jd)/di: if heatmap == True: self.heatmap(log=True, jd=jd) else: self.scatter(jd=jd, coord=coord, land=land) writer.grab_frame() plt.switch_backend(curr_backend)
def __init__(self, f=None, reader=None, interactive=True, toolbar=True, *args, **kwargs): # Read in data if f is not None: if reader is None: self.reader = DefaultReader(f, *args, **kwargs) else: self.reader = reader(f, *args, **kwargs) self.data = self.reader.init_data(*args, **kwargs) self._initialized = True else: self.reader = None self._initialized = False # `interactive` determines if the MPL event loop is used or a # raw figure is made. Set to False if using an external event handler, # e.g. if embedding in a separate program. self.interactive = interactive # Static version is not threaded, but want to make sure any subclasses # are thread-safe self.lock = threading.RLock() # Need to keep track of the backend, since not all backends support # all update schemes self._backend = plt.get_backend().lower() self.fig = self._create_fig(toolbar=toolbar) self.axes = None self.canvas = self.fig.canvas self.mode = 'none' self._plotdict = {'autoscalex': True, # Autoscale is meaningless in 'autoscaley': True, # Static, but useful in RT 'windowsize': None}
def test16(self): '''test that makes a real time graph resembling a sine graph ''' # note that by using list comprehension the first 20 points are auto plotted fig = plt.figure(1) import math my = [] t = range(-50, 50) for item in t: b = math.sin(item) my.append(b) mylist = array(my) plt.cla() plt.plot(my[-20:], '-r') #analyzing the plot components a = plt.get_backend() c = plt.isinteractive() # analyzing the axis commands z = plt.axis() v = plt.get_plot_commands() #plt.draw() fig.canvas.draw() self.logger.debug("PLT.GET_NEXT_COMMANDS OUTPUT RESPONS: " + repr(v) ) self.logger.debug("PLT.GET_BACKEND OUTPUT: " + repr(a) ) #self.logger.debug("PLT.GET_NEXT_COMMANDS OUTPUT RESPONS: " + repr(d) ) self.logger.debug("PLT.AXIS COMMAND OUTPUTANSWER TO PLT.AXIS: " + repr(z) )
def movie(self, fldname, jd1=None, jd2=None, jdvec=None, fps=10, **kwargs): curr_backend = plt.get_backend() plt.switch_backend('Agg') FFMpegWriter = animation.writers['ffmpeg'] metadata = dict(title='%s' % (self.projname), artist=self.projname, comment='https://github.com/brorfred/njord') writer = FFMpegWriter(fps=fps, metadata=metadata, extra_args=['-vcodec', 'libx264',"-pix_fmt", "yuv420p"]) jdvec = self.get_tvec(jd1, jd2) if jdvec is None else jdvec fig = plt.figure() with writer.saving(fig, "%s.mp4" % self.projname, 200): for jd in jdvec: pl.clf() print(pl.num2date(jd).strftime("%Y-%m-%d %H:%M load "), end="") sys.stdout.flush() try: fld= self.get_field(fldname, jd=jd) except: print("not downloaded" % jd) continue print("plot ", end="") sys.stdout.flush() self.pcolor(fld, **kwargs) pl.title(pl.num2date(jd).strftime("%Y-%m-%d %H:%M")) print("write") writer.grab_frame()#bbox_inches="tight", pad_inches=0) plt.switch_backend(curr_backend)
def plot_plot(self, desk, pictureName, stay=False): mng = plt.get_current_fig_manager() ''' if desk.isWindows: mng.window.state('zoomed') #works fine on Windows! ''' if plt.get_backend() == 'TkAgg': try: mng.window.state('zoomed') # #works fine on Windows! except: try: mng.frame.Maximize(True) except: try: mng.window.showMaximized() except: # print "Could not maximize" mng.window.wm_geometry("1400x900+50+50") if stay and not desk.saveGraph: plt.show() else: plt.show(block=False) if desk.saveGraph: plt.savefig(pictureName, format=desk.imgType, dpi=desk.dpi) if not desk.showGraph: plt.close()
def wrapped(*args, **kwargs): orig_backend = plt.get_backend() plt.switch_backend('agg') mpl_setup() if pyplot_helpers.Gcf.figs: warnings.warn('Figures existed before running the %s %s test.' ' All figures should be closed after they run. ' 'They will be closed automatically now.' % (mod_name, test_name)) pyplot_helpers.Gcf.destroy_all() if MPL_VERSION >= '2': style_context = mpl.style.context else: @contextlib.contextmanager def style_context(style, after_reset=False): yield with style_context(self.style): r = test_func(*args, **kwargs) fig_managers = pyplot_helpers.Gcf._activeQue figures = [manager.canvas.figure for manager in fig_managers] try: self.run_figure_comparisons(figures, test_name=mod_name) finally: for figure in figures: pyplot_helpers.Gcf.destroy_fig(figure) plt.switch_backend(orig_backend) return r
def tight_layout(pad=1.2, h_pad=None, w_pad=None, fig=None): """ Adjust subplot parameters to give specified padding. Note. For plotting please use this function instead of plt.tight_layout Parameters ---------- pad : float padding between the figure edge and the edges of subplots, as a fraction of the font-size. h_pad, w_pad : float padding (height/width) between edges of adjacent subplots. Defaults to `pad_inches`. """ import matplotlib.pyplot as plt if fig is None: fig = plt.gcf() try: # see https://github.com/matplotlib/matplotlib/issues/2654 fig.canvas.draw() fig.tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad) except: msg = ('Matplotlib function \'tight_layout\'%s.' ' Skipping subpplot adjusment.') if not hasattr(plt, 'tight_layout'): case = ' is not available' else: case = (' is not supported by your backend: `%s`' % plt.get_backend()) warn(msg % case)
def _setup_timers(self): """Set up timers to limit call-rate and avoid "flickering" effect.""" self.timer_expired = {} self.ax_timer = {} # Timer to control call-rate. def expire_func(ax, *args, **kwargs): self.timer_expired[ax] = True # Return True to keep callback return True for ax in self.axes: interval = 300 if self.hover else 100 try: self.ax_timer[ax] = ax.figure.canvas.new_timer( interval=interval, callbacks=[(expire_func, [ax], {})], ) except AttributeError: # Some backends don't have timers at all! Starting/stopping # will raise an AttributeError, but this is caught regardless # as some backend's timers don't support start/stop. self.ax_timer[ax] = None try: if plt.get_backend() != 'MacOSX': # Single-shot timers on the OSX backend segfault! self.ax_timer[ax].single_shot = True except AttributeError: # For mpl <= 1.3.1 with the wxAgg backend, setting the # timer to be single_shot will raise an error that can be # safely ignored. pass self.timer_expired[ax] = True
def test(target=None, show=False, onlydoctests=False, coverage=False, htmlreport=False): """Run docstring examples and additional tests. Examples -------- >>> from pygimli.utils import boxprint >>> test(target=boxprint) Parameters ---------- target : function, optional Function or method to test. By default everything is tested. show : boolean, optional Show matplotlib windows during test run. They will be closed automatically. onlydoctests : boolean, optional Run test files in ../tests as well. coverage : boolean, optional Create a coverage report. Requires the pytest-cov plugin. htmlreport : str, optional Filename for HTML report such as www.pygimli.org/build_tests.html. Requires pytest-html plugin. """ if target: import doctest doctest.run_docstring_examples(target, globals()) return try: import pytest except ImportError: raise ImportError("pytest is required to run test suite. " + \ "Try 'sudo pip install pytest'.") from matplotlib import pyplot as plt from pygimli.utils import opt_import pc = opt_import("pytest_cov", "create a code coverage report") ph = opt_import("pytest_html", "create a html report") old_backend = plt.get_backend() if not show: plt.switch_backend("Agg") cwd = os.path.realpath(__path__[0]) cfg = os.path.join(cwd, "../tests/setup.cfg") cmd = "" if os.path.exists(cfg): cmd += "-c %s " % cfg if pc and coverage: cmd += "--cov pygimli --cov-report term " + \ "--cov-config %s " % cfg.replace("setup.cfg", ".coveragerc") if ph and htmlreport: cmd += "--html %s " % htmlreport cmd += "%s " % cwd if not onlydoctests and os.path.exists(cfg): cmd += os.path.join(cwd, "../tests") exitcode = pytest.main(cmd) plt.switch_backend(old_backend) plt.close('all') sys.exit(exitcode)
def load_nb(cls, inline=True): """ Initialize matplotlib backend """ import matplotlib.pyplot as plt backend = plt.get_backend() if backend not in ['agg', 'module://ipykernel.pylab.backend_inline']: plt.switch_backend('agg')
def savefig(self,*args,**kwargs): backend = plt.get_backend() plt.switch_backend('Agg') try: self.draw() plt.savefig(*args,**kwargs) finally: plt.switch_backend(backend)
def __init__(self, backend): import matplotlib.pyplot as plt from IPython.core.interactiveshell import InteractiveShell from IPython.core.pylabtools import backend2gui self.shell = InteractiveShell.instance() self.old_backend = backend2gui[str(plt.get_backend())] self.new_backend = backend
def maximize_plot_window(): fig_manager = plt.get_current_fig_manager() backend_name = plt.get_backend().lower() if backend_name.find('qt') >= 0: fig_manager.window.showMaximized() elif backend_name.find('tk') >= 0: maxsz = fig_manager.window.maxsize() fig_manager.resize(maxsz[0] - 80, maxsz[1] - 80)
def set_agg_backend(): """Fixture to ensure the Agg backend is active.""" import matplotlib.pyplot as plt prev_backend = plt.get_backend() try: plt.switch_backend('agg') yield finally: plt.switch_backend(prev_backend)
def on_figure_window_close(figure, function): """Connects a close figure signal to a given function Parameters ---------- figure : mpl figure instance function : function """ backend = plt.get_backend() if backend not in ("GTKAgg", "WXAgg", "TkAgg", "Qt4Agg"): return window = figure.canvas.manager.window if not hasattr(figure, "_on_window_close"): figure._on_window_close = list() if function not in figure._on_window_close: figure._on_window_close.append(function) if backend == "GTKAgg": def function_wrapper(*args): function() window.connect("destroy", function_wrapper) elif backend == "WXAgg": # In linux the following code produces a segmentation fault # so it is enabled only for Windows import wx def function_wrapper(event): # When using WX window.connect does not supports multiple funtions for f in figure._on_window_close: f() plt.close(figure) window.Bind(wx.EVT_CLOSE, function_wrapper) elif backend == "TkAgg": def function_wrapper(*args): # When using TK window.connect does not supports multiple funtions for f in figure._on_window_close: f() figure.canvas.manager.window.bind("<Destroy>", function_wrapper) elif backend == "Qt4Agg": # PyQt # In PyQt window.connect supports multiple funtions from IPython.external.qt_for_kernel import QtCore window.connect(window, QtCore.SIGNAL("closing()"), function) else: raise AttributeError("The %s backend is not supported. " % backend)
def plot(self, **kwargs): ''' Plot the spectrum Parameters ---------- show : bool If True (the default), then run the matplotlib.pyplot show command to display the plot. Disable this if you are running from a script and wish to delay showing the plot. Other keyword arguments are passed to the matplotlib plot command. ''' import matplotlib.pyplot as plt from ..analysis.interactive_plot import PlotWrapNav ax = plt.gca() fig = plt.gcf() artists = {} ax.axhline(0, color='k', lw=0.5) show = kwargs.pop('show', True) kwargs.update(color='0.8') artists['fl'] = ax.plot(self.dispersion, self.flux, drawstyle='steps-mid', **kwargs)[0] if self.sig is not None: kwargs.update(color='g') ax.plot(self.dispersion, self.sig, **kwargs) if hasattr(self,'co'): if self.co is not None: kwargs.update(color='r') ax.plot(self.dispersion, self.co, **kwargs) ax.set_ylim(*get_flux_plotrange(self.flux)) ax.set_xlim(self.dispersion.value[0], self.dispersion.value[-1]) if plt.get_backend() == 'MacOSX': warnings.warn("""\ Looks like you're using the MacOSX matplotlib backend. Switch to the TkAgg or QtAgg backends to enable all interactive plotting commands. """) else: # Enable xspecplot-style navigation (i/o for zooming, etc). # Need to save this as an attribute so it doesn't get # garbage-collected. self._plotter = PlotWrapNav(fig, ax, self.dispersion, self.flux, artists, printhelp=False) if show: plt.show()
def matplotlib_config(): """Configure matplotlib for viz tests.""" import matplotlib matplotlib.use('agg') # don't pop up windows import matplotlib.pyplot as plt assert plt.get_backend() == 'agg' # overwrite some params that can horribly slow down tests that # users might have changed locally (but should not otherwise affect # functionality) plt.ioff() plt.rcParams['figure.dpi'] = 100
def init(parameters=None): global __backend global __parameters __backend = plot.get_backend() __parameters = matplotlib.rcParams.copy() plot.switch_backend("pgf") matplotlib.rcParams.update(pgf_parameters) if parameters is not None: matplotlib.rcParams.update(parameters)
def run(self, interactive=True, block=True): """Run the monitor """ # check backend if get_backend() not in interactive_bk: interactive = False # run interactive with show() if interactive: return self.run_interactive() # run non-interactive with save() else: return self.run_noninteractive()
def plot_single_day_traffic(df): y_tj_l1 = df["tj_level1_count"] y_tj_l2 = df["tj_level2_count"] y_tj_l3 = df["tj_level3_count"] y_tj_l4 = df["tj_level4_count"] x_time = df["time_slices"] x_district = df["district"] fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(x_time, x_district, y_tj_l1) # ax.plot_surface(x_time, x_district, y_tj_l1) print(plt.get_backend()) #plt.show() plt.savefig("plot_traffic.png")
def _maximize_window(): # https://stackoverflow.com/questions/12439588/how-to-maximize-a-plt-show-window-using-python plt_backend = plt.get_backend() mng = plt.get_current_fig_manager() if "Qt" in plt_backend: mng.window.showMaximized() return True elif "wx" in plt_backend: mng.frame.Maximize(True) return True elif "Tk" in plt_backend: mng.window_state('zoomed') return True else: print("figure out how to maximize for ", plt_backend) return False
def maximize_plot() -> None: backend = plt.get_backend() mng = plt.get_current_fig_manager() backend_ = backend.lower() if backend_ == 'tkagg': if os.name == 'posix': mng.resize(*mng.window.maxsize()) else: mng.window.state('zoomed') elif backend_ == 'wxagg': mng.frame.Maximize(True) elif re.match('qt[0-9]*agg', backend_): mng.window.showMaximized() else: raise RuntimeError(f"Backend {backend} is not supported")
def draw(iteration, mesh, cost_to_go, policy): # Drawing is slow, don't draw every frame. if iteration % 20 != 0: return plt.suptitle("iteration " + str(iteration)) J = np.reshape(cost_to_go, Q.shape) surf = ax1.plot_surface(Q, Qdot, J, rstride=1, cstride=1, cmap=cm.jet) Pi = np.reshape(policy, Q.shape) surf2 = ax2.plot_surface(Q, Qdot, Pi, rstride=1, cstride=1, cmap=cm.jet) if plt.get_backend() != u"template": fig.canvas.draw() plt.pause(1e-10) fig.savefig('img/quad/'+str(iteration)+'.png') surf.remove() surf2.remove()
def draw(iteration, mesh, cost_to_go, policy): # Drawing is slow, don't draw every frame. if iteration % 20 != 0: return plt.title("iteration " + str(iteration)) J = np.reshape(cost_to_go, Q.shape) surf = ax.plot_surface(Q, Qdot, J, rstride=1, cstride=1, cmap=cm.jet) Pi = np.reshape(policy, Q.shape) surf2 = ax2.plot_surface(Q, Qdot, Pi, rstride=1, cstride=1, cmap=cm.jet) if plt.get_backend() != u"template": plt.draw_all() plt.pause(0.00001) surf.remove() surf2.remove()
def show_plot(figure_id=None): import matplotlib.pyplot as plt if figure_id is not None: fig = plt.figure(num=figure_id) else: fig = plt.gcf() pass fig.canvas.draw() if 'qt' in plt.get_backend().lower(): plt.show(block=False) plt.pause(1e-7) fig.canvas.manager.window.activateWindow() fig.canvas.manager.window.raise_() else: plt.show() plt.pause(1e-7) pass
def on_figure_window_close(figure, function): """Connects a close figure signal to a given function Parameters ---------- figure : mpl figure instance function : function """ window = figure.canvas.manager.window backend = plt.get_backend() if not hasattr(figure, '_on_window_close'): figure._on_window_close = list() if function not in figure._on_window_close: figure._on_window_close.append(function) if backend == 'GTKAgg': def function_wrapper(*args): function() window.connect('destroy', function_wrapper) elif backend == 'WXAgg': # In linux the following code produces a segmentation fault # so it is enabled only for Windows import wx def function_wrapper(event): for f in figure._on_window_close: f() plt.close(figure) window.Bind(wx.EVT_CLOSE, function_wrapper) elif backend == 'TkAgg': def function_wrapper(*args): function() figure.canvas.manager.window.bind("<Destroy>", function_wrapper) elif backend == 'Qt4Agg': from PyQt4.QtCore import SIGNAL window = figure.canvas.manager.window window.connect(window, SIGNAL('destroyed()'), function)
def main(): print('backend is', plt.get_backend()) # Set figure size (inches) plt.rcParams['figure.figsize'] = [24., 15.] rclpy.init() node = PlotControlNode() try: rclpy.spin(node) except KeyboardInterrupt: node.get_logger().info('ctrl-C detected, shutting down') # TODO notify consumer thread for a clean shutdown finally: node.destroy_node() rclpy.shutdown()
def __call__(self, i): if plt.get_backend().lower() == "agg": fig = Figure(figsize=(self._fig_width_in_inches, self._fig_height_in_inches)) else: fig = plt.figure(figsize=(self._fig_width_in_inches, self._fig_height_in_inches)) overlay_ax = fig.add_subplot( self._grid_spec[:self._image_height - self._height_padding, self._overlay_lim[0]:self._overlay_lim[1]]) cbar_ax = fig.add_subplot( self._grid_spec[:self._image_height - self._height_padding, self._colorbar_lim[0]:self._colorbar_lim[1]]) cbar_ax.set_title(self._colorbar_title, y=1.04) xy_ax = fig.add_subplot( self._grid_spec[:self._image_height - self._height_padding, self._xy_lim[0]:self._xy_lim[1]], **self._xy_axes_options) image_filename = self._image_filenames[i] dic_filename = self._dic_filenames[i] vmax, vmin = self._set_contour_levels(dic_filename) plot_overlay(image_filename, load_dic_data(dic_filename), self._variable, ax=overlay_ax, contour_options=self._overlay_contour_options) plot_colorbar(self._cmap, vmin, vmax, ax=cbar_ax, colorbar_options=self._colorbar_options) plot_xy(self._x_data, self._y_data, ax=xy_ax, axes_options=self._xy_axes_options, plot_options=self._xy_plot_options) plot_xy([self._x_data[i]], [self._y_data[i]], ax=xy_ax, plot_options=self._point_plot_options) return fig
def _plot(gp, X, Y, sampled_X, sampled_Y, sampled_sigma, reference): x_margin = 0 XX = np.atleast_2d(np.linspace(0 - x_margin, 1 + x_margin, 1000)).T YY, sigma = gp.predict(XX, return_std=True) ex_imp = (np.array([(reference - m) / s for m, s in zip(YY, sigma)])) sampled_ex_imp = (np.array([(reference - m) / s for m, s in zip(sampled_Y, sampled_sigma)])) plt.figure() plt.xlim(0 - x_margin, 1 + x_margin) plt.plot(XX, ex_imp, 'b-', label='exp. improvement') plt.plot(sampled_X, sampled_ex_imp, 'g.', markersize=10, label='sampled exp. improvement') plt.figure() plt.xlim(0 - x_margin, 1 + x_margin) plt.plot(sampled_X, sampled_Y, 'g.', markersize=10, label='Observations') plt.plot(XX, YY, 'b-', label='Prediction') plt.fill(np.concatenate([XX, XX[::-1]]), np.concatenate([YY - 1.9600 * sigma, (YY + 1.9600 * sigma)[::-1]]), alpha=.5, fc='b', ec='None', label='95% confidence interval') plt.fill(np.concatenate([XX, XX[::-1]]), np.concatenate([YY - 1 * sigma, (YY + 1 * sigma)[::-1]]), alpha=.5, fc='b', ec='None', label='sigma interval') plt.plot(X, Y, 'r.', markersize=10, label='Observations') plt.xlabel('$x$') plt.ylabel('$f(x)$') plt.legend(loc='lower center') print(plt.get_backend()) plt.show()
def matplotlib_config(): """Configure matplotlib for viz tests.""" import matplotlib matplotlib.use('agg') # don't pop up windows import matplotlib.pyplot as plt assert plt.get_backend() == 'agg' # overwrite some params that can horribly slow down tests that # users might have changed locally (but should not otherwise affect # functionality) plt.ioff() plt.rcParams['figure.dpi'] = 100 try: with warnings.catch_warnings(record=True): # traits from mayavi import mlab except Exception: pass else: mlab.options.backend = 'test'
def __init__(self, system, **kwargs): self.__dict__.update(self._params) # Default parameters self.__dict__.update(kwargs) # Modify parameters # Colors of the +ve/-ve conductance regions # First is +ve conductance, each successive is -ve conductance in the # next timescale self.colors = ['C0', 'C3', 'C1', 'C6'] self.system = system # associate GUI with a neuron or a network self.V = np.arange(self.vmin, self.vmax, self.dv) # Extended voltage range for finding vrest vrange = self.vmax - self.vmin self.V_extended = np.arange(self.vmin - vrange / 2, self.vmax + vrange / 2, self.dv) self.i_app_const = self.i0 self.i_app = lambda t: self.i_app_const self.IV_curves = [] # list of IV curve objects self.IV_size = 0 # Initial fixed point (guess) self.v_rest = 0 self.I_ss_rest = 0 # Create empty plot plt.close("all") self.fig = plt.figure() # Fix window size bck = plt.get_backend() if (bck == "Qt5Agg" or bck == "Qt4Agg"): win = self.fig.canvas.window() win.setFixedSize(win.size()) else: print("IMPORTANT: Resizing figure is not supported") self.axs_iv = [] # list of IV curve axis self.axsim = None # simulation plot axis self.pause_value = False
def plt_maximize(): # See discussion: https://stackoverflow.com/questions/12439588/how-to-maximize-a-plt-show-window-using-python backend = plt.get_backend() cfm = plt.get_current_fig_manager() if backend == "wxAgg": cfm.frame.Maximize(True) elif backend == "TkAgg": if system() == "win32": cfm.window.state('zoomed') # This is windows only else: cfm.resize(*cfm.window.maxsize()) elif backend == 'QT4Agg': cfm.window.showMaximized() elif callable(getattr(cfm, "full_screen_toggle", None)): if not getattr(cfm, "flag_is_max", None): cfm.full_screen_toggle() cfm.flag_is_max = True else: raise RuntimeError("plt_maximize() is not implemented for current backend:", backend)
def __init__(self, logpath, secondary_screen=False): """Initialize the cockpit plotter. Args: logpath (str): Full path to the JSON logfile. secondary_screen (bool): Whether to plot other experimental quantities on a secondary screen. """ # Split (and store) logpath up to indentify testproblem, data set, etc. self.__dict__ = utils_plotting._split_logpath(logpath) self._mpl_default_backend = plt.get_backend() self._mpl_no_show_backend = "Agg" self._secondary_screen = secondary_screen # Set plotting parameters self._set_plotting_params() self._set_layout_params()
def matplotlib_config(): """Configure matplotlib for viz tests.""" import matplotlib from matplotlib import cbook # Allow for easy interactive debugging with a call like: # # $ MNE_MPL_TESTING_BACKEND=Qt5Agg pytest mne/viz/tests/test_raw.py -k annotation -x --pdb # noqa: E501 # try: want = os.environ['MNE_MPL_TESTING_BACKEND'] except KeyError: want = 'agg' # don't pop up windows with warnings.catch_warnings(record=True): # ignore warning warnings.filterwarnings('ignore') matplotlib.use(want, force=True) import matplotlib.pyplot as plt assert plt.get_backend() == want # overwrite some params that can horribly slow down tests that # users might have changed locally (but should not otherwise affect # functionality) plt.ioff() plt.rcParams['figure.dpi'] = 100 try: from traits.etsconfig.api import ETSConfig except Exception: pass else: try: ETSConfig.toolkit = 'qt4' except Exception: pass # 'null' might be the only option in some configs # Make sure that we always reraise exceptions in handlers orig = cbook.CallbackRegistry class CallbackRegistryReraise(orig): def __init__(self, exception_handler=None): args = () if LooseVersion(matplotlib.__version__) >= LooseVersion('2.1'): args += (exception_handler, ) super(CallbackRegistryReraise, self).__init__(*args) cbook.CallbackRegistry = CallbackRegistryReraise
def show_scatter(self) -> None: self.__format_axis() fig, scatter_plot = plt.subplots() if plt.get_backend() == "TkAgg": fig.suptitle("Scatter Graph: " + str(self.__grid_size) + " grid size") # This sets the boundaries of the graph plt.axis([self.__area_coordinates[0], self.__area_coordinates[2], self.__area_coordinates[1], self.__area_coordinates[3]]) self.__set_axis_ticks() plt.grid(True) scatter_plot.scatter(self.__points_x, self.__points_y) plt.show(block=False)
def maximize_window(): # See discussion: https://stackoverflow.com/questions/12439588/how-to-maximize-a-plt-show-window-using-python backend = plt.get_backend() cfm = plt.get_current_fig_manager() if backend == "wxAgg": cfm.frame.Maximize(True) elif backend == "TkAgg": if system() == "win32": cfm.window.state('zoomed') # This is windows only else: cfm.resize(*cfm.window.maxsize()) elif backend == 'QT4Agg': cfm.window.showMaximized() elif callable(getattr(cfm, "full_screen_toggle", None)): if not getattr(cfm, "flag_is_max", None): cfm.full_screen_toggle() cfm.flag_is_max = True else: print('Couldn\'t maximize window, unknown pyplot backend {}'.format( backend))
def plot_LL_data(self, df): title = np.datetime_as_string(df.index.values[0], unit='D') + " to " title += np.datetime_as_string(df.index.values[-1], unit='D') file_name = os.path.join("plots", "LL_" + title.replace(" ", "_") + ".png") current_backend = plt.get_backend() plt.switch_backend("agg") plt.ioff() fig, axs = plt.subplots() sc = axs.scatter(df.PPFD.values, df.NEE.values, c=df.VPD.values, s=10) axs.set_title(title) axs.set_xlabel("PPFD (umol/m^2/s)") axs.set_ylabel("NEE (umol/m^2/s)") clb = plt.colorbar(sc) clb.ax.set_title("VPD") fig.savefig(file_name, format="png") plt.close(fig) plt.switch_backend(current_backend) plt.ion() return
def main(args=None): print('backend is', plt.get_backend()) # Set figure size (width and height in inches) plt.rcParams['figure.figsize'] = [21., 10.] queue_for = 8 queue_for = int(sys.argv[1]) print('queue for {} seconds'.format(queue_for)) rclpy.init() node = PlotFilterNode(queue_for) try: rclpy.spin(node) except KeyboardInterrupt: node.get_logger().info('ctrl-C detected, shutting down') finally: node.destroy_node() rclpy.shutdown()
def plot_mask_statistics(maskable, masked_table, output=None, ignore_zero=True): # get statistics stats = maskable.mask_statistics(masked_table) # calculate total if isinstance(masked_table, t.Group): total = 0 for table in masked_table: total += table._original_len() else: total = masked_table._original_len() labels = ['total', 'unmasked'] values = [total, stats['unmasked']] for key, value in sorted(stats.items()): if not key == 'unmasked': if not ignore_zero or value > 0: labels.append(key) values.append(value) if output is not None: old_backend = plt.get_backend() plt.switch_backend('pdf') plt.ioff() barplot = sns.barplot(x=np.array(labels), y=np.array(values), palette="muted") sns.despine() if output is not None: barplot.figure.savefig(output) plt.close(barplot.figure) plt.ion() plt.switch_backend(old_backend) else: plt.show()
def show_bug(): import tables as pytb print("pytable version: ", pytb.__version__) print( "import pytables before matplotlib and numpy causes `Segmentation fault` or `libpng error: IDAT: bad parameters to zlib`" ) import numpy as np print("numpy version: ", np.__version__) import matplotlib.pyplot as plt import matplotlib as mpl print("mpl version: ", mpl.__version__, "plt with backend: ", plt.get_backend()) print("read file") h5 = pytb.open_file("test_data.h5") h5data = h5.root.h5data nt, nj, ni = h5data.shape _data = h5.root.h5data[0] _data2 = h5.root.h5data[1] print(_data.shape) print("try to create fig") fig, ax = plt.subplots() arrowstride = 1 arrowwidth = 0.001 XI, YI = np.meshgrid(np.linspace(0, ni - 1, ni), np.linspace(0, nj - 1, nj)) qv = ax.quiver( XI[::arrowstride, ::arrowstride], YI[::arrowstride, ::arrowstride], _data[::arrowstride, ::arrowstride], _data2[::arrowstride, ::arrowstride], units='width', angles='xy', scale_units='xy', scale=2, headlength=4, width=arrowwidth, headwidth=3, headaxislength=3, color='C8', ) print("figure creation is OK")
def test_other_signal_before_sigint(target, kwargs): backend = plt.get_backend() proc = WaitForStringPopen([ sys.executable, "-c", inspect.getsource(_test_other_signal_before_sigint_impl) + "\n_test_other_signal_before_sigint_impl(" f"{backend!r}, {target!r}, {kwargs!r})" ]) try: proc.wait_for('DRAW') os.kill(proc.pid, signal.SIGUSR1) proc.wait_for('SIGUSR1') os.kill(proc.pid, signal.SIGINT) stdout, _ = proc.communicate(timeout=_test_timeout) except: proc.kill() stdout, _ = proc.communicate() raise print(stdout) assert 'SUCCESS' in stdout plt.figure()
def main(): print('backend is', plt.get_backend()) num_messages = 100 if len(sys.argv) > 1: num_messages = int(sys.argv[1]) print('num messages =', num_messages) # Set figure size (inches) plt.rcParams['figure.figsize'] = [24., 12.] rclpy.init() node = PlotControlNode(num_messages) try: rclpy.spin(node) except KeyboardInterrupt: node.get_logger().info('ctrl-C detected, shutting down') finally: node.destroy_node() rclpy.shutdown()
def matplotlib_config(): """Configure matplotlib for viz tests.""" import matplotlib # "force" should not really be necessary but should not hurt kwargs = dict() with warnings.catch_warnings(record=True): # ignore warning warnings.filterwarnings('ignore') matplotlib.use('agg', force=True, **kwargs) # don't pop up windows import matplotlib.pyplot as plt assert plt.get_backend() == 'agg' # overwrite some params that can horribly slow down tests that # users might have changed locally (but should not otherwise affect # functionality) plt.ioff() plt.rcParams['figure.dpi'] = 100 try: from traits.etsconfig.api import ETSConfig except Exception: pass else: ETSConfig.toolkit = 'qt4'
def setup_matplotlib_backend(): """Helper to support multiple workflows: 1) nominal -- running locally w/ jupyter notebook 2) unit tests (no ipython, backend is template) 3) binder -- does have notebook backend 4) colab -- does NOT have notebook backend Puts the matplotlib backend into notebook mode, if possible, otherwise falls back to inline mode. Returns True iff the final backend is interactive. """ ipython = get_ipython() if ipython is not None: try: ipython.run_line_magic("matplotlib", "notebook") except KeyError: ipython.run_line_magic("matplotlib", "inline") # import needs to happen after the backend is set. import matplotlib.pyplot as plt from matplotlib.rcsetup import interactive_bk return plt.get_backend() in interactive_bk
def main(output): print('Simulation of the lorenz equation.') lorenz = generate_lorenz() print(lorenz.shape) # plt.plot(lorenz[:, 0]) # plt.show() os.environ["DISPLAY"] = ":1" import matplotlib.pyplot as plt if output is None: import matplotlib matplotlib.use('Qt5Agg') print('Backend', plt.get_backend()) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot(lorenz[:, 0], lorenz[:, 1], lorenz[:, 2]) if output is None: plt.show() else: plt.savefig(output)
def __init__(self, output_dir, result_dir, log_queue, obs_config, files, config_file=CONFIG_FILE, obs_name=''): """ :param str output_dir: Output directory for data products :param str result_dir: central directory to copy output PDF to :param Queue log_queue: Queue to use for logging :param dict obs_config: Observations settings :param list files: HDF5 files to visualize :param str config_file: Path to config file :param str obs_name: Observation name to use in log messages """ module_name = type(self).__module__.split('.')[-1] self.output_dir = output_dir self.result_dir = result_dir self.logger = get_queue_logger(module_name, log_queue) self.obs_config = obs_config self.files = np.array(files) self.obs_name = obs_name # load config self.config_file = config_file self.config = self._load_config() self.logger.info(f"{self.obs_name}Starting visualization") # switch the plot backend to pdf old_backend = plt.get_backend() plt.switch_backend('PDF') try: self._visualize() except Exception as e: self.logger.error( f"{self.obs_name}Visualization failed: {type(e)}: {e}") # put back the old backend plt.switch_backend(old_backend)
def __init__(self, output_dir, result_dir, logger, obs_config, files, config_file=CONFIG_FILE): """ :param str output_dir: Output directory for data products :param str result_dir: central directory to copy output PDF to :param Logger logger: Processor logger object :param dict obs_config: Observations settings :param list files: HDF5 files to visualize :param str config_file: Path to config file """ self.output_dir = output_dir self.result_dir = result_dir self.logger = logger self.obs_config = obs_config self.files = np.array(files) # load config self.config_file = config_file self.config = self._load_config() self.logger.info( f"Starting visualization of task ID " f"{self.obs_config['parset']['task.taskID']}: {self.obs_config['datetimesource']}" ) # switch the plot backend to pdf old_backend = plt.get_backend() plt.switch_backend('PDF') try: self._visualize() except Exception as e: self.logger.error( f"Visualization of task ID {self.obs_config['parset']['task.taskID']} failed: " f"{type(e)}: {e}") # put back the old backend plt.switch_backend(old_backend)
def move_window(x=0, y=0): """Move PyPlot window to the specified location if possible. :type x: int :type y: int """ try: manager = plt.get_current_fig_manager() if manager is not None: # each backend has to be treated individually backend = plt.get_backend() window = manager.window if backend == 'Qt4Agg': window.move(x, y) elif backend == 'TkAgg': window.wm_geometry('+{0}+{1}'.format(x, y)) elif backend in ['GTKAgg', 'GTK3Agg', 'GTK', 'GTKCairo', 'GTK3Cairo']: window.set_position((x, y)) elif backend in ['WXAgg', 'WX']: window.SetPosition((x, y)) except: pass
def plot_training(self, history): acc = history.history['acc'] val_acc = history.history['val_acc'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(len(acc)) print("run to here") print(plt.get_backend()) plt.plot(epochs, acc, 'r.', label='train_acc') plt.plot(epochs, val_acc, 'b', label='val_acc') plt.title("Training and validation accuracy") plt.legend(loc=0, ncol=2) plt.savefig('./data/accuracy.png') plt.figure() plt.plot(epochs, loss, 'r.', label='train_loss') plt.plot(epochs, val_loss, 'b-', label='val_loss') plt.title("Training and validation loss") plt.legend(loc=0, ncol=2) plt.savefig('./data/loss.png') plt.show()