备注
交互式地图绘制#
除了静态的情节, geopandas
can create interactive maps based on the folium 类库。
为交互式探索创建地图反映了的API static plots 在一个 explore() GeoSeries或GeoDataFrame的方法。
加载一些示例数据:
[1]:
import geopandas
nybb = geopandas.read_file(geopandas.datasets.get_path('nybb'))
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
cities = geopandas.read_file(geopandas.datasets.get_path('naturalearth_cities'))
最简单的选择是使用 GeoDataFrame.explore()
:
[2]:
nybb.explore()
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/explore.py:271, in _explore(df, column, cmap, color, m, tiles, attr, tooltip, popup, highlight, categorical, legend, scheme, k, vmin, vmax, width, height, categories, classification_kwds, control_scale, marker_type, marker_kwds, style_kwds, highlight_kwds, missing_kwds, tooltip_kwds, popup_kwds, legend_kwds, map_kwds, **kwargs)
270 import matplotlib.pyplot as plt
--> 271 from mapclassify import classify
272 except (ImportError, ModuleNotFoundError):
ModuleNotFoundError: No module named 'mapclassify'
During handling of the above exception, another exception occurred:
ImportError Traceback (most recent call last)
Input In [2], in <cell line: 1>()
----> 1 nybb.explore()
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/geodataframe.py:1903, in GeoDataFrame.explore(self, *args, **kwargs)
1900 @doc(_explore)
1901 def explore(self, *args, **kwargs):
1902 """Interactive map based on folium/leaflet.js"""
-> 1903 return _explore(self, *args, **kwargs)
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/explore.py:273, in _explore(df, column, cmap, color, m, tiles, attr, tooltip, popup, highlight, categorical, legend, scheme, k, vmin, vmax, width, height, categories, classification_kwds, control_scale, marker_type, marker_kwds, style_kwds, highlight_kwds, missing_kwds, tooltip_kwds, popup_kwds, legend_kwds, map_kwds, **kwargs)
271 from mapclassify import classify
272 except (ImportError, ModuleNotFoundError):
--> 273 raise ImportError(
274 "The 'folium', 'matplotlib' and 'mapclassify' packages are required for "
275 "'explore()'. You can install them using "
276 "'conda install -c conda-forge folium matplotlib mapclassify' "
277 "or 'pip install folium matplotlib mapclassify'."
278 )
280 # xyservices is an optional dependency
281 try:
ImportError: The 'folium', 'matplotlib' and 'mapclassify' packages are required for 'explore()'. You can install them using 'conda install -c conda-forge folium matplotlib mapclassify' or 'pip install folium matplotlib mapclassify'.
交互式打印在很大程度上提供了与静态打印相同的定制功能,此外还提供了一些功能。检查下面的代码,该代码绘制了一个定制的科洛普莱斯地图。您可以使用 "BoroName"
将纽约区名称作为合唱的输入的列,在悬停时在工具提示中仅显示其名称,但在单击时显示所有值。您还可以传递自定义背景切片(Folium支持的名称或可识别的名称 xyzservices.providers.query_name()
、XYZ URL或 xyzservices.TileProvider
对象),指定色彩映射表(均受 matplotlib
),并指定黑色轮廓。
注意事项
请注意,如果您想要使用背景瓦片,则GeoDataFrame需要设置CRS。
[3]:
nybb.explore(
column="BoroName", # make choropleth based on "BoroName" column
tooltip="BoroName", # show "BoroName" value in tooltip (on hover)
popup=True, # show all values in popup (on click)
tiles="CartoDB positron", # use "CartoDB positron" tiles
cmap="Set1", # use "Set1" matplotlib colormap
style_kwds=dict(color="black") # use black outline
)
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/explore.py:271, in _explore(df, column, cmap, color, m, tiles, attr, tooltip, popup, highlight, categorical, legend, scheme, k, vmin, vmax, width, height, categories, classification_kwds, control_scale, marker_type, marker_kwds, style_kwds, highlight_kwds, missing_kwds, tooltip_kwds, popup_kwds, legend_kwds, map_kwds, **kwargs)
270 import matplotlib.pyplot as plt
--> 271 from mapclassify import classify
272 except (ImportError, ModuleNotFoundError):
ModuleNotFoundError: No module named 'mapclassify'
During handling of the above exception, another exception occurred:
ImportError Traceback (most recent call last)
Input In [3], in <cell line: 1>()
----> 1 nybb.explore(
2 column="BoroName", # make choropleth based on "BoroName" column
3 tooltip="BoroName", # show "BoroName" value in tooltip (on hover)
4 popup=True, # show all values in popup (on click)
5 tiles="CartoDB positron", # use "CartoDB positron" tiles
6 cmap="Set1", # use "Set1" matplotlib colormap
7 style_kwds=dict(color="black") # use black outline
8 )
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/geodataframe.py:1903, in GeoDataFrame.explore(self, *args, **kwargs)
1900 @doc(_explore)
1901 def explore(self, *args, **kwargs):
1902 """Interactive map based on folium/leaflet.js"""
-> 1903 return _explore(self, *args, **kwargs)
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/explore.py:273, in _explore(df, column, cmap, color, m, tiles, attr, tooltip, popup, highlight, categorical, legend, scheme, k, vmin, vmax, width, height, categories, classification_kwds, control_scale, marker_type, marker_kwds, style_kwds, highlight_kwds, missing_kwds, tooltip_kwds, popup_kwds, legend_kwds, map_kwds, **kwargs)
271 from mapclassify import classify
272 except (ImportError, ModuleNotFoundError):
--> 273 raise ImportError(
274 "The 'folium', 'matplotlib' and 'mapclassify' packages are required for "
275 "'explore()'. You can install them using "
276 "'conda install -c conda-forge folium matplotlib mapclassify' "
277 "or 'pip install folium matplotlib mapclassify'."
278 )
280 # xyservices is an optional dependency
281 try:
ImportError: The 'folium', 'matplotlib' and 'mapclassify' packages are required for 'explore()'. You can install them using 'conda install -c conda-forge folium matplotlib mapclassify' or 'pip install folium matplotlib mapclassify'.
这个 explore()
方法返回一个 folium.Map
对象,该对象也可以直接传递(就像使用 ax
在……里面 plot()
)。然后,您可以在生成的地图上直接使用Folium功能。在下面的示例中,您可以在同一地图上绘制两个GeoDataFrame,并使用Folium添加层控件。您还可以添加其他瓷砖,使您可以直接在地图中更改背景。
[4]:
import folium
m = world.explore(
column="pop_est", # make choropleth based on "BoroName" column
scheme="naturalbreaks", # use mapclassify's natural breaks scheme
legend=True, # show legend
k=10, # use 10 bins
legend_kwds=dict(colorbar=False), # do not use colorbar
name="countries" # name of the layer in the map
)
cities.explore(
m=m, # pass the map object
color="red", # use red color on all points
marker_kwds=dict(radius=10, fill=True), # make marker radius 10px with fill
tooltip="name", # show "name" column in the tooltip
tooltip_kwds=dict(labels=False), # do not show column label in the tooltip
name="cities" # name of the layer in the map
)
folium.TileLayer('Stamen Toner', control=True).add_to(m) # use folium to add alternative tiles
folium.LayerControl().add_to(m) # use folium to add layer control
m # show map
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/explore.py:271, in _explore(df, column, cmap, color, m, tiles, attr, tooltip, popup, highlight, categorical, legend, scheme, k, vmin, vmax, width, height, categories, classification_kwds, control_scale, marker_type, marker_kwds, style_kwds, highlight_kwds, missing_kwds, tooltip_kwds, popup_kwds, legend_kwds, map_kwds, **kwargs)
270 import matplotlib.pyplot as plt
--> 271 from mapclassify import classify
272 except (ImportError, ModuleNotFoundError):
ModuleNotFoundError: No module named 'mapclassify'
During handling of the above exception, another exception occurred:
ImportError Traceback (most recent call last)
Input In [4], in <cell line: 3>()
1 import folium
----> 3 m = world.explore(
4 column="pop_est", # make choropleth based on "BoroName" column
5 scheme="naturalbreaks", # use mapclassify's natural breaks scheme
6 legend=True, # show legend
7 k=10, # use 10 bins
8 legend_kwds=dict(colorbar=False), # do not use colorbar
9 name="countries" # name of the layer in the map
10 )
12 cities.explore(
13 m=m, # pass the map object
14 color="red", # use red color on all points
(...)
18 name="cities" # name of the layer in the map
19 )
21 folium.TileLayer('Stamen Toner', control=True).add_to(m) # use folium to add alternative tiles
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/geodataframe.py:1903, in GeoDataFrame.explore(self, *args, **kwargs)
1900 @doc(_explore)
1901 def explore(self, *args, **kwargs):
1902 """Interactive map based on folium/leaflet.js"""
-> 1903 return _explore(self, *args, **kwargs)
File /usr/local/lib/python3.10/dist-packages/geopandas-0.10.2+79.g3abc6a7-py3.10.egg/geopandas/explore.py:273, in _explore(df, column, cmap, color, m, tiles, attr, tooltip, popup, highlight, categorical, legend, scheme, k, vmin, vmax, width, height, categories, classification_kwds, control_scale, marker_type, marker_kwds, style_kwds, highlight_kwds, missing_kwds, tooltip_kwds, popup_kwds, legend_kwds, map_kwds, **kwargs)
271 from mapclassify import classify
272 except (ImportError, ModuleNotFoundError):
--> 273 raise ImportError(
274 "The 'folium', 'matplotlib' and 'mapclassify' packages are required for "
275 "'explore()'. You can install them using "
276 "'conda install -c conda-forge folium matplotlib mapclassify' "
277 "or 'pip install folium matplotlib mapclassify'."
278 )
280 # xyservices is an optional dependency
281 try:
ImportError: The 'folium', 'matplotlib' and 'mapclassify' packages are required for 'explore()'. You can install them using 'conda install -c conda-forge folium matplotlib mapclassify' or 'pip install folium matplotlib mapclassify'.