import os
# os.environ['PROJ_LIB'] = '/git/usr/miniforge/envs/python312/share/proj'
GeoPandas提供了一个用于制作地图的高级接口 Matplotlib 库。
其地图绘制功能与GeoSeries或GeoDataFrame使用 plot()
方法一样便于操作。
一些示例数据:
%matplotlib inline
import geopandas as gpd
wdata_path = './ne-data/ne_110m_admin_0_countries.shp'
world = gpd.read_file(wdata_path)
wcity_data = './ne-data/ne_110m_populated_places.shp'
# cities = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))
cities = gpd.read_file(wcity_data)
查看数据。
world.head()
geometry | |
---|---|
0 | MULTIPOLYGON (((180 -16.06713, 180 -16.55522, ... |
1 | POLYGON ((33.90371 -0.95, 34.07262 -1.05982, 3... |
2 | POLYGON ((-8.66559 27.65643, -8.66512 27.58948... |
3 | MULTIPOLYGON (((-122.84 49, -122.97421 49.0025... |
4 | MULTIPOLYGON (((-122.84 49, -120 49, -117.0312... |
world.crs = 'EPSG:4326'
cities.head()
SCALERANK | NATSCALE | LABELRANK | FEATURECLA | NAME | NAMEPAR | NAMEALT | NAMEASCII | ADM0CAP | CAPIN | ... | FCLASS_ID | FCLASS_PL | FCLASS_GR | FCLASS_IT | FCLASS_NL | FCLASS_SE | FCLASS_BD | FCLASS_UA | FCLASS_TLC | geometry | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 8 | 10 | 3 | Admin-0 capital | Vatican City | None | None | Vatican City | 1 | None | ... | None | None | None | None | None | None | None | None | None | POINT (12.45339 41.90328) |
1 | 7 | 20 | 0 | Admin-0 capital | San Marino | None | None | San Marino | 1 | None | ... | None | None | None | None | None | None | None | None | None | POINT (12.44177 43.9361) |
2 | 7 | 20 | 0 | Admin-0 capital | Vaduz | None | None | Vaduz | 1 | None | ... | None | None | None | None | None | None | None | None | None | POINT (9.51667 47.13372) |
3 | 6 | 30 | 8 | Admin-0 capital alt | Lobamba | None | None | Lobamba | 0 | Legislative and | ... | None | None | None | None | None | None | None | None | None | POINT (31.2 -26.46667) |
4 | 6 | 30 | 8 | Admin-0 capital | Luxembourg | None | None | Luxembourg | 1 | None | ... | None | None | None | None | None | None | None | None | None | POINT (6.13 49.61166) |
5 rows × 138 columns
cities.crs
<Geographic 2D CRS: EPSG:4326> Name: WGS 84 Axis Info [ellipsoidal]: - Lat[north]: Geodetic latitude (degree) - Lon[east]: Geodetic longitude (degree) Area of Use: - name: World. - bounds: (-180.0, -90.0, 180.0, 90.0) Datum: World Geodetic System 1984 ensemble - Ellipsoid: WGS 84 - Prime Meridian: Greenwich
我们现在可以绘制这些GeoDataFrames:
world.plot();
# Plot by GDP per capta
world = world[(world.pop_est>0) & (world.name!="Antarctica")]
world['gdp_per_cap'] = world.gdp_md_est / world.pop_est
world.plot(column='gdp_per_cap');
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /tmp/ipykernel_224/2951389404.py in ?() 1 # Plot by GDP per capta ----> 2 world = world[(world.pop_est>0) & (world.name!="Antarctica")] 3 4 world['gdp_per_cap'] = world.gdp_md_est / world.pop_est 5 /opt/conda/lib/python3.12/site-packages/pandas/core/generic.py in ?(self, name) 6295 and name not in self._accessors 6296 and self._info_axis._can_hold_identifiers_and_holds_name(name) 6297 ): 6298 return self[name] -> 6299 return object.__getattribute__(self, name) AttributeError: 'GeoDataFrame' object has no attribute 'pop_est'
选择颜色。 还可以使用cmap选项修改plot颜色(有关色彩图的完整列表,请参阅matplotlib网站):
world.plot(column='gdp_per_cap', cmap='OrRd');
颜色映射的缩放方式也可以使用scheme选项(如果你已经安装了pysal,可以通过conda install pysal来实现)。
默认情况下,scheme设置为 equal_intervals
,但也可以调整为任何其他pysal选项,如“分位数”,“百分位数”等。
另外在制图方面可以安装 Python 的
mapclassify
,以支持更多的制图方式。
pip install mapclassify
使用如下方式:
world.plot(column='gdp_per_cap', cmap='OrRd', scheme='quantiles');
world.crs
# Look at capitals
# Note use of standard `pyplot` line style options
cities.plot(marker='*', color='green', markersize=5);
# Check crs
cities = cities.to_crs(world.crs)
# Now we can overlay over country outlines
# And yes, there are lots of island capitals
# apparently in the middle of the ocean!
方法1
base = world.plot(color='white')
cities.plot(ax=base, marker='o', color='red', markersize=5);
使用matplotlib对象
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
# set aspect to equal. This is done automatically
# when using *geopandas* plot on it's own, but not when
# working with pyplot directly.
ax.set_aspect('equal')
world.plot(ax=ax, color='white')
cities.plot(ax=ax, marker='o', color='red', markersize=5)
fig, ax = plt.subplots()
# set aspect to equal. This is done automatically
# when using *geopandas* plot on it's own, but not when
# working with pyplot directly.
ax.set_aspect('equal')
world.plot(ax=ax, color='white')
cities.plot(ax=ax, marker='o', color='red', markersize=5)
plt.show()