def __plot_variance(self): pointsMin = self.__calc_min() pointsMax = self.__calc_max() polys = [] variance = [] varMin = 1000 varMax = 0 lastX = None lastYMin = None lastYMax = None for x in pointsMin.iterkeys(): if lastX is None: lastX = x if lastYMin is None: lastYMin = pointsMin[x] if lastYMax is None: lastYMax = pointsMax[x] polys.append([[x, pointsMin[x]], [x, pointsMax[x]], [lastX, lastYMax], [lastX, lastYMin], [x, pointsMin[x]]]) lastX = x lastYMin = pointsMin[x] lastYMax = pointsMax[x] var = pointsMax[x] - pointsMin[x] variance.append(var) varMin = min(varMin, var) varMax = max(varMax, var) norm = Normalize(vmin=varMin, vmax=varMax) sm = ScalarMappable(norm, self.colourMap) colours = sm.to_rgba(variance) pc = PolyCollection(polys) pc.set_gid('plot') pc.set_norm(norm) pc.set_color(colours) self.axes.add_collection(pc) return None, None
def shapes(shapes, data=None, colorbar=False, colorbar_ticklabels=None, norm=None, with_labels=False, edgecolors=None, facecolors=None, fontsize=None, ax=None, **kwds): """ Plot `data` on the basis of a dictionary of shapes. `data` must be given as a pandas Series with the corresponding keys of shapes as index. Parameters ---------- shapes : dict | pd.Series Dictionary of shapes data : pd.Series Float valued data to be plotted. If data is omitted, np.arange(N) will be used. with_labels : bool Whether to plot the name of each shape at its centroid Returns ------- collection : PolyCollection """ if 'outline' in kwds: # deprecated if kwds.pop('outline'): facecolors = 'none' if 'colour' in kwds: # deprecated edgecolors = kwds.pop('colour') if ax is None: ax = plt.gca() if not isinstance(shapes, pd.Series): shapes = pd.Series(shapes) def flatten_multipolygons(shapes): flat_shapes = [] flat_index = [] for n, sh in shapes.iteritems(): if isinstance(sh, MultiPolygon): flat_shapes += list(sh) flat_index += [n] * len(sh) else: flat_shapes.append(sh) flat_index.append(n) return pd.Series(flat_shapes, index=flat_index) if isinstance(edgecolors, pd.Series): shapes = shapes.reindex(edgecolors.index) flat_shapes = flatten_multipolygons(shapes) edgecolors = edgecolors.reindex(flat_shapes.index) elif isinstance(facecolors, pd.Series): shapes = shapes.reindex(facecolors.index) flat_shapes = flatten_multipolygons(shapes) facecolors = facecolors.reindex(flat_shapes.index) elif isinstance(data, pd.Series): shapes = shapes.reindex(data.index) flat_shapes = flatten_multipolygons(shapes) data = data.reindex(flat_shapes.index) else: flat_shapes = flatten_multipolygons(shapes) if facecolors is None: data = pd.Series(np.arange(len(shapes)), index=shapes.index).reindex(flat_shapes.index) coll = PolyCollection((np.asarray(x.exterior) for x in flat_shapes), transOffset=ax.transData, facecolors=facecolors, edgecolors=edgecolors, **kwds) if data is not None: coll.set_array(data) if norm is not None: coll.set_norm(norm) ax.add_collection(coll, autolim=True) if colorbar: kwargs = dict() if isinstance(colorbar, dict): kwargs.update(colorbar) ## FIXME : sounds like a bug to me, but hey if norm is not None: norm.autoscale(data) cbar = plt.colorbar(mappable=coll, **kwargs) if colorbar_ticklabels is not None: cbar.ax.set_yticklabels(colorbar_ticklabels) if with_labels: for k, v in iteritems(shapes.reindex(data.index)): x, y = np.asarray(v.centroid) plt.text(x, y, k, fontsize=fontsize, horizontalalignment='center', verticalalignment='center') ax.autoscale_view() return coll
def tripcolor(ax, *args, **kwargs): """ Create a pseudocolor plot of an unstructured triangular grid to the :class:`~matplotlib.axes.Axes`. The triangulation can be specified in one of two ways; either:: tripcolor(triangulation, ...) where triangulation is a :class:`~matplotlib.tri.Triangulation` object, or :: tripcolor(x, y, ...) tripcolor(x, y, triangles, ...) tripcolor(x, y, triangles=triangles, ...) tripcolor(x, y, mask=mask, ...) tripcolor(x, y, triangles, mask=mask, ...) in which case a Triangulation object will be created. See :class:`~matplotlib.tri.Triangulation` for a explanation of these possibilities. The next argument must be *C*, the array of color values, one per point in the triangulation. The colors used for each triangle are from the mean C of the triangle's three points. The remaining kwargs are the same as for :meth:`~matplotlib.axes.Axes.pcolor`. **Example:** .. plot:: mpl_examples/pylab_examples/tripcolor_demo.py """ if not ax._hold: ax.cla() alpha = kwargs.pop('alpha', 1.0) norm = kwargs.pop('norm', None) cmap = kwargs.pop('cmap', None) vmin = kwargs.pop('vmin', None) vmax = kwargs.pop('vmax', None) shading = kwargs.pop('shading', 'flat') tri, args, kwargs = Triangulation.get_from_args_and_kwargs(*args, **kwargs) x = tri.x y = tri.y triangles = tri.get_masked_triangles() # Vertices of triangles. verts = np.concatenate((x[triangles][...,np.newaxis], y[triangles][...,np.newaxis]), axis=2) C = np.asarray(args[0]) if C.shape != x.shape: raise ValueError('C array must have same length as triangulation x and' ' y arrays') # Color values, one per triangle, mean of the 3 vertex color values. C = C[triangles].mean(axis=1) if shading == 'faceted': edgecolors = (0,0,0,1), linewidths = (0.25,) else: edgecolors = 'face' linewidths = (1.0,) kwargs.setdefault('edgecolors', edgecolors) kwargs.setdefault('antialiaseds', (0,)) kwargs.setdefault('linewidths', linewidths) collection = PolyCollection(verts, **kwargs) collection.set_alpha(alpha) collection.set_array(C) if norm is not None: assert(isinstance(norm, Normalize)) collection.set_cmap(cmap) collection.set_norm(norm) if vmin is not None or vmax is not None: collection.set_clim(vmin, vmax) else: collection.autoscale_None() ax.grid(False) minx = tri.x.min() maxx = tri.x.max() miny = tri.y.min() maxy = tri.y.max() corners = (minx, miny), (maxx, maxy) ax.update_datalim( corners) ax.autoscale_view() ax.add_collection(collection) return collection
def tripcolor(ax, *args, **kwargs): """ Create a pseudocolor plot of an unstructured triangular grid to the :class:`~matplotlib.axes.Axes`. The triangulation can be specified in one of two ways; either:: tripcolor(triangulation, ...) where triangulation is a :class:`~matplotlib.tri.Triangulation` object, or :: tripcolor(x, y, ...) tripcolor(x, y, triangles, ...) tripcolor(x, y, triangles=triangles, ...) tripcolor(x, y, mask, ...) tripcolor(x, y, mask=mask, ...) tripcolor(x, y, triangles, mask, ...) tripcolor(x, y, triangles, mask=mask, ...) in which case a Triangulation object will be created. See :class:`~matplotlib.tri.Triangulation` for a explanation of these possibilities. The next argument must be *C*, the array of color values, one per point in the triangulation. The colors used for each triangle are from the mean C of the triangle's three points. The remaining kwargs are the same as for :meth:`~matplotlib.axes.Axes.pcolor`. **Example:** .. plot:: mpl_examples/pylab_examples/tripcolor_demo.py """ if not ax._hold: ax.cla() alpha = kwargs.pop('alpha', 1.0) norm = kwargs.pop('norm', None) cmap = kwargs.pop('cmap', None) vmin = kwargs.pop('vmin', None) vmax = kwargs.pop('vmax', None) shading = kwargs.pop('shading', 'flat') tri, args, kwargs = Triangulation.get_from_args_and_kwargs(*args, **kwargs) x = tri.x y = tri.y triangles = tri.get_masked_triangles() # Vertices of triangles. verts = np.concatenate( (x[triangles][..., np.newaxis], y[triangles][..., np.newaxis]), axis=2) C = np.asarray(args[0]) if C.shape != x.shape: raise ValueError('C array must have same length as triangulation x and' ' y arrays') # Color values, one per triangle, mean of the 3 vertex color values. C = C[triangles].mean(axis=1) if shading == 'faceted': edgecolors = (0, 0, 0, 1), linewidths = (0.25, ) else: edgecolors = 'face' linewidths = (1.0, ) kwargs.setdefault('edgecolors', edgecolors) kwargs.setdefault('antialiaseds', (0, )) kwargs.setdefault('linewidths', linewidths) collection = PolyCollection(verts, **kwargs) collection.set_alpha(alpha) collection.set_array(C) if norm is not None: assert (isinstance(norm, Normalize)) collection.set_cmap(cmap) collection.set_norm(norm) if vmin is not None or vmax is not None: collection.set_clim(vmin, vmax) else: collection.autoscale_None() ax.grid(False) minx = tri.x.min() maxx = tri.x.max() miny = tri.y.min() maxy = tri.y.max() corners = (minx, miny), (maxx, maxy) ax.update_datalim(corners) ax.autoscale_view() ax.add_collection(collection) return collection