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 get_matplotlib_backend( ) == 'module://ipykernel.pylab.backend_inline': # returns None when matplotlib is inline mode to prevent Jupyter # with matplotlib inlining enabled to draw the diagram twice. im = None else: # when matplotlib is not inline mode, # self.figure.savefig is called twice because... # ... this is needed to get the in-memory representation with tempfile.TemporaryDirectory() as tmpdir: tmpfile = os.path.join(tmpdir, 'circuit.png') self.figure.savefig(tmpfile, dpi=self._style.dpi, bbox_inches='tight') im = PIL.Image.open(tmpfile) _utils._trim(im) os.remove(tmpfile) # ... and this is needed to delegate in matplotlib the generation of # the proper format. if filename: self.figure.savefig(filename, dpi=self._style.dpi, bbox_inches='tight') return im
def show_stack(self, indices: Mapping[Indices, Union[int, slice]], color_map: str = 'gray', figure_size: Tuple[int, int] = (10, 10), rescale: bool = False, p_min: Optional[float] = None, p_max: Optional[float] = None, **kwargs): """Create an interactive visualization of an image stack Produces a slider that flips through the selected volume tile-by-tile. Supports manual adjustment of dynamic range. Parameters ---------- indices : Mapping[Indices, Union[int, slice]], Indices to select a volume to visualize. Passed to `Image.get_slice()`. See `Image.get_slice()` for examples. color_map : str (default = 'gray') string id of a matplotlib colormap figure_size : Tuple[int, int] (default = (10, 10)) size of the figure in inches rescale : bool (default = False) if True, rescale the data to exclude high and low-value outliers (see skimage.exposure.rescale_intensity). p_min: float clip values below this intensity percentile. If provided, overrides rescale, above. (default = None) p_max: float clip values above this intensity percentile. If provided, overrides rescale, above. (default = None) Raises ------ ValueError : User must select one of rescale or p_min/p_max to adjust the image dynamic range. If both are selected, a ValueError is raised. Notes ----- For this widget to function interactively in the notebook, after ipywidgets has been installed, the user must register the widget with jupyter by typing the following command into the terminal: jupyter nbextension enable --py widgetsnbextension """ # infer if %matplotlib inline or notebook mpl_is_notebook = 'nbAgg' in get_matplotlib_backend() if not indices: raise ValueError('indices may not be an empty dict or None') # get linearized scaled and clipped tiles, along with title names, for plotting linear_view, labels, n_tiles = self._get_scaled_clipped_linear_view( indices, rescale, p_min, p_max) if mpl_is_notebook: self._show_matplotlib_notebook(linear_view, labels, n_tiles, figure_size, color_map) else: return self._show_matplotlib_inline(linear_view, labels, n_tiles, figure_size, color_map)