我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用matplotlib.pyplot.draw_if_interactive()。
def activate_matplotlib(backend): """Activate the given backend and set interactive to True.""" import matplotlib matplotlib.interactive(True) # Matplotlib had a bug where even switch_backend could not force # the rcParam to update. This needs to be set *before* the module # magic of switch_backend(). matplotlib.rcParams['backend'] = backend import matplotlib.pyplot matplotlib.pyplot.switch_backend(backend) # This must be imported last in the matplotlib series, after # backend/interactivity choices have been made import matplotlib.pyplot as plt plt.show._needmain = False # We need to detect at runtime whether show() is called by the user. # For this, we wrap it into a decorator which adds a 'called' flag. plt.draw_if_interactive = flag_calls(plt.draw_if_interactive)
def hide_axis(where, ax=None): ax = ax or plt.gca() if type(where) == str: _w = [where] else: _w = where [sk.set_color('None') for k, sk in ax.spines.items() if k in _w ] if 'top' in _w and 'bottom' in _w: ax.xaxis.set_ticks_position('none') elif 'top' in _w: ax.xaxis.set_ticks_position('bottom') elif 'bottom' in _w: ax.xaxis.set_ticks_position('top') if 'left' in _w and 'right' in _w: ax.yaxis.set_ticks_position('none') elif 'left' in _w: ax.yaxis.set_ticks_position('right') elif 'right' in _w: ax.yaxis.set_ticks_position('left') plt.draw_if_interactive()
def shift_axis(which, delta, where='outward', ax=None): ax = ax or plt.gca() if type(which) == str: _w = [which] else: _w = which scales = (ax.xaxis.get_scale(), ax.yaxis.get_scale()) lbls = (ax.xaxis.get_label(), ax.yaxis.get_label()) for wk in _w: ax.spines[wk].set_position((where, delta)) ax.set_xscale(scales[0]) ax.set_yscale(scales[1]) ax.xaxis.set_label(lbls[0]) ax.yaxis.set_label(lbls[1]) plt.draw_if_interactive()
def setMargins(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None): """ Tune the subplot layout via the meanings (and suggested defaults) are:: left = 0.125 # the left side of the subplots of the figure right = 0.9 # the right side of the subplots of the figure bottom = 0.1 # the bottom of the subplots of the figure top = 0.9 # the top of the subplots of the figure wspace = 0.2 # the amount of width reserved for blank space between subplots hspace = 0.2 # the amount of height reserved for white space between subplots The actual defaults are controlled by the rc file """ plt.subplots_adjust(left, bottom, right, top, wspace, hspace) plt.draw_if_interactive()
def setNmajors(xval=None, yval=None, ax=None, mode='auto', **kwargs): """ setNmajors - set major tick number see figure.MaxNLocator for kwargs """ if ax is None: ax = plt.gca() if (mode == 'fixed'): if xval is not None: ax.xaxis.set_major_locator(MaxNLocator(xval, **kwargs)) if yval is not None: ax.yaxis.set_major_locator(MaxNLocator(yval, **kwargs)) elif (mode == 'auto'): if xval is not None: ax.xaxis.set_major_locator(AutoLocator(xval, **kwargs)) if yval is not None: ax.yaxis.set_major_locator(AutoLocator(yval, **kwargs)) plt.draw_if_interactive()
def boxplot(self, column=None, by=None, ax=None, fontsize=None, rot=0, grid=True, figsize=None, layout=None, return_type=None, **kwds): import pandas.tools.plotting as plots import matplotlib.pyplot as plt ax = plots.boxplot(self, column=column, by=by, ax=ax, fontsize=fontsize, grid=grid, rot=rot, figsize=figsize, layout=layout, return_type=return_type, **kwds) plt.draw_if_interactive() return ax
def hinton(W, bg='grey', facecolors=('w', 'k')): """Draw a hinton diagram of the matrix W on the current pylab axis Hinton diagrams are a way of visualizing numerical values in a matrix/vector, popular in the neural networks and machine learning literature. The area occupied by a square is proportional to a value's magnitude, and the colour indicates its sign (positive/negative). Example usage: R = np.random.normal(0, 1, (2,1000)) h, ex, ey = np.histogram2d(R[0], R[1], bins=15) hh = h - h.T hinton.hinton(hh) """ M, N = W.shape square_x = np.array([-.5, .5, .5, -.5]) square_y = np.array([-.5, -.5, .5, .5]) ioff = False if plt.isinteractive(): plt.ioff() ioff = True plt.fill([-.5, N - .5, N - .5, - .5], [-.5, -.5, M - .5, M - .5], bg) Wmax = np.abs(W).max() for m, Wrow in enumerate(W): for n, w in enumerate(Wrow): c = plt.signbit(w) and facecolors[1] or facecolors[0] plt.fill(square_x * w / Wmax + n, square_y * w / Wmax + m, c, edgecolor=c) plt.ylim(-0.5, M - 0.5) plt.xlim(-0.5, M - 0.5) if ioff is True: plt.ion() plt.draw_if_interactive()
def mpl_runner(safe_execfile): """Factory to return a matplotlib-enabled runner for %run. Parameters ---------- safe_execfile : function This must be a function with the same interface as the :meth:`safe_execfile` method of IPython. Returns ------- A function suitable for use as the ``runner`` argument of the %run magic function. """ def mpl_execfile(fname,*where,**kw): """matplotlib-aware wrapper around safe_execfile. Its interface is identical to that of the :func:`execfile` builtin. This is ultimately a call to execfile(), but wrapped in safeties to properly handle interactive rendering.""" import matplotlib import matplotlib.pyplot as plt #print '*** Matplotlib runner ***' # dbg # turn off rendering until end of script is_interactive = matplotlib.rcParams['interactive'] matplotlib.interactive(False) safe_execfile(fname,*where,**kw) matplotlib.interactive(is_interactive) # make rendering call now, if the user tried to do it if plt.draw_if_interactive.called: plt.draw() plt.draw_if_interactive.called = False # re-draw everything that is stale try: da = plt.draw_all except AttributeError: pass else: da() return mpl_execfile
def add_markers(self, ax=None, where=0.0, orientation='horizontal', jitter=0, **kwargs): if ax is None: ax = plt.gca() # draw the positions if 'marker' not in kwargs: if orientation == 'horizontal': kwargs['marker'] = '|' else: kwargs['marker'] = '_' if ('facecolor' not in kwargs.keys()) | ('fc' not in kwargs.keys()) | \ ('markerfacecolor' not in kwargs.keys()) | ('mfc' not in kwargs.keys()): kwargs['markerfacecolor'] = 'None' if ('edgecolor' not in kwargs.keys()) | ('ec' not in kwargs.keys()) | \ ('markeredgecolor' not in kwargs.keys()) | ('mec' not in kwargs.keys()): kwargs['markeredgecolor'] = 'k' if ('linestyle' not in kwargs.keys()) | ('ls' not in kwargs.keys()): kwargs['linestyle'] = 'None' if ('size' not in kwargs.keys()) | ('markersize' not in kwargs.keys()): kwargs['markersize'] = 3 if orientation == 'horizontal': # Draw the lines if jitter > 0: pos = np.random.uniform(low=float(where - jitter), high=float(where + jitter), size=len(self.x)) ax.plot(self.x, pos, **kwargs) else: ax.plot(self.x, float(where) * np.ones(len(self.x)), **kwargs) plt.draw_if_interactive() elif orientation == 'vertical': # Draw the lines if jitter > 0.: pos = np.random.uniform(low=float(where - jitter), high=float(where + jitter), size=len(self.x)) ax.plot(pos, self.x, **kwargs) else: ax.plot(float(where) * np.ones(len(self.x)), self.x, marker='_', **kwargs) plt.draw_if_interactive()