geopandas.GeoDataFrame.plot#
- GeoDataFrame.plot()[source]#
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.
- Parameters:
- column
str,np.array,pd.Series,pd.Index(defaultNone) The name of the GeoDataFrame column,
np.array,pd.Series, orpd.Indexto be plotted. Ifnp.array,pd.Series, orpd.Indexare used then it must have same length as GeoDataFrame. Values are used to color the plot. Ignored ifcoloris also set.- kind: str
The kind of plots to produce. The default is to create a map (
"geo"). Other supported kinds of plots from pandas:'line': line plot'bar': vertical bar plot'barh': horizontal bar plot'hist': histogram'box': BoxPlot'kde': Kernel Density Estimation plot'density': same as'kde''area': area plot'pie': pie plot'scatter': scatter plot'hexbin': hexbin plot.
- cmap
str|Colormap|dict(defaultNone) The name of a colormap recognized by matplotlib, a
matplotlib.colors.Colormapor a dictionary ofvalue: colormapping for categorical plots.- color
str,np.array,pd.Series(defaultNone) Color of the geometry. If specified as scalar matplotlib understands as a color (
str,tupleor RGBA etc.), all objects will be colored uniformly. If specifies as array-like of the same length as GeoDataFrame, individual colors will be mapped to respective geometries.- ax
matplotlib.axes.Axes(defaultNone) matplotlib.axes.Axesaxes on which to draw the plot- cax
matplotlib.axes.Axes(defaultNone) matplotlib.axes.Axeson which to draw the legend in case of a colorbar.- categorical
bool(defaultFalse) Determine whether values of
columnshall be treated as a categorical variable or as a continuous one. For non-numerical columns, this will be set toTrueautomatically.- legend
bool(defaultFalse) Plot a legend. Ignored if no
columnis given, or ifcoloris given. This can be amatplotlib.legend.Legendin case of categorical variables or those with setschemeand a categorical legend, or a :class`:matplotlib.colorbar.Colorbar` in case of continuous variables or those with setschemeand a colorbar legend.- schemestr (default None)
Name of a choropleth classification scheme (requires
mapclassify). Amapclassify.classifiers.MapClassifierobject will be used under the hood. Supported are all schemes provided bymapclassify(e.g.'BoxPlot','EqualInterval','FisherJenks','FisherJenksSampled','HeadTailBreaks','JenksCaspall','JenksCaspallForced','JenksCaspallSampled','MaxP','MaximumBreaks','NaturalBreaks','Quantiles','Percentiles','StdMean','UserDefined'). Arguments can be passed inclassification_kwds.This requires continuous variable and can result in a categorical plot where each classification bin is mapped to a category (default behavior) or in a continuous plot where bins are used to define
matplotlib.colors.BoundaryNorm. The latter can be enabled by specifyingcolorbar=Truewithinlegend_kwdsand yields colorbar legend.In addition,
scheme='greedy'usesmapclassify.greedy()to derive greedy (topological) coloring which attempts to color a GeoDataFrame using as few colors as possible, where no neighbours can have same color as the feature itself. This cannot be specified together withcolumnas each geometry is treated as unique with no relation to its attributes.- k
int(default5) Number of classes (ignored if
schemeisNone)- vmin
Noneorfloat(defaultNone) Minimum value of
cmaporscheme. IfNone, the minimum data value in the column to be plotted is used.- vmax
Noneorfloat(defaultNone) Maximum value of
cmaporscheme. IfNone, the maximum data value in the column to be plotted is used.- markersize
strorfloatorSequence(default None) Only applies to point geometries within a GeoDataFrame. If a
str, will use the values in the column of the GeoDataFrame specified bymarkersizeto set the size of markers. Otherwise can be a value to apply to all points, or a sequence of the same length as the number of points.- figsize
tupleof integers (default None) Size of the resulting
matplotlib.figure.Figure. If the argumentaxis given explicitly,figsizeis ignored.- legend_kwdsdict (default None)
Keyword arguments to pass to
matplotlib.pyplot.legend()(e.g.labels, orframeon) ormatplotlib.pyplot.colorbar()(e.g.orientation). Additional accepted keywords whenschemeis specified:fmtstringA formatting specification for the bin edges of the classes in the legend. For example, to have no decimals:
{"fmt": "{:.0f}"}.intervalboolean (default False)An option to control brackets from mapclassify legend. If True, open/closed interval brackets are shown in the legend.
colorbarboolean (default False)An option to control whether the legend should be treated as categorical or as a colorbar. When set to True,
fmtandintervalshall not be used.
- categories
list-like Ordered list-like object of categories to be used for categorical plot.
- classification_kwds
dict(defaultNone) Keyword arguments to pass to
mapclassifywhenschemeis set.- missing_kwds
dict(default None) Keyword arguments specifying style options (e.g. color, hatch, linewidth) to be passed on to geometries with missing values in addition to or overwriting other
style_kwds. IfNone, geometries with missing values are not plotted.- aspect‘auto’, ‘equal’,
Noneorfloat(default'auto') Set aspect of axis. If
'auto', the default aspect for map plots is'equal'; if however data are not projected (coordinates are long/lat), the aspect is by default set to1/cos(df_y * pi/180)withdf_ythe y coordinate of the middle of the GeoDataFrame (the mean of the y range of bounding box) so that a long/lat square appears square in the middle of the plot. This implies an Equirectangular projection. IfNone, the aspect ofaxwon’t be changed. It can also be set manually (float) as the ratio of y-unit to x-unit.- autolim
bool(defaultTrue) Update axes data limits to contain the new geometries.
- tilesbool, str, xyzservices.TileProvider, os.PathLike, file-like, or rasterio.io.MemoryFile (default False)
Add contextual background tiles. Can be either a boolean,
xyzservices.TileProvider, any string that can be resolved byxyzservices.providers.query_name(), URL, or a path to a local file. The placeholders for the XYZ in the URL need to be {x}, {y}, {z}, respectively. For local file paths, the file is read with rasterio and all bands are loaded into the basemap. The tiles are automatically warped to the CRS of the geometry. Note that this can result in suboptimal rendering. To avoid warping, geometry needs to be in EPSG:3857 (Web Mercator) or a CRS of the tiles if other projection is used. Default basemap when True follows the default of the underlyingcontextily.add_basemap(), which is OpenStreetMap Humanitarian.- attrstr (default None)
Attribution text passed to
contextily.add_basemap()asattribution. When not provided, the default attribution of the selected tile source is used.- add_labelsbool (default True)
Use CRS metadata to label the axes.
- **style_kwdsdict
Style options to be passed on to the actual plot function, such as
edgecolor,facecolor,linewidth,markersize,alpha. These can be scalar, which are uniformly mapped to all geometries, array-likes of the same length as GeoDataFrame, which are mapped to their respective geometries, or dictionaries mapping styles to values when dealing with categorical plots.
- column
- Returns:
- axmatplotlib axes instance
Examples
>>> import geodatasets >>> df = geopandas.read_file(geodatasets.get_path("nybb")) >>> df.head() BoroCode ... geometry 0 5 ... MULTIPOLYGON (((970217.022 145643.332, 970227.... 1 4 ... MULTIPOLYGON (((1029606.077 156073.814, 102957... 2 3 ... MULTIPOLYGON (((1021176.479 151374.797, 102100... 3 1 ... MULTIPOLYGON (((981219.056 188655.316, 980940.... 4 2 ... MULTIPOLYGON (((1012821.806 229228.265, 101278...
>>> df.plot("BoroName", cmap="Set1")
See the User Guide page Mapping and plotting tools for details.