|
2 | 2 | Choropleth map
|
3 | 3 | ==============
|
4 | 4 |
|
5 |
| -The :meth:`pygmt.Figure.plot` method allows us to plot geographical data such |
6 |
| -as polygons which are stored in a :class:`geopandas.GeoDataFrame` object. Use |
7 |
| -:func:`geopandas.read_file` to load data from any supported OGR format such as |
8 |
| -a shapefile (.shp), GeoJSON (.geojson), geopackage (.gpkg), etc. You can also |
9 |
| -use a full URL pointing to your desired data source. Then, pass the |
10 |
| -:class:`geopandas.GeoDataFrame` as an argument to the ``data`` parameter of |
11 |
| -:meth:`pygmt.Figure.plot`, and style the geometry using the ``pen`` parameter. |
12 |
| -To fill the polygons based on a corresponding column you need to set |
13 |
| -``fill="+z"`` as well as select the appropriate column using the ``aspatial`` |
14 |
| -parameter as shown in the example below. |
| 5 | +The :meth:`pygmt.Figure.plot` method allows us to plot geographical data such as |
| 6 | +polygons which are stored in a :class:`geopandas.GeoDataFrame` object. Use |
| 7 | +:func:`geopandas.read_file` to load data from any supported OGR format such as a |
| 8 | +shapefile (.shp), GeoJSON (.geojson), geopackage (.gpkg), etc. You can also use a full |
| 9 | +URL pointing to your desired data source. Then, pass the class:`geopandas.GeoDataFrame` |
| 10 | +as an argument to the ``data`` parameter of :meth:`pygmt.Figure.plot`, and style the |
| 11 | +geometry using the ``pen`` parameter. To fill the polygons based on a corresponding |
| 12 | +column you need to set ``fill="+z"`` as well as select the appropriate column using the |
| 13 | +``aspatial`` parameter as shown in the example below. |
15 | 14 | """
|
16 | 15 |
|
17 | 16 | # %%
|
| 17 | +import geodatasets |
18 | 18 | import geopandas as gpd
|
19 | 19 | import pygmt
|
20 | 20 |
|
21 |
| -# Read polygon data using geopandas |
22 |
| -gdf = gpd.read_file("https://geodacenter.github.io/data-and-lab/data/airbnb.zip") |
| 21 | +# Read the example dataset provided by geodatasets. |
| 22 | +gdf = gpd.read_file(geodatasets.get_path("geoda airbnb")) |
| 23 | +print(gdf) |
23 | 24 |
|
| 25 | +# %% |
24 | 26 | fig = pygmt.Figure()
|
25 | 27 |
|
26 | 28 | fig.basemap(
|
|
29 | 31 | frame="+tPopulation of Chicago",
|
30 | 32 | )
|
31 | 33 |
|
32 |
| -# The dataset contains different attributes, here we select |
33 |
| -# the "population" column to plot. |
| 34 | +# The dataset contains different attributes, here we select the "population" column to |
| 35 | +# plot. |
34 | 36 |
|
35 |
| -# First, we define the colormap to fill the polygons based on |
36 |
| -# the "population" column. |
| 37 | +# First, we define the colormap to fill the polygons based on the "population" column. |
37 | 38 | pygmt.makecpt(
|
38 | 39 | cmap="acton",
|
39 | 40 | series=[gdf["population"].min(), gdf["population"].max(), 10],
|
40 | 41 | continuous=True,
|
41 | 42 | reverse=True,
|
42 | 43 | )
|
43 | 44 |
|
44 |
| -# Next, we plot the polygons and fill them using the defined colormap. |
45 |
| -# The target column is defined by the aspatial parameter. |
| 45 | +# Next, we plot the polygons and fill them using the defined colormap. The target column |
| 46 | +# is defined by the aspatial parameter. |
46 | 47 | fig.plot(
|
47 | 48 | data=gdf,
|
48 | 49 | pen="0.3p,gray10",
|
|
51 | 52 | aspatial="Z=population",
|
52 | 53 | )
|
53 | 54 |
|
54 |
| -# Add colorbar legend |
| 55 | +# Add colorbar legend. |
55 | 56 | fig.colorbar(frame="x+lPopulation", position="jML+o-0.5c+w3.5c/0.2c")
|
56 | 57 |
|
57 | 58 | fig.show()
|
0 commit comments