尝试在Geopandas的同一地块中与两个传说一起绘制两个Geodataframes
,我在与地理的同一地块中遇到了麻烦。我成功地分别获得了彼此的情节,但是要绘制两个传说(一个传说),我不能。下面是读取数据集的代码。
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)
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()
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()
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.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
ax.get_legend
从第一个图中保存图例,然后在使用ax.add_artist
中手动将其添加回它。这是一个最小的例子:这产生以下(荒谬)图:

You need to save the legend from the first plot using
ax.get_legend
, then manually add it back in usingax.add_artist
. Here's a minimal example:this produces the following (nonsensical) figure:
