簡體   English   中英

為大熊貓圖生成圖例

[英]Generating Legend for geopandas plot

我正在用Geopandas繪制形狀文件。 另外,im添加數據幀的點(參見圖片)。 現在,我正在嘗試為該點添加圖例(在原始圖的右側)。 我真的不知道該怎么做!

情節

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()

一個想法是添加一個簡單的圖例。 我想要更好的東西。 也許與本教程中的操作類似: 教程

我遵循了您所引用的示例,這是簡潔的版本。 如果可以共享一些數據集“ df”,那就更好了。 似乎您想擁有一個由fig.colorbar生成的colorbar。

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)

輸出量

您可以將其添加到解決方案中,為此必須為每個圖設置標簽

plt.legend()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM