Python 使用geopandas制作多张choropleth地图,使用分类变量着色,并在地图上保持相同的配色方案

Python 使用geopandas制作多张choropleth地图,使用分类变量着色,并在地图上保持相同的配色方案,python,geospatial,spatial,geopandas,choropleth,Python,Geospatial,Spatial,Geopandas,Choropleth,我正在尝试制作多个choropleth贴图(在我的数据中,每年一个),根据一个分类变量填充每个多边形。我还想为这些情节附上一些传说 我使用geopandas读取数据: 将geopandas导入为gpd 将matplotlib.pyplot作为plt导入 all_gdf=gpd.read_文件('./my data.shp') 然后,我可以通过在for循环中对每年进行子集设置来创建单独的地图: years=all_gdf.year.unique() 对于y年: 图,ax=plt.子批次() tm

我正在尝试制作多个choropleth贴图(在我的数据中,每年一个),根据一个分类变量填充每个多边形。我还想为这些情节附上一些传说

我使用geopandas读取数据:

将geopandas导入为gpd
将matplotlib.pyplot作为plt导入
all_gdf=gpd.read_文件('./my data.shp')
然后,我可以通过在for循环中对每年进行子集设置来创建单独的地图:

years=all_gdf.year.unique()
对于y年:
图,ax=plt.子批次()
tmp_gdf=all_gdf.query(f'year=={y})
tmp_gdf.plot(column='col_to_plot',
图例=真,分类=真,
ax=ax)
这将为每年生成一张地图,但由于并非所有类别都为每年提供,因此我为每张地图提供了不同的颜色方案。例如,2015年可能有类别a、b、c,而2016年可能有类别a、c、d和e,因此2015年地图中c的颜色与2016年地图中c的颜色不同。我想确保所有地图的c颜色都相同

然后我发现:,他们的例子似乎正是我要寻找的,但按照他们的代码,我的地图无法生成图例

color\u dict={
‘a’:‘红色’,
“b”:蓝色,
‘c’:‘绿色’,
“d”:“黄色”,
“e”:“青色”,
“f”:“洋红”}
对于y年:
图,ax=plt.子批次()
tmp_gdf=all_gdf.query(f'year=={y})
对于ctype,tmp_gdf.groupby('col_to_plot')中的数据:
color=颜色[ctype]
data.plot(颜色=颜色,ax=ax,标签=ctype)
ax.图例()
plt.show()
这将在地图中生成正确的颜色,但拒绝制作图例


在这一点上,我完全迷路了,任何帮助都将不胜感激。

这是我的解决方案。。。作为一个例子,我将使用随GeoPandas一起提供的世界地图数据集,并绘制每个国家的GDP十分位数

# Import the things an setup
import geopandas as gpd
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from matplotlib import cm
from matplotlib.colors import ListedColormap

sns.set_style('darkgrid')
sns.set_context('notebook')

# Read the data and calculate the deciles
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world = world[(world['name'] != 'Antarctica')
              & ~world['continent'].str.contains("Seven seas")]
world['gdp_decile'] = pd.qcut(world['gdp_md_est'], 10,
                              labels=[q+1 for q in range(10)])

# Plot the world map
fig, ax = plt.subplots(figsize=(13, 10))
world.plot(column='gdp_decile', categorical=True, ax=ax, legend=True,
           legend_kwds={'title': 'Decile'})
ax.set(xticklabels=[], yticklabels=[])
ax.grid(False)

现在,让我们用相同的传奇色彩分别绘制每个大陆

# Create a colour mapping between the legend values and some colours
palette = dict(zip(sorted(world['gdp_decile'].unique()),
               [cm.tab10(x) for x in range(world['gdp_decile'].nunique())]))
cm.tab10
是一个示例彩色地图。有


我希望这有帮助

您是否尝试在
data.plot()
中添加
legend=True
。是的,我得到了相同的结果。
for continent, df in world.groupby('continent'):
    # Need to ensure the column you're plotting is sorted
    df = df.sort_values('gdp_decile')

    # Create a colormap using only the colours in this group
    cmap = ListedColormap([color for decile, color in palette.items()
                           if decile in df['gdp_decile'].unique()])

    # Do the plotting!
    fig, ax = plt.subplots(figsize=(10, 7))
    df.plot(column='gdp_decile', categorical=True, legend=True, cmap=cmap,
            ax=ax, legend_kwds={'title': 'Decile'})
    ax.set(xticklabels=[], yticklabels=[], title=continent)
    ax.grid(False)

    plt.show()