def test_linestrings_values(self): from geopandas.plotting import plot_linestring_collection fig, ax = plt.subplots() # default colormap coll = plot_linestring_collection(ax, self.lines, 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_color(), expected_colors) ax.cla() # specify colormap coll = plot_linestring_collection(ax, self.lines, self.values, cmap='RdBu') fig.canvas.draw_idle() cmap = plt.get_cmap('RdBu') expected_colors = cmap(np.arange(self.N) / (self.N - 1)) _check_colors(self.N, coll.get_color(), expected_colors) ax.cla() # specify vmin/vmax coll = plot_linestring_collection(ax, self.lines, self.values, vmin=3, vmax=5) fig.canvas.draw_idle() cmap = plt.get_cmap() expected_colors = cmap([0]) _check_colors(self.N, coll.get_color(), expected_colors) ax.cla()
def test_linestrings(self): from geopandas.plotting import plot_linestring_collection from matplotlib.collections import LineCollection fig, ax = plt.subplots() coll = plot_linestring_collection(ax, self.lines) assert isinstance(coll, LineCollection) ax.cla() # default: single default matplotlib color coll = plot_linestring_collection(ax, self.lines) _check_colors(self.N, coll.get_color(), [MPL_DFT_COLOR] * self.N) ax.cla() # specify single other color coll = plot_linestring_collection(ax, self.lines, color='g') _check_colors(self.N, coll.get_colors(), ['g'] * self.N) ax.cla() # specify edgecolor / facecolor coll = plot_linestring_collection(ax, self.lines, 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_linestring_collection(ax, self.lines, color=['r', 'g', 'b']) _check_colors(self.N, coll.get_colors(), ['r', 'g', 'b']) ax.cla() # pass through of kwargs coll = plot_linestring_collection(ax, self.lines, linestyle='--', linewidth=1) exp_ls = _style_to_linestring_onoffseq('dashed', 1) res_ls = coll.get_linestyle()[0] assert res_ls[0] == exp_ls[0] assert res_ls[1] == exp_ls[1] 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
def test_linestrings(self): from geopandas.plotting import plot_linestring_collection from matplotlib.collections import LineCollection fig, ax = plt.subplots() coll = plot_linestring_collection(ax, self.lines) assert isinstance(coll, LineCollection) ax.cla() # default: single default matplotlib color coll = plot_linestring_collection(ax, self.lines) _check_colors(self.N, coll.get_color(), [MPL_DFT_COLOR] * self.N) ax.cla() # specify single other color coll = plot_linestring_collection(ax, self.lines, color="g") _check_colors(self.N, coll.get_colors(), ["g"] * self.N) ax.cla() # specify edgecolor / facecolor coll = plot_linestring_collection(ax, self.lines, 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_linestring_collection(ax, self.lines, color=["r", "g", "b"]) _check_colors(self.N, coll.get_colors(), ["r", "g", "b"]) ax.cla() coll = plot_linestring_collection( ax, self.lines, 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_colors(), [(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() # pass through of kwargs coll = plot_linestring_collection(ax, self.lines, linestyle="--", linewidth=1) exp_ls = _style_to_linestring_onoffseq("dashed", 1) res_ls = coll.get_linestyle()[0] assert res_ls[0] == exp_ls[0] assert res_ls[1] == exp_ls[1] ax.cla() # not a color with pytest.raises(TypeError): plot_linestring_collection(ax, self.lines, color="not color")