在 Python 中使用 matplotlib 在同一子图中绘制多个 geopandas 数据帧时如何添加图例?
How can I add legend while plotting multiple geopandas dataframes in the same subplot using matplotlib in Python?
我有一个 geopandas 数据框 world
,我使用以下方法创建的:
import geopandas as gpd
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
我为 usa
和 china
创建了两个不同的地理数据框,如下所示:
usa = world[world.name == "United States of America"]
china = world[world.name == "China"]
我想在地图上将美国绘制为蓝色,将中国绘制为红色。我使用以下代码行绘制它:
fig, ax = plt.subplots(figsize = (20, 8))
world.plot(ax = ax, color = "whitesmoke", ec = "black")
usa.plot(ax = ax, color = "blue", label = "USA")
china.plot(ax = ax, color = "red", label = "China")
ax.legend()
plt.show()
看起来如下:
我想添加说明美国为蓝色,中国为红色的图例。因此,我给出了上面代码中所示的标签。但是,我收到以下警告:
No artists with labels found to put in legend. Note that artists
whose label start with an underscore are ignored when legend() is
called with no argument.
我无法添加图例。如何在此情节中添加美国和中国的传说?是否可以使用 geopandas 和 matplotlib?
我从来没有用过geopandas
,但是看结果似乎那些填充区域是PathCollection
,图例不支持这些区域。但我们可以创造传奇艺术家:
import geopandas as gpd
from matplotlib.lines import Line2D
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
usa = world[world.name == "United States of America"]
china = world[world.name == "China"]
fig, ax = plt.subplots()
world.plot(ax = ax, color = "whitesmoke", ec = "black")
usa.plot(ax = ax, color = "blue", label = "USA")
china.plot(ax = ax, color = "red", label = "China")
lines = [
Line2D([0], [0], linestyle="none", marker="s", markersize=10, markerfacecolor=t.get_facecolor())
for t in ax.collections[1:]
]
labels = [t.get_label() for t in ax.collections[1:]]
ax.legend(lines, labels)
plt.show()
我有一个 geopandas 数据框 world
,我使用以下方法创建的:
import geopandas as gpd
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
我为 usa
和 china
创建了两个不同的地理数据框,如下所示:
usa = world[world.name == "United States of America"]
china = world[world.name == "China"]
我想在地图上将美国绘制为蓝色,将中国绘制为红色。我使用以下代码行绘制它:
fig, ax = plt.subplots(figsize = (20, 8))
world.plot(ax = ax, color = "whitesmoke", ec = "black")
usa.plot(ax = ax, color = "blue", label = "USA")
china.plot(ax = ax, color = "red", label = "China")
ax.legend()
plt.show()
看起来如下:
我想添加说明美国为蓝色,中国为红色的图例。因此,我给出了上面代码中所示的标签。但是,我收到以下警告:
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
我无法添加图例。如何在此情节中添加美国和中国的传说?是否可以使用 geopandas 和 matplotlib?
我从来没有用过geopandas
,但是看结果似乎那些填充区域是PathCollection
,图例不支持这些区域。但我们可以创造传奇艺术家:
import geopandas as gpd
from matplotlib.lines import Line2D
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
usa = world[world.name == "United States of America"]
china = world[world.name == "China"]
fig, ax = plt.subplots()
world.plot(ax = ax, color = "whitesmoke", ec = "black")
usa.plot(ax = ax, color = "blue", label = "USA")
china.plot(ax = ax, color = "red", label = "China")
lines = [
Line2D([0], [0], linestyle="none", marker="s", markersize=10, markerfacecolor=t.get_facecolor())
for t in ax.collections[1:]
]
labels = [t.get_label() for t in ax.collections[1:]]
ax.legend(lines, labels)
plt.show()