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()
def drawGraph(self, sqlX, sqlY, sparqlX, sparqlY, kind='bar', figsize=(12, 6)): fig, axes = plt.pyplot.subplots(1, 2, figsize=figsize) self.sqlResult[sqlY].value_counts().plot(ax=axes[0], kind=kind) self.sparqlResult[sparqlY].value_counts().plot(ax=axes[1], kind=kind) axes[0].set_title("SQL Result Graph: " + sqlY) axes[1].set_title("SPARQL Result Graph: " + sparqlY) flush_figures()
def draw_plot_with_oryginal(image, oryg_image, name="image.png"): fig, (ax, ax2) = plt.subplots(ncols=2, nrows=1, figsize=(10, 10)) plt.tight_layout() ax.imshow(image, cmap=plt.cm.gray) ax2.imshow(oryg_image, cmap=plt.cm.gray) plt.show() #plt.savefig(name) flush_figures()
def flush_all(): """Flushes stdout/stderr/matplotlib.""" sys.stdout.flush() sys.stderr.flush() # pylint: disable=g-import-not-at-top try: from ipykernel.pylab.backend_inline import flush_figures except ImportError: # older ipython from IPython.kernel.zmq.pylab.backend_inline import flush_figures flush_figures()
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()
def colorThreshold(image, t): processed = (image > t) * 1 flush_figures() return processed
def thresh(t, photo): warnings.simplefilter("ignore") binary = (photo > t) * 255 binary = np.uint8(binary) flush_figures() return binary
def drawPlot(image, oryg_image, name="image.png"): fig, (ax, ax2) = plt.subplots(ncols=2, nrows=1, figsize=(10, 10)) ax.imshow(image, cmap=plt.cm.gray) ax2.imshow(oryg_image, cmap=plt.cm.gray) plt.savefig(name) flush_figures()
def upd(patient_id, n_slice, nods): img = batch.get(patient_id, 'images')[n_slice] mask = batch.get(patient_id, 'masks')[n_slice] nonlocal predict if predict is None: rows, cols = 1, len(select) pred = np.zeros_like(img) fig, axes = plt.subplots(rows, cols, squeeze=False, figsize=(size1, size2)) else: rows = np.ceil(len(select) / 3).astype(np.int) cols = 3 if rows > 1 else len(select) fig, axes = plt.subplots(rows, cols, squeeze=False, figsize=(size1, size2)) # where fun begins :D if 4 or 5 or 6 in select: pred = predict[indices.index(patient_id), n_slice, ...] if grid: inv_spacing = 1 / batch.get(patient_id, 'spacing').reshape(-1)[1:] step_mult = 10 xticks = np.arange(0, img.shape[0], step_mult * inv_spacing[0]) yticks = np.arange(0, img.shape[1], step_mult * inv_spacing[1]) all_plots = { 1: { 'args': (img, plt.cm.bone) }, 2: { 'args': (mask, plt.cm.bone) }, 3: { 'args': (mask * img, plt.cm.bone) }, 4: { 'args': (img + mask * 300, plt.cm.seismic) }, 5: { 'args': (pred, plt.cm.bone) }, 6: { 'args': (pred * img, plt.cm.bone) }, 7: { 'args': (img + pred * 300, plt.cm.seismic) } } i = 0 for r in range(rows): for c in range(cols): axes[r][c].imshow(*all_plots[select[i]]['args']) axes[r][c].set_xticks(xticks, minor=True) axes[r][c].set_yticks(yticks, minor=True) axes[r][c].grid(color='r', linewidth=1.5, alpha=0.15, which='minor') i += 1 if i == len(select): break fig.subplots_adjust(left=None, bottom=0.1, right=None, top=0.4, wspace=0.1, hspace=0.1) flush_figures()