为geopandas p生成图例

2024-04-28 19:38:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Geopandas绘制一个形状文件。另外,我正在添加一个数据帧的点(见图)。现在我试着为这个点添加一个图例(在原始图的右边)。我真的不知道怎么做!你知道吗

Plot

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import geopandas as gpd
import test

variable = 'RTD_rtd'

df = test.getdataframe()
gdf = gpd.GeoDataFrame(
    df, geometry=gpd.points_from_xy(df.NP_LongDegree, df.NP_LatDegree))


fp = "xxx"
map_df = gpd.read_file(fp)
ax = map_df.plot(color='white', edgecolor='black', linewidth=0.4, figsize= (10,10))

gdf.plot(column=variable, ax=ax, cmap='Reds', markersize=14.0, linewidth=2.0)
plt.show()

一个想法是添加一个简单的传说。我想要好看点的。可能与本教程中所做的类似:Tutorial


Tags: testimportmapdfplotasnpplt
2条回答

我遵循了你提到的例子,这是简明的版本。如果您可以共享一点数据集“df”,那就更好了。你好像想要一个色条图色条生成。你知道吗

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import geopandas as gpd
import test
from shapely.geometry import Point

df = pd.read_csv('london-borough-profiles.csv', header=0)
df = df[['Area name','Population density (per hectare) 2017']]

fp = 'London_Borough_Excluding_MHW.shp'
map_df = gpd.read_file(fp)
gdf = map_df.set_index('NAME').join(df.set_index('Area name'))

variable = 'Population density (per hectare) 2017'
vmin, vmax = 120, 220
fig, ax = plt.subplots(1, figsize=(10, 6))
gdf.plot(column=variable, cmap='Blues', ax = ax, linewidth=0.8, edgecolor='0.8')
ax.axis('off')
ax.set_title('Population density (per hectare) 2017', fontdict={'fontsize': '25', 'fontweight' : '3'})
ax.annotate('Source: London Datastore, 2014',xy=(0.1, .08),  xycoords='figure fraction', horizontalalignment='left', verticalalignment='top', fontsize=12, color='#555555')
sm = plt.cm.ScalarMappable(cmap='Blues', norm=plt.Normalize(vmin=vmin, vmax=vmax))
sm._A = []
cbar = fig.colorbar(sm)

Output

可以将其添加到解决方案中,为此,必须为每个绘图设置标签

plt.legend()

相关问题 更多 >