def __init__(self, vars, title=None, limits={}, cmap=None, colorbar='vertical', axes=None, figaspect='auto', **kwlimits): """ Creates a `Matplotlib2DGridViewer`. :Parameters: vars A `CellVariable` object. title displayed at the top of the `Viewer` window limits : dict a (deprecated) alternative to limit keyword arguments cmap The colormap. Defaults to `matplotlib.cm.jet` xmin, xmax, ymin, ymax, datamin, datamax displayed range of data. Any limit set to a (default) value of `None` will autoscale. colorbar plot a colorbar in specified orientation if not `None` axes if not `None`, `vars` will be plotted into this Matplotlib `Axes` object figaspect desired aspect ratio of figure. If arg is a number, use that aspect ratio. If arg is 'auto', the aspect ratio will be determined from the Variable's mesh. """ kwlimits.update(limits) AbstractMatplotlib2DViewer.__init__(self, vars=vars, title=title, cmap=cmap, colorbar=colorbar, axes=axes, figaspect=figaspect, **kwlimits) xmin, ymin = self.vars[0].mesh.extents['min'] xmax, ymax = self.vars[0].mesh.extents['max'] self.image = self.axes.imshow(self._data, extent=(xmin, xmax, ymin, ymax), vmin=0, vmax=1, cmap=self.cmap) self.axes.set_xlim(xmin=self._getLimit('xmin'), xmax=self._getLimit('xmax')) self.axes.set_ylim(ymin=self._getLimit('ymin'), ymax=self._getLimit('ymax')) if title is None: self.axes.set_title(self.vars[0].name)
def __init__(self, vars, title=None, scale=None, sparsity=None, log=False, limits={}, axes=None, figaspect='auto', **kwlimits): """Creates a `Matplotlib2DViewer`. :Parameters: vars a rank-1 `CellVariable` or `FaceVariable` object. title displayed at the top of the `Viewer` window scale if not `None`, scale all arrow lengths by this value sparsity if not `None`, then this number of arrows will be randomly chosen (weighted by the cell volume or face area) log if `True`, arrow length goes at the base-10 logarithm of the magnitude limits : dict a (deprecated) alternative to limit keyword arguments xmin, xmax, ymin, ymax, datamin, datamax displayed range of data. Any limit set to a (default) value of `None` will autoscale. axes if not `None`, `vars` will be plotted into this Matplotlib `Axes` object figaspect desired aspect ratio of figure. If arg is a number, use that aspect ratio. If arg is 'auto', the aspect ratio will be determined from the Variable's mesh. """ kwlimits.update(limits) AbstractMatplotlib2DViewer.__init__(self, vars=vars, title=title, axes=axes, figaspect=figaspect, **kwlimits) self.quiver(sparsity=sparsity, scale=scale) self.log = log self._plot()
def __init__(self, vars, title=None, limits={}, cmap=None, colorbar='vertical', axes=None, figaspect='auto', **kwlimits): """Creates a `Matplotlib2DViewer`. :Parameters: vars a `CellVariable` object. title displayed at the top of the `Viewer` window limits : dict a (deprecated) alternative to limit keyword arguments xmin, xmax, ymin, ymax, datamin, datamax displayed range of data. Any limit set to a (default) value of `None` will autoscale. cmap the colormap. Defaults to `matplotlib.cm.jet` colorbar plot a colorbar in specified orientation if not `None` axes if not `None`, `vars` will be plotted into this Matplotlib `Axes` object figaspect desired aspect ratio of figure. If arg is a number, use that aspect ratio. If arg is 'auto', the aspect ratio will be determined from the Variable's mesh. """ kwlimits.update(limits) AbstractMatplotlib2DViewer.__init__(self, vars=vars, title=title, cmap=cmap, colorbar=colorbar, axes=axes, figaspect=figaspect, **kwlimits) self._plot()
def __init__(self, vars, title=None, limits={}, cmap=None, colorbar='vertical', axes=None, number=10, levels=None, figaspect='auto', **kwlimits): """Creates a `Matplotlib2DContourViewer`. :Parameters: vars a `CellVariable` object. title displayed at the top of the `Viewer` window limits : dict a (deprecated) alternative to limit keyword arguments xmin, xmax, ymin, ymax, datamin, datamax displayed range of data. Any limit set to a (default) value of `None` will autoscale. cmap the colormap. Defaults to `matplotlib.cm.jet` colorbar plot a colorbar in specified orientation if not `None` axes if not `None`, `vars` will be plotted into this Matplotlib `Axes` object number contour `number` automatically-chosen levels *levels* [level0, level1, ..., leveln] A list of floating point numbers indicating the level curves to draw; eg to draw just the zero contour pass ``levels=[0]`` figaspect desired aspect ratio of figure. If arg is a number, use that aspect ratio. If arg is 'auto', the aspect ratio will be determined from the Variable's mesh. """ kwlimits.update(limits) AbstractMatplotlib2DViewer.__init__(self, vars=vars, title=title, cmap=cmap, colorbar=colorbar, axes=axes, figaspect=figaspect, **kwlimits) self.number = number self.levels = levels self._plot()
def __init__(self, vars, title=None, limits={}, cmap=None, colorbar='vertical', axes=None, figaspect='auto', **kwlimits): """Creates a `Matplotlib2DGridViewer`. Parameters ---------- vars : ~fipy.variables.cellVariable.CellVariable the `Variable` to display. title : str, optional displayed at the top of the `Viewer` window limits : dict, optional a (deprecated) alternative to limit keyword arguments cmap : ~matplotlib.colors.Colormap, optional the :class:`~matplotlib.colors.Colormap`. Defaults to `matplotlib.cm.jet` float xmin, xmax, ymin, ymax, datamin, datamax : float, optional displayed range of data. Any limit set to a (default) value of `None` will autoscale. colorbar : bool, optional plot a color bar in specified orientation if not `None` axes : ~matplotlib.axes.Axes, optional if not `None`, `vars` will be plotted into this Matplotlib `Axes` object figaspect : float, optional desired aspect ratio of figure. If arg is a number, use that aspect ratio. If arg is `auto`, the aspect ratio will be determined from the Variable's mesh. """ kwlimits.update(limits) AbstractMatplotlib2DViewer.__init__(self, vars=vars, title=title, cmap=cmap, colorbar=colorbar, axes=axes, figaspect=figaspect, **kwlimits) self.axes.set_xlim(xmin=self._getLimit('xmin'), xmax=self._getLimit('xmax')) self.axes.set_ylim(ymin=self._getLimit('ymin'), ymax=self._getLimit('ymax')) if title is None: self.axes.set_title(self.vars[0].name)
def __init__(self, vars, title=None, limits={}, cmap=None, colorbar='vertical', axes=None, number=10, levels=None, figaspect='auto', **kwlimits): """Creates a `Matplotlib2DContourViewer`. Parameters ---------- vars : ~fipy.variables.cellVariable.CellVariable `Variable` to display title : str, optional displayed at the top of the `Viewer` window limits : dict, optional a (deprecated) alternative to limit keyword arguments float xmin, xmax, ymin, ymax, datamin, datamax : float, optional displayed range of data. Any limit set to a (default) value of `None` will autoscale. cmap : ~matplotlib.colors.Colormap, optional the Colormap. Defaults to `matplotlib.cm.jet` colorbar : bool, optional plot a color bar in specified orientation if not `None` axes : ~matplotlib.axes.Axes, optional if not `None`, `vars` will be plotted into this Matplotlib `Axes` object number : int, optional contour `number` automatically-chosen levels levels : :obj:`list` of :obj:`float`, optional A list of numbers indicating the level curves to draw; e.g. to draw just the zero contour pass ``levels=[0]`` figaspect : float desired aspect ratio of figure. If arg is a number, use that aspect ratio. If arg is `auto`, the aspect ratio will be determined from the Variable's mesh. """ kwlimits.update(limits) AbstractMatplotlib2DViewer.__init__(self, vars=vars, title=title, cmap=cmap, colorbar=colorbar, axes=axes, figaspect=figaspect, **kwlimits) self.number = number self.levels = levels self._plot()
def _getSuitableVars(self, vars): from fipy.meshes.mesh2D import Mesh2D from fipy.variables.cellVariable import CellVariable vars = [var for var in AbstractMatplotlib2DViewer._getSuitableVars(self, vars) \ if ((isinstance(var.mesh, Mesh2D) and isinstance(var, CellVariable)) and var.rank == 0)] if len(vars) == 0: from fipy.viewers import MeshDimensionError raise MeshDimensionError("Matplotlib2DViewer can only display a rank-0, 2D CellVariable") # this viewer can only display one variable return [vars[0]]
def _getSuitableVars(self, vars): from fipy.meshes.mesh2D import Mesh2D from fipy.variables.cellVariable import CellVariable vars = [var for var in AbstractMatplotlib2DViewer._getSuitableVars(self, vars) \ if ((isinstance(var.mesh, Mesh2D) and isinstance(var, CellVariable)) and var.rank == 0)] if len(vars) == 0: from fipy.viewers import MeshDimensionError raise MeshDimensionError, "Matplotlib2DViewer can only display a rank-0, 2D CellVariable" # this viewer can only display one variable return [vars[0]]
def _getSuitableVars(self, vars): from fipy.meshes.uniformGrid2D import UniformGrid2D from fipy.variables.cellVariable import CellVariable vars = [var for var in AbstractMatplotlib2DViewer._getSuitableVars(self, vars) \ if (isinstance(var.mesh, UniformGrid2D) and isinstance(var, CellVariable) and var.rank == 0)] if len(vars) == 0: from fipy.viewers import MeshDimensionError raise MeshDimensionError, "Matplotlib2DGridViewer can only display a rank-0 CellVariable with a UniformGrid2D mesh" # this viewer can only display one variable return [vars[0]]
def _getSuitableVars(self, vars): from fipy.meshes.uniformGrid2D import UniformGrid2D from fipy.variables.cellVariable import CellVariable vars = [var for var in AbstractMatplotlib2DViewer._getSuitableVars(self, vars) \ if (isinstance(var.mesh, UniformGrid2D) and isinstance(var, CellVariable) and var.rank == 0)] if len(vars) == 0: from fipy.viewers import MeshDimensionError raise MeshDimensionError("Matplotlib2DGridViewer can only display a rank-0 CellVariable with a UniformGrid2D mesh") # this viewer can only display one variable return [vars[0]]
def __init__(self, vars, title=None, limits={}, cmap=None, colorbar='vertical', axes=None, number=10, levels=None, figaspect='auto', **kwlimits): """Creates a `Matplotlib2DContourViewer`. :Parameters: vars a `CellVariable` object. title displayed at the top of the `Viewer` window limits : dict a (deprecated) alternative to limit keyword arguments xmin, xmax, ymin, ymax, datamin, datamax displayed range of data. Any limit set to a (default) value of `None` will autoscale. cmap the colormap. Defaults to `matplotlib.cm.jet` colorbar plot a colorbar in specified orientation if not `None` axes if not `None`, `vars` will be plotted into this Matplotlib `Axes` object number contour `number` automatically-chosen levels *levels* [level0, level1, ..., leveln] A list of floating point numbers indicating the level curves to draw; e.g. to draw just the zero contour pass ``levels=[0]`` figaspect desired aspect ratio of figure. If arg is a number, use that aspect ratio. If arg is 'auto', the aspect ratio will be determined from the Variable's mesh. """ kwlimits.update(limits) AbstractMatplotlib2DViewer.__init__(self, vars=vars, title=title, cmap=cmap, colorbar=colorbar, axes=axes, figaspect=figaspect, **kwlimits) self.number = number self.levels = levels self._plot()
def _getLimit(self, key, default=None): limit = AbstractMatplotlib2DViewer._getLimit(self, key, default=default) if limit is None: X, Y = self.vars[0].mesh.faceCenters if 'xmin' in key: limit = float(min(X)) elif 'ymin' in key: limit = float(min(Y)) if 'xmax' in key: limit = float(max(X)) elif 'ymax' in key: limit = float(max(Y)) return limit
def _getSuitableVars(self, vars): from fipy.meshes.grid2D import Grid2D from fipy.meshes.uniformGrid2D import UniformGrid2D from fipy.variables.cellVariable import CellVariable vars = [var for var in AbstractMatplotlib2DViewer._getSuitableVars(self, vars) \ if ((isinstance(var.mesh, Grid2D) or isinstance(var.mesh, UniformGrid2D)) and isinstance(var, CellVariable))] if len(vars) == 0: from fipy.viewers import MeshDimensionError raise MeshDimensionError, "The mesh must be a Grid2D instance" # this viewer can only display one variable return [vars[0]]
def _getSuitableVars(self, vars): from fipy.meshes.mesh2D import Mesh2D from fipy.meshes.uniformGrid2D import UniformGrid2D vars = [var for var in AbstractMatplotlib2DViewer._getSuitableVars(self, vars) \ if ((isinstance(var.mesh, Mesh2D) or isinstance(var.mesh, UniformGrid2D))\ and (isinstance(var, FaceVariable) \ or isinstance(var, CellVariable)) and var.rank == 1)] if len(vars) == 0: from fipy.viewers import MeshDimensionError raise MeshDimensionError("The mesh must be a Mesh2D instance") # this viewer can only display one variable return [vars[0]]
def __init__(self, vars, title=None, log=False, limits={}, axes=None, figaspect='auto', density=1, linewidth=None, color=None, cmap=None, norm=None, arrowsize=1, arrowstyle='-|>', minlength=0.1, **kwlimits): """Creates a `MatplotlibStreamViewer`. :Parameters: vars a rank-1 `CellVariable` or `FaceVariable` object. title displayed at the top of the `Viewer` window log if `True`, arrow length goes at the base-10 logarithm of the magnitude limits : dict a (deprecated) alternative to limit keyword arguments xmin, xmax, ymin, ymax, datamin, datamax displayed range of data. Any limit set to a (default) value of `None` will autoscale. axes if not `None`, `vars` will be plotted into this Matplotlib `Axes` object figaspect desired aspect ratio of figure. If arg is a number, use that aspect ratio. If arg is 'auto', the aspect ratio will be determined from the Variable's mesh. *density* : float or 2-tuple Controls the closeness of streamlines. When `density = 1`, the domain is divided into a 25x25 grid---*density* linearly scales this grid. Each cell in the grid can have, at most, one traversing streamline. For different densities in each direction, use [density_x, density_y]. linewidth : Numeric or rank-0 `MeshVariable` vary linewidth when given a `CellVariable` or `FaceVariable` of same type as vars. *color* : matplotlib color code, or rank-0 `MeshVariable` Streamline color. When given an array with the type as vars, *color* values are converted to colors using *cmap*. *cmap* : :class:`~matplotlib.colors.Colormap` Colormap used to plot streamlines and arrows. Only necessary when using an `MeshVariable` input for *color*. *norm* : :class:`~matplotlib.colors.Normalize` Normalize object used to scale luminance data to 0, 1. If None, stretch (min, max) to (0, 1). Only necessary when *color* is an `MeshVariable`. *arrowsize* : float Factor scale arrow size. *arrowstyle* : str Arrow style specification. See :class:`~matplotlib.patches.FancyArrowPatch`. *minlength* : float Minimum length of streamline in axes coordinates. """ kwlimits.update(limits) AbstractMatplotlib2DViewer.__init__(self, vars=vars, title=title, axes=axes, figaspect=figaspect, **kwlimits) self.log = log self.kwargs = dict(density=density, cmap=cmap, norm=norm, arrowsize=arrowsize, arrowstyle=arrowstyle, minlength=minlength) self.linewidth = linewidth self.color = color self._stream = None self._plot()
class MatplotlibStreamViewer(AbstractMatplotlib2DViewer): """Displays a stream plot of a 2D rank-1 `CellVariable` or `FaceVariable` object using Matplotlib_ One issue is that this `Viewer` relies on `scipy.interpolate.griddata`, which interpolates on the convex hull of the data. The results is that streams are plotted across any concavities in the mesh. Another issue is that it does not seem possible to remove the streams without calling `cla()`, which means that different set of streams cannot be overlaid. .. _Matplotlib: http://matplotlib.sourceforge.net/ """ __doc__ += AbstractMatplotlib2DViewer._test2Dvector( viewer="MatplotlibStreamViewer") __doc__ += AbstractMatplotlib2DViewer._test2DvectorIrregular( viewer="MatplotlibStreamViewer") def __init__(self, vars, title=None, log=False, limits={}, axes=None, figaspect='auto', density=1, linewidth=None, color=None, cmap=None, norm=None, arrowsize=1, arrowstyle='-|>', minlength=0.1, **kwlimits): """Creates a `MatplotlibStreamViewer`. :Parameters: vars a rank-1 `CellVariable` or `FaceVariable` object. title displayed at the top of the `Viewer` window log if `True`, arrow length goes at the base-10 logarithm of the magnitude limits : dict a (deprecated) alternative to limit keyword arguments xmin, xmax, ymin, ymax, datamin, datamax displayed range of data. Any limit set to a (default) value of `None` will autoscale. axes if not `None`, `vars` will be plotted into this Matplotlib `Axes` object figaspect desired aspect ratio of figure. If arg is a number, use that aspect ratio. If arg is 'auto', the aspect ratio will be determined from the Variable's mesh. *density* : float or 2-tuple Controls the closeness of streamlines. When `density = 1`, the domain is divided into a 25x25 grid---*density* linearly scales this grid. Each cell in the grid can have, at most, one traversing streamline. For different densities in each direction, use [density_x, density_y]. linewidth : Numeric or rank-0 `MeshVariable` vary linewidth when given a `CellVariable` or `FaceVariable` of same type as vars. *color* : matplotlib color code, or rank-0 `MeshVariable` Streamline color. When given an array with the type as vars, *color* values are converted to colors using *cmap*. *cmap* : :class:`~matplotlib.colors.Colormap` Colormap used to plot streamlines and arrows. Only necessary when using an `MeshVariable` input for *color*. *norm* : :class:`~matplotlib.colors.Normalize` Normalize object used to scale luminance data to 0, 1. If None, stretch (min, max) to (0, 1). Only necessary when *color* is an `MeshVariable`. *arrowsize* : float Factor scale arrow size. *arrowstyle* : str Arrow style specification. See :class:`~matplotlib.patches.FancyArrowPatch`. *minlength* : float Minimum length of streamline in axes coordinates. """ kwlimits.update(limits) AbstractMatplotlib2DViewer.__init__(self, vars=vars, title=title, axes=axes, figaspect=figaspect, **kwlimits) self.log = log self.kwargs = dict(density=density, cmap=cmap, norm=norm, arrowsize=arrowsize, arrowstyle=arrowstyle, minlength=minlength) self.linewidth = linewidth self.color = color self._stream = None self._plot() def _getSuitableVars(self, vars): from fipy.meshes.mesh2D import Mesh2D from fipy.meshes.uniformGrid2D import UniformGrid2D vars = [var for var in AbstractMatplotlib2DViewer._getSuitableVars(self, vars) \ if ((isinstance(var.mesh, Mesh2D) or isinstance(var.mesh, UniformGrid2D))\ and (isinstance(var, FaceVariable) \ or isinstance(var, CellVariable)) and var.rank == 1)] if len(vars) == 0: from fipy.viewers import MeshDimensionError raise MeshDimensionError("The mesh must be a Mesh2D instance") # this viewer can only display one variable return [vars[0]] def _plot(self): from scipy.interpolate import griddata var = self.vars[0] mesh = var.mesh xmin, ymin = mesh.extents['min'] xmax, ymax = mesh.extents['max'] N = 100 X = numerix.linspace(xmin, xmax, N) Y = numerix.linspace(ymin, ymax, N) grid_x, grid_y = numerix.mgrid[xmin:xmax:N * 1j, ymin:ymax:N * 1j] if isinstance(var, FaceVariable): C = mesh.faceCenters elif isinstance(var, CellVariable): C = mesh.cellCenters U = griddata(C.value.T, var.value[0], (grid_x, grid_y), method='cubic') V = griddata(C.value.T, var.value[1], (grid_x, grid_y), method='cubic') lw = self.linewidth if isinstance(lw, (FaceVariable, CellVariable)): lw = griddata(C.value.T, lw.value, (grid_x, grid_y), method='cubic') color = self.color if isinstance(color, (FaceVariable, CellVariable)): color = griddata(C.value.T, color.value, (grid_x, grid_y), method='cubic', fill_value=color.min()) U = U.T V = V.T ang = numerix.arctan2(V, U) mag = numerix.sqrt(U**2 + V**2) datamin, datamax = self._autoscale(vars=(mag, ), datamin=self._getLimit('datamin'), datamax=self._getLimit('datamax')) mag = numerix.where(mag > datamax, numerix.nan, mag) mag = numerix.where(mag < datamin, numerix.nan, mag) if self.log: mag = numerix.log10(mag) U = mag * numerix.cos(ang) V = mag * numerix.sin(ang) # if self._stream is not None: # # the following doesn't work, nor does it help to `add_collection` first # # self._stream.arrows.remove() # self._stream.lines.remove() self.axes.cla() self._stream = self.axes.streamplot(X, Y, U, V, linewidth=lw, color=color, **self.kwargs) self.axes.set_xlim(xmin=self._getLimit('xmin'), xmax=self._getLimit('xmax')) self.axes.set_ylim(ymin=self._getLimit('ymin'), ymax=self._getLimit('ymax'))
class Matplotlib2DGridViewer(AbstractMatplotlib2DViewer): """ Displays an image plot of a 2D `CellVariable` object using Matplotlib_. .. _Matplotlib: http://matplotlib.sourceforge.net/ """ __doc__ += AbstractMatplotlib2DViewer._test2D( viewer="Matplotlib2DGridViewer") def __init__(self, vars, title=None, limits={}, cmap=None, colorbar='vertical', axes=None, figaspect='auto', **kwlimits): """ Creates a `Matplotlib2DGridViewer`. :Parameters: vars A `CellVariable` object. title displayed at the top of the `Viewer` window limits : dict a (deprecated) alternative to limit keyword arguments cmap The colormap. Defaults to `matplotlib.cm.jet` xmin, xmax, ymin, ymax, datamin, datamax displayed range of data. Any limit set to a (default) value of `None` will autoscale. colorbar plot a colorbar in specified orientation if not `None` axes if not `None`, `vars` will be plotted into this Matplotlib `Axes` object figaspect desired aspect ratio of figure. If arg is a number, use that aspect ratio. If arg is 'auto', the aspect ratio will be determined from the Variable's mesh. """ kwlimits.update(limits) AbstractMatplotlib2DViewer.__init__(self, vars=vars, title=title, cmap=cmap, colorbar=colorbar, axes=axes, figaspect=figaspect, **kwlimits) self.image = self.axes.imshow(self._data, extent=(self._getLimit('xmin'), self._getLimit('xmax'), self._getLimit('ymin'), self._getLimit('ymax')), vmin=0, vmax=1, cmap=self.cmap) if title is None: self.axes.set_title(self.vars[0].name) def _getLimit(self, key, default=None): limit = AbstractMatplotlib2DViewer._getLimit(self, key, default=default) if limit is None: X, Y = self.vars[0].mesh.faceCenters if 'xmin' in key: limit = float(min(X)) elif 'ymin' in key: limit = float(min(Y)) if 'xmax' in key: limit = float(max(X)) elif 'ymax' in key: limit = float(max(Y)) return limit def _getSuitableVars(self, vars): from fipy.meshes.uniformGrid2D import UniformGrid2D from fipy.variables.cellVariable import CellVariable vars = [var for var in AbstractMatplotlib2DViewer._getSuitableVars(self, vars) \ if (isinstance(var.mesh, UniformGrid2D) and isinstance(var, CellVariable) and var.rank == 0)] if len(vars) == 0: from fipy.viewers import MeshDimensionError raise MeshDimensionError, "Matplotlib2DGridViewer can only display a rank-0 CellVariable with a UniformGrid2D mesh" # this viewer can only display one variable return [vars[0]] @getsetDeprecated def _getData(self): return self._data @property def _data(self): from fipy.tools.numerix import array, reshape return reshape(array(self.vars[0]), self.vars[0].mesh.shape[::-1])[::-1] def _plot(self): self.norm.vmin = self._getLimit(('datamin', 'zmin')) self.norm.vmax = self._getLimit(('datamax', 'zmax')) self.image.set_data(self.norm(self._data)) if self.colorbar is not None: self.colorbar.plot()
class Matplotlib2DContourViewer(AbstractMatplotlib2DViewer): """Displays a contour plot of a 2D `CellVariable` object. The `Matplotlib2DContourViewer` plots a 2D `CellVariable` using Matplotlib_. .. _Matplotlib: http://matplotlib.sourceforge.net/ """ __doc__ += AbstractMatplotlib2DViewer._test2D(viewer="Matplotlib2DContourViewer") def __init__(self, vars, title=None, limits={}, cmap=None, colorbar='vertical', axes=None, number=10, levels=None, figaspect='auto', **kwlimits): """Creates a `Matplotlib2DContourViewer`. Parameters ---------- vars : ~fipy.variables.cellVariable.CellVariable `Variable` to display title : str, optional displayed at the top of the `Viewer` window limits : dict, optional a (deprecated) alternative to limit keyword arguments float xmin, xmax, ymin, ymax, datamin, datamax : float, optional displayed range of data. Any limit set to a (default) value of `None` will autoscale. cmap : ~matplotlib.colors.Colormap, optional the Colormap. Defaults to `matplotlib.cm.jet` colorbar : bool, optional plot a color bar in specified orientation if not `None` axes : ~matplotlib.axes.Axes, optional if not `None`, `vars` will be plotted into this Matplotlib `Axes` object number : int, optional contour `number` automatically-chosen levels levels : :obj:`list` of :obj:`float`, optional A list of numbers indicating the level curves to draw; e.g. to draw just the zero contour pass ``levels=[0]`` figaspect : float desired aspect ratio of figure. If arg is a number, use that aspect ratio. If arg is `auto`, the aspect ratio will be determined from the Variable's mesh. """ kwlimits.update(limits) AbstractMatplotlib2DViewer.__init__(self, vars=vars, title=title, cmap=cmap, colorbar=colorbar, axes=axes, figaspect=figaspect, **kwlimits) self.number = number self.levels = levels self._plot() def _getSuitableVars(self, vars): from fipy.meshes.mesh2D import Mesh2D from fipy.variables.cellVariable import CellVariable vars = [var for var in AbstractMatplotlib2DViewer._getSuitableVars(self, vars) \ if ((isinstance(var.mesh, Mesh2D) and isinstance(var, CellVariable)) and var.rank == 0)] if len(vars) == 0: from fipy.viewers import MeshDimensionError raise MeshDimensionError("Matplotlib2DViewer can only display a rank-0, 2D CellVariable") # this viewer can only display one variable return [vars[0]] def _plot(self): ## plt.clf() ## ## Added garbage collection since matplotlib objects seem to hang ## ## around and accumulate. ## import gc ## gc.collect() mesh = self.vars[0].mesh x, y = mesh.cellCenters z = self.vars[0].value xmin, ymin = mesh.extents['min'] xmax, ymax = mesh.extents['max'] from matplotlib.mlab import griddata xi = numerix.linspace(xmin, xmax, 1000) yi = numerix.linspace(ymin, ymax, 1000) # grid the data. zi = griddata(x, y, z, xi, yi, interp='linear') if hasattr(self, "_contourSet"): for collection in self._contourSet.collections: try: ix = self.axes.collections.index(collection) except ValueError as e: ix = None if ix is not None: del self.axes.collections[ix] zmin, zmax = self._autoscale(vars=self.vars, datamin=self._getLimit(('datamin', 'zmin')), datamax=self._getLimit(('datamax', 'zmax'))) self.norm.vmin = zmin self.norm.vmax = zmax if self.levels is not None: levels = self.levels else: levels = numerix.arange(self.number + 1) * (zmax - zmin) / self.number + zmin self._contourSet = self.axes.contour(xi, yi, zi, levels=levels, cmap=self.cmap) self.axes.set_xlim(xmin=self._getLimit('xmin'), xmax=self._getLimit('xmax')) self.axes.set_ylim(ymin=self._getLimit('ymin'), ymax=self._getLimit('ymax')) if self.colorbar is not None: self.colorbar.plot()
def __init__(self, vars, title=None, log=False, limits={}, axes=None, figaspect='auto', density=1, linewidth=None, color=None, cmap=None, norm=None, arrowsize=1, arrowstyle='-|>', minlength=0.1, **kwlimits): """Creates a `MatplotlibStreamViewer`. Parameters ---------- vars : ~fipy.variables.cellVariable.CellVariable or ~fipy.variables.faceVariable.FaceVariable rank-1 `Variable` to display title : str, optional displayed at the top of the `Viewer` window log : bool, optional if `True`, arrow length goes at the base-10 logarithm of the magnitude limits : dict, optional a (deprecated) alternative to limit keyword arguments float xmin, xmax, ymin, ymax, datamin, datamax : float displayed range of data. Any limit set to a (default) value of `None` will autoscale. axes : ~matplotlib.axes.Axes, optional if not `None`, `vars` will be plotted into this Matplotlib `Axes` object figaspect : float, optional desired aspect ratio of figure. If arg is a number, use that aspect ratio. If arg is `auto`, the aspect ratio will be determined from the Variable's mesh. density : float or (float, float), optional Controls the closeness of streamlines. When ``density = 1``, the domain is divided into a 30x30 grid. *density* linearly scales this grid. Each cell in the grid can have, at most, one traversing streamline. For different densities in each direction, use a tuple (density_x, density_y). linewidth : array_like or ~fipy.variables.cellVariable.CellVariable or ~fipy.variables.faceVariable.FaceVariable, optional The width of the stream lines. With a rank-0 `CellVariable` or `FaceVariable` the line width can be varied across the grid. The MeshVariable must have the same type and be defined on the same `Mesh` as *vars*. color : str or ~fipy.variables.cellVariable.CellVariable or ~fipy.variables.faceVariable.FaceVariable, optional The streamline color as a matplotlib color code or a field of numbers. If given a rank-0 `CellVariable` or `FaceVariable`, its values are converted to colors using *cmap* and *norm*. The MeshVariable must have the same type and be defined on the same `Mesh` as *vars*. cmap : ~matplotlib.colors.Colormap, optional Colormap used to plot streamlines and arrows. This is only used if *color* is a MeshVariable. norm : ~matplotlib.colors.Normalize, optional Normalize object used to scale luminance data to 0, 1. If ``None``, stretch (min, max) to (0, 1). Only necessary when *color* is a MeshVariable. arrowsize : float, optional Scaling factor for the arrow size. arrowstyle : str, optional Arrow style specification. See `~matplotlib.patches.FancyArrowPatch`. minlength : float, optional Minimum length of streamline in axes coordinates. """ kwlimits.update(limits) AbstractMatplotlib2DViewer.__init__(self, vars=vars, title=title, axes=axes, figaspect=figaspect, **kwlimits) self.log = log self.kwargs = dict(density=density, cmap=cmap, norm=norm, arrowsize=arrowsize, arrowstyle=arrowstyle, minlength=minlength) self.linewidth = linewidth self.color = color self._stream = None self._plot()
class MatplotlibVectorViewer(AbstractMatplotlib2DViewer): """Displays a vector plot of a 2D rank-1 `CellVariable` or `FaceVariable` object using Matplotlib_ .. _Matplotlib: http://matplotlib.sourceforge.net/ """ __doc__ += AbstractMatplotlib2DViewer._test2Dvector( viewer="MatplotlibVectorViewer") __doc__ += """ >>> for sparsity in numerix.arange(5000, 0, -500): ... viewer.quiver(sparsity=sparsity) ... viewer.plot() >>> viewer._promptForOpinion() """ __doc__ += AbstractMatplotlib2DViewer._test2DvectorIrregular( viewer="MatplotlibVectorViewer") def __init__(self, vars, title=None, scale=None, sparsity=None, log=False, limits={}, axes=None, figaspect='auto', **kwlimits): """Creates a `Matplotlib2DViewer`. :Parameters: vars a rank-1 `CellVariable` or `FaceVariable` object. title displayed at the top of the `Viewer` window scale if not `None`, scale all arrow lengths by this value sparsity if not `None`, then this number of arrows will be randomly chosen (weighted by the cell volume or face area) log if `True`, arrow length goes at the base-10 logarithm of the magnitude limits : dict a (deprecated) alternative to limit keyword arguments xmin, xmax, ymin, ymax, datamin, datamax displayed range of data. Any limit set to a (default) value of `None` will autoscale. axes if not `None`, `vars` will be plotted into this Matplotlib `Axes` object figaspect desired aspect ratio of figure. If arg is a number, use that aspect ratio. If arg is 'auto', the aspect ratio will be determined from the Variable's mesh. """ kwlimits.update(limits) AbstractMatplotlib2DViewer.__init__(self, vars=vars, title=title, axes=axes, figaspect=figaspect, **kwlimits) self.quiver(sparsity=sparsity, scale=scale) self.log = log self._plot() def quiver(self, sparsity=None, scale=None): var = self.vars[0] mesh = var.mesh if isinstance(var, FaceVariable): N = mesh.numberOfFaces X, Y = mesh.faceCenters elif isinstance(var, CellVariable): N = mesh.numberOfCells X, Y = mesh.cellCenters if sparsity is not None and N > sparsity: XYrand = numerix.random.random((2, sparsity)) XYrand = numerix.array([ [min(X)], [min(Y)] ]) + XYrand * numerix.array([[max(X) - min(X)], [max(Y) - min(Y)]]) self.indices = numerix.nearest(numerix.array([X, Y]), XYrand) else: self.indices = numerix.arange(N) X = numerix.take(X, self.indices) Y = numerix.take(Y, self.indices) U = V = numerix.ones(X.shape, 'l') if hasattr(self, "_quiver"): self._quiver.remove() self._quiver = self.axes.quiver(X, Y, U, V, scale=scale, pivot='middle') def _getSuitableVars(self, vars): from fipy.meshes.mesh2D import Mesh2D from fipy.meshes.uniformGrid2D import UniformGrid2D vars = [var for var in AbstractMatplotlib2DViewer._getSuitableVars(self, vars) \ if ((isinstance(var.mesh, Mesh2D) or isinstance(var.mesh, UniformGrid2D))\ and (isinstance(var, FaceVariable) \ or isinstance(var, CellVariable)) and var.rank == 1)] if len(vars) == 0: from fipy.viewers import MeshDimensionError raise MeshDimensionError, "The mesh must be a Mesh2D instance" # this viewer can only display one variable return [vars[0]] def _plot(self): var = self.vars[0] mesh = var.mesh U, V = var.numericValue U = numerix.take(U, self.indices) V = numerix.take(V, self.indices) ang = numerix.arctan2(V, U) mag = numerix.sqrt(U**2 + V**2) datamin, datamax = self._autoscale(vars=(mag, ), datamin=self._getLimit('datamin'), datamax=self._getLimit('datamax')) mag = numerix.where(mag > datamax, datamax, mag) mag = numerix.ma.masked_array(mag, mag < datamin) if self.log: mag = numerix.log10(mag) mag = numerix.ma.masked_array(mag, numerix.isnan(mag)) U = mag * numerix.cos(ang) V = mag * numerix.sin(ang) self._quiver.set_UVC(U, V) self.axes.set_xlim(xmin=self._getLimit('xmin'), xmax=self._getLimit('xmax')) self.axes.set_ylim(ymin=self._getLimit('ymin'), ymax=self._getLimit('ymax'))
class Matplotlib2DGridContourViewer(AbstractMatplotlib2DViewer): """Displays a contour plot of a 2D `CellVariable` object. The `Matplotlib2DGridContourViewer` plots a 2D `CellVariable` using Matplotlib_. .. _Matplotlib: http://matplotlib.sourceforge.net/ """ __doc__ += AbstractMatplotlib2DViewer._test2D( viewer="Matplotlib2DGridContourViewer") def __init__(self, vars, title=None, limits={}, cmap=None, colorbar='vertical', axes=None, figaspect='auto', **kwlimits): """Creates a `Matplotlib2DViewer`. :Parameters: vars a `CellVariable` object. title displayed at the top of the `Viewer` window limits : dict a (deprecated) alternative to limit keyword arguments xmin, xmax, ymin, ymax, datamin, datamax displayed range of data. Any limit set to a (default) value of `None` will autoscale. cmap the colormap. Defaults to `matplotlib.cm.jet` colorbar plot a colorbar in specified orientation if not `None` axes if not `None`, `vars` will be plotted into this Matplotlib `Axes` object figaspect desired aspect ratio of figure. If arg is a number, use that aspect ratio. If arg is 'auto', the aspect ratio will be determined from the Variable's mesh. """ kwlimits.update(limits) AbstractMatplotlib2DViewer.__init__(self, vars=vars, title=title, cmap=cmap, colorbar=colorbar, axes=axes, figaspect=figaspect, **kwlimits) self._plot() def _getSuitableVars(self, vars): from fipy.meshes.grid2D import Grid2D from fipy.meshes.uniformGrid2D import UniformGrid2D from fipy.variables.cellVariable import CellVariable vars = [var for var in AbstractMatplotlib2DViewer._getSuitableVars(self, vars) \ if ((isinstance(var.mesh, Grid2D) or isinstance(var.mesh, UniformGrid2D)) and isinstance(var, CellVariable))] if len(vars) == 0: from fipy.viewers import MeshDimensionError raise MeshDimensionError, "The mesh must be a Grid2D instance" # this viewer can only display one variable return [vars[0]] def _plot(self): ## pylab.clf() ## ## Added garbage collection since matplotlib objects seem to hang ## ## around and accumulate. ## import gc ## gc.collect() mesh = self.vars[0].mesh shape = mesh.shape X, Y = mesh.cellCenters Z = self.vars[0].value X, Y, Z = [v.reshape(shape, order="FORTRAN") for v in (X, Y, Z)] zmin, zmax = self._autoscale(vars=self.vars, datamin=self._getLimit( ('datamin', 'zmin')), datamax=self._getLimit( ('datamax', 'zmax'))) self.norm.vmin = zmin self.norm.vmax = zmax numberOfContours = 10 smallNumber = 1e-7 diff = zmax - zmin if diff < smallNumber: V = numerix.arange(numberOfContours + 1) * smallNumber / numberOfContours + zmin else: V = numerix.arange(numberOfContours + 1) * diff / numberOfContours + zmin self.axes.contourf(X, Y, Z, V, cmap=self.cmap) self.axes.set_xlim(xmin=self._getLimit('xmin'), xmax=self._getLimit('xmax')) self.axes.set_ylim(ymin=self._getLimit('ymin'), ymax=self._getLimit('ymax')) if self.colorbar is not None: self.colorbar.plot()