def test_points(self): # failing with matplotlib 1.4.3 (edge stays black even when specified) pytest.importorskip("matplotlib", "1.5.0") from geopandas.plotting import plot_point_collection from matplotlib.collections import PathCollection fig, ax = plt.subplots() coll = plot_point_collection(ax, self.points) assert isinstance(coll, PathCollection) ax.cla() # default: single default matplotlib color coll = plot_point_collection(ax, self.points) _check_colors(self.N, coll.get_facecolors(), [MPL_DFT_COLOR] * self.N) # edgecolor depends on matplotlib version # _check_colors(self.N, coll.get_edgecolors(), [MPL_DFT_COLOR]*self.N) ax.cla() # specify single other color coll = plot_point_collection(ax, self.points, color="g") _check_colors(self.N, coll.get_facecolors(), ["g"] * self.N) _check_colors(self.N, coll.get_edgecolors(), ["g"] * self.N) ax.cla() # specify edgecolor/facecolor coll = plot_point_collection(ax, self.points, facecolor="g", edgecolor="r") _check_colors(self.N, coll.get_facecolors(), ["g"] * self.N) _check_colors(self.N, coll.get_edgecolors(), ["r"] * self.N) ax.cla() # list of colors coll = plot_point_collection(ax, self.points, color=["r", "g", "b"]) _check_colors(self.N, coll.get_facecolors(), ["r", "g", "b"]) _check_colors(self.N, coll.get_edgecolors(), ["r", "g", "b"]) ax.cla() coll = plot_point_collection( ax, self.points, color=[(0.5, 0.5, 0.5, 0.5), (0.1, 0.2, 0.3, 0.5), (0.4, 0.5, 0.6, 0.5)], ) _check_colors( self.N, coll.get_facecolors(), [(0.5, 0.5, 0.5, 0.5), (0.1, 0.2, 0.3, 0.5), (0.4, 0.5, 0.6, 0.5)], ) _check_colors( self.N, coll.get_edgecolors(), [(0.5, 0.5, 0.5, 0.5), (0.1, 0.2, 0.3, 0.5), (0.4, 0.5, 0.6, 0.5)], ) ax.cla() # not a color with pytest.raises(TypeError): plot_point_collection(ax, self.points, color="not color")
def test_points_values(self): from geopandas.plotting import plot_point_collection # default colormap fig, ax = plt.subplots() coll = plot_point_collection(ax, self.points, self.values) fig.canvas.draw_idle() cmap = plt.get_cmap() expected_colors = cmap(np.arange(self.N) / (self.N - 1)) _check_colors(self.N, coll.get_facecolors(), expected_colors)
def test_points(self): # failing with matplotlib 1.4.3 (edge stays black even when specified) pytest.importorskip('matplotlib', '1.5.0') from geopandas.plotting import plot_point_collection from matplotlib.collections import PathCollection fig, ax = plt.subplots() coll = plot_point_collection(ax, self.points) assert isinstance(coll, PathCollection) ax.cla() # default: single default matplotlib color coll = plot_point_collection(ax, self.points) _check_colors(self.N, coll.get_facecolors(), [MPL_DFT_COLOR] * self.N) # edgecolor depends on matplotlib version # _check_colors(self.N, coll.get_edgecolors(), [MPL_DFT_COLOR]*self.N) ax.cla() # specify single other color coll = plot_point_collection(ax, self.points, color='g') _check_colors(self.N, coll.get_facecolors(), ['g'] * self.N) _check_colors(self.N, coll.get_edgecolors(), ['g'] * self.N) ax.cla() # specify edgecolor/facecolor coll = plot_point_collection(ax, self.points, facecolor='g', edgecolor='r') _check_colors(self.N, coll.get_facecolors(), ['g'] * self.N) _check_colors(self.N, coll.get_edgecolors(), ['r'] * self.N) ax.cla() # list of colors coll = plot_point_collection(ax, self.points, color=['r', 'g', 'b']) _check_colors(self.N, coll.get_facecolors(), ['r', 'g', 'b']) _check_colors(self.N, coll.get_edgecolors(), ['r', 'g', 'b']) ax.cla()
def m_plot_dataframe(s, column=None, colormap=None, alpha=0.5, edgecolor=None, categorical=False, legend=False, axes=None, scheme=None, contour_poly_width=0.5, k=5): """ Plot a GeoDataFrame Generate a plot of a GeoDataFrame with matplotlib. If a column is specified, the plot coloring will be based on values in that column. Otherwise, a categorical plot of the geometries in the `geometry` column will be generated. Parameters ---------- GeoDataFrame The GeoDataFrame to be plotted. Currently Polygon, MultiPolygon, LineString, MultiLineString and Point geometries can be plotted. column : str (default None) The name of the column to be plotted. categorical : bool (default False) If False, colormap will reflect numerical values of the column being plotted. For non-numerical columns (or if column=None), this will be set to True. colormap : str (default 'Set1') The name of a colormap recognized by matplotlib. alpha : float (default 0.5) Alpha value for polygon fill regions. Has no effect for lines or points. legend : bool (default False) Plot a legend (Experimental; currently for categorical plots only) axes : matplotlib.pyplot.Artist (default None) axes on which to draw the plot scheme : pysal.esda.mapclassify.Map_Classifier Choropleth classification schemes k : int (default 5) Number of classes (ignored if scheme is None) Returns ------- matplotlib axes instance """ import matplotlib.pyplot as plt from matplotlib.lines import Line2D from matplotlib.colors import Normalize from matplotlib import cm if column is None: return plot_series(s.geometry, colormap=colormap, alpha=alpha, axes=axes) else: if s[column].dtype is np.dtype('O'): categorical = True if categorical: if colormap is None: colormap = 'Set1' categories = list(set(s[column].values)) categories.sort() valuemap = dict([(key, v) for (v, key) in enumerate(categories)]) values = [valuemap[key] for key in s[column]] else: values = s[column] if scheme is not None: values = _mapclassify_choro(values, scheme, k=k) norm = Normalize(vmin=values.min(), vmax=values.max()) cmap = cm.ScalarMappable(norm=norm, cmap=colormap) if not axes: fig = plt.gcf() fig.add_subplot(111, aspect='equal') ax = plt.gca() else: ax = axes for geom, value in zip(s.geometry, values): if geom.type == 'Polygon' or geom.type == 'MultiPolygon': m_plot_multipolygon(ax, geom, facecolor=cmap.to_rgba(value), edgecolor=edgecolor, linewidth=contour_poly_width, alpha=alpha) elif geom.type == 'LineString' or geom.type == 'MultiLineString': plot_linestring_collection(ax, GeoSeries([geom]), colors=[cmap.to_rgba(value)]) # TODO: color point geometries elif geom.type == 'Point': plot_point_collection(ax, GeoSeries([geom])) if legend: if categorical: patches = [] for value, cat in enumerate(categories): patches.append( Line2D([0], [0], linestyle="none", marker="o", alpha=alpha, markersize=10, markerfacecolor=cmap.to_rgba(value))) ax.legend(patches, categories, numpoints=1, loc='best') else: # TODO: show a colorbar raise NotImplementedError plt.draw() return ax