尝试在Geopandas的同一地块中与两个传说一起绘制两个Geodataframes

发布于 2025-01-27 13:02:12 字数 1630 浏览 1 评论 0原文

,我在与地理的同一地块中遇到了麻烦。我成功地分别获得了彼此的情节,但是要绘制两个传说(一个传说),我不能。下面是读取数据集的代码。

dfGeo = pd.read_csv('drive/MyDrive/dfGeo.csv', sep=',').dropna()
dfGeo.drop_duplicates(subset=['Link'], inplace=True)
gdf = gpd.GeoDataFrame(dfGeo, geometry=gpd.points_from_xy(dfGeo.long, dfGeo.lat))
bairros = gpd.read_file('drive/MyDrive/bairros.geojson')

下面是绘制带有传说的第一个地图的代码。

fig, ax = plt.subplots(figsize=(10,20))
bairros.plot(column='rpa', legend=True, categorical=True, ax=ax)

“

这是第一个绘制每个层没有传说的代码

fig, ax = plt.subplots(figsize=(10,20))
bins = mapc.Quantiles(gdf['Preco'], k=5).bins
ax.set_aspect('equal')
bairros.plot(ax=ax, color='gray', edgecolor='silver')
gdf.plot(ax=ax, marker='o', markersize=12, color='gold')
plt.show()

fig, ax = plt.subplots(figsize=(10,20))
bins = mapc.Quantiles(gdf['Preco'], k=5).bins
ax.set_aspect('equal')

# legend this doesn't appear
bairros.plot(column='rpa', legend=True, categorical=True, ax=ax)

# legend this appear    
gdf.plot(column='Preco', cmap='inferno', ax=ax, marker='o', markersize=12, legend=True, scheme="User_Defined", classification_kwds=dict(bins=bins))

plt.show()

“第三图”

我想绘制并将两者的传说放在同一图中。我该怎么办? obs:我尝试了问题,但是输出没有任何传说。

第四图

预先感谢!

I have gotten a trouble with multi layers in the same plot with geopandas. I got plot both successfully separately, however to plot two legends (one to each) I could not. Below it is the code to read the datasets.

dfGeo = pd.read_csv('drive/MyDrive/dfGeo.csv', sep=',').dropna()
dfGeo.drop_duplicates(subset=['Link'], inplace=True)
gdf = gpd.GeoDataFrame(dfGeo, geometry=gpd.points_from_xy(dfGeo.long, dfGeo.lat))
bairros = gpd.read_file('drive/MyDrive/bairros.geojson')

Below it is the code to plot the first map with legend to it.

fig, ax = plt.subplots(figsize=(10,20))
bairros.plot(column='rpa', legend=True, categorical=True, ax=ax)

first plot

This is the first code to plot both layers without legend to each one

fig, ax = plt.subplots(figsize=(10,20))
bins = mapc.Quantiles(gdf['Preco'], k=5).bins
ax.set_aspect('equal')
bairros.plot(ax=ax, color='gray', edgecolor='silver')
gdf.plot(ax=ax, marker='o', markersize=12, color='gold')
plt.show()

second plot

At end it is the code trying to plot both with legend to each one, but unsuccessfully the only one legend appeared from the second plot.

fig, ax = plt.subplots(figsize=(10,20))
bins = mapc.Quantiles(gdf['Preco'], k=5).bins
ax.set_aspect('equal')

# legend this doesn't appear
bairros.plot(column='rpa', legend=True, categorical=True, ax=ax)

# legend this appear    
gdf.plot(column='Preco', cmap='inferno', ax=ax, marker='o', markersize=12, legend=True, scheme="User_Defined", classification_kwds=dict(bins=bins))

plt.show()

third plot

I would like to plot and putting the legend of both in the same plot. How could I do it?
Obs: I tried a solution from this issue, but the output changed without any legend.

fourth plot

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你不是我要的菜∠ 2025-02-03 13:02:12

您需要使用ax.get_legend从第一个图中保存图例,然后在使用ax.add_artist中手动将其添加回它。这是一个最小的例子:

import geopandas as gpd
import matplotlib.pyplot as plt

gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# subset to just a few countries in North America
gdf = gdf.loc[gdf.name.isin(['Canada', 'United States of America', 'Mexico'])]

gdf2 = gpd.GeoDataFrame(gdf.drop(columns='geometry'), geometry=gdf.centroid)

fig, ax = plt.subplots()
gdf.plot(column='name', ax=ax, legend=True)
leg1 = ax.get_legend() # save the legend from the first plot
gdf2.plot(column='continent', ax=ax, legend=True, legend_kwds={'loc': 'upper left'}, cmap='Pastel1')
ax.add_artist(leg1) # add the first legend back in

这产生以下(荒谬)图:

You need to save the legend from the first plot using ax.get_legend, then manually add it back in using ax.add_artist. Here's a minimal example:

import geopandas as gpd
import matplotlib.pyplot as plt

gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# subset to just a few countries in North America
gdf = gdf.loc[gdf.name.isin(['Canada', 'United States of America', 'Mexico'])]

gdf2 = gpd.GeoDataFrame(gdf.drop(columns='geometry'), geometry=gdf.centroid)

fig, ax = plt.subplots()
gdf.plot(column='name', ax=ax, legend=True)
leg1 = ax.get_legend() # save the legend from the first plot
gdf2.plot(column='continent', ax=ax, legend=True, legend_kwds={'loc': 'upper left'}, cmap='Pastel1')
ax.add_artist(leg1) # add the first legend back in

this produces the following (nonsensical) figure:
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文