matplotlib 如何为GeoAxes子图创建单个地物图例

pinkon5k  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(173)

我已经看了这里的许多其他问题,试图解决这个问题,但无论出于什么原因,我不能。每个解决方案似乎给我同样的错误,给予,或返回什么都没有。
我有一个列表,其中有六个我正在循环创建一个6Map的图形。每个Map的格式类似,唯一的区别是它们的时间列。每个Map都有通过制图创建的相同的分类方案。颜色是由颜色表确定的,Map本身没有与值相关的颜色。我希望所有Map都有一个单一的图例,以便读者更容易看到,下面是我的代码:

import cartopy.crs as ccrs
import cartopy.feature as cfeature
import mapclassify
from matplotlib.colors import rgb2hex
from matplotlib.colors import ListedColormap
plt.style.use('seaborn-v0_8-dark')

# Define the Robinson projection
robinson = ccrs.Robinson()

# Create a 3x2 grid of subplots
fig, axs = plt.subplots(3, 2, figsize=(12, 12), subplot_kw={'projection': robinson})

# Flatten the subplot array for easy iteration
axs = axs.flatten()

# Define color map and how many bins needed
cmap = plt.cm.get_cmap('YlOrRd', 5) #Blues #Greens #PuRd #YlOrRd
# Any countries with NaN values will be colored grey
missing_kwds = dict(color='grey', label='No Data')

# Loop through the dataframes and create submaps
for i, df in enumerate(dataframes):
    
    # Create figure and axis with Robinson projection
    mentionsgdf_robinson = df.to_crs(robinson.proj4_init)
    
   
    # Plot the submap
    ax = axs[i]
    
    # Add land mask and gridlines
    ax.add_feature(cfeature.LAND.with_scale('50m'), facecolor='lightgrey')
    gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
                  linewidth=1, color='gray', alpha=0.3, linestyle='--')
    
    gl.xlabel_style = {'fontsize': 7}
    gl.ylabel_style = {'fontsize': 7}
    
    # Classification scheme options: EqualInterval, Quantiles, NaturalBreaks, UserDefined etc.
    mentionsgdf_robinson.plot(column='mentions', 
                              ax=ax, 
                              legend=True, #True
                              cmap=cmap, 
                              legend_kwds=({"loc":'center left', 'title': 'Number of Mentions', 'prop': {'size': 7, 'family': 'serif'}}),
                              missing_kwds=missing_kwds,
                              scheme="UserDefined", 
                              classification_kwds = {'bins':[20, 50, 150, 300, 510]})
    
    # Set the titles of each submap
    ax.set_title(f'20{i+15}', size = 15, family = 'Serif')
    
    # Define the bounds of the classification scheme
    upper_bounds = mapclassify.UserDefined(mentionsgdf_robinson.mentions, bins=[20, 50, 150, 300, 510]).bins
    
    
    bounds = []
    for index, upper_bound in enumerate(upper_bounds):
        if index == 0:
            lower_bound = mentionsgdf_robinson.mentions.min()
        else:
            lower_bound = upper_bounds[index-1]

        bound = f'{lower_bound:.0f} - {upper_bound:.0f}'
        bounds.append(bound)
    
    # replace the legend title and increase font size
    legend_title = ax.get_legend().get_title()
    legend_title.set_fontsize(8)
    legend_title.set_family('serif')
    
    
    # get all the legend labels and increase font size
    legend_labels = ax.get_legend().get_texts()
    # replace the legend labels
    for bound, legend_label in zip(bounds, legend_labels):
        legend_label.set_text(bound)
    

fig.suptitle(' Yearly Country Mentions in Online News about Species Threatened by Trade ', fontsize=15, family = 'Serif')
    
# Adjust spacing between subplots
plt.tight_layout(pad=4.0)

# Save the figure
#plt.savefig('figures/submaps_5years.png', dpi=300)

# Show the submap
plt.show()

字符串
这是我现在的结果,我希望在Map的中心位置有一个图例。
x1c 0d1x的数据
我尝试了here建议的代码,但只收到一个UserWarning:Legend does not support handles for PatchCollection instances.此外,我不知道如何在循环之外合并所有其他需要的Legend修改(边界,字体,bin等)。

handles, labels = ax.get_legend_handles_labels()
fig.legend(handles, labels, loc='upper center')


以下是2015-2017年三年的数据:https://jmp.sh/s/ohkSJpaMZ4c1GifIX0nu
下面是我使用的全局shapefile的所有文件:https://jmp.sh/uTP9DZsC
使用这些数据和下面的代码应该可以让你运行上面分享的完整的可视化代码。谢谢。

import geopandas as gpd
import pandas as pd

# Read in globe shapefile dataframe 
world = gpd.read_file("TM_WORLD_BORDERS-0.3.shp")
# Read in sample dataframe
df = pd.read_csv("fifsixseventeen.csv", sep = ";")

# Separate according to date column 
fifteen = df[(df['date'] == 2015)].reset_index(drop=True)
sixteen = df[(df['date'] == 2016)].reset_index(drop=True)
seventeen = df[(df['date'] == 2017)].reset_index(drop=True)

# Function to merge isocodes of the countries with world shapefile 
def merge_isocodes(df):
    
    # Groupby iso3 column in order to merge with shapefile
    allmentions = df.groupby("iso3")['mentions'].sum().sort_values(ascending = False).reset_index()

    # Merge on iso3 code 
    mentionsgdf = pd.merge(allmentions, world, left_on=allmentions["iso3"], right_on=world["ISO3"], how="right").drop(columns = "key_0")

    # Redefine as a geodataframe
    mentionsgdf = gpd.GeoDataFrame(mentionsgdf, geometry='geometry')
    
    return mentionsgdf

onefive = merge_isocodes(fifteen)
onesix = merge_isocodes(sixteen)
oneseven = merge_isocodes(seventeen)

# Create a list to store each years' dataframes
dataframes = [onefive, onesix, oneseven]

6ss1mwsb

6ss1mwsb1#

  • 这些Axescartopy.mpl.geoaxes.GeoAxes
  • axs[0].get_legend_handles_labels()产生UserWarning: Legend does not support handles for PatchCollection instances.,并返回([], [])
  • 使用Axes.get_legend()获取Legend示例。
  • .legend_handlesmatplotlib 3.7.2中是新的,它返回Artist对象的列表。或者,使用.legendHandles,但它已被弃用。
  • 有关图例定位的详细信息,请参见How to put the legend outside the plot
  • Moving matplotlib legend outside of the axis makes it cutoff by the figure boxfig.savefig('fig.png', bbox_inches='tight')
  • 注意:假设所有图例都相同。
  • python 3.9.18geopandas 0.12.2matplotlib 3.7.2cartopy 0.22.0中测试。
... 

for i, df in enumerate(dataframes):
    ...

# after the for loop, use the following code

# extract the legend from an axes - used the last axes for the smaller sample data
l = axs[2].get_legend()
# extract the handles
handles = l.legend_handles
# get the label text
labels = [v.get_text() for v in l.texts]
# get the title text
title = l.get_title().get_text()
# create the figure legend
fig.legend(title=title, handles=handles, labels=labels, bbox_to_anchor=(1, 0.5), loc='center left', frameon=False)

# iterate through each Axes
for ax in axs:
    # if the legend isn't None (if this condition isn't required, remove it and use only ax.get_legend().remove()) 
    if gt := ax.get_legend():
        # remove the legend
        gt.remove()

字符串


的数据

bq9c1y66

bq9c1y662#

这可能不是你正在寻找的解决方法,但我在过去已经成功地实现了。本质上,你只放置一个子图的图例,并手动操纵其位置和大小,使其落在整个图的适当位置的单个子图之外。
使用边界框函数:bbox_to_anchor()将图例放置在您选择的子图之外。
然后,使用transform参数从数据切换到轴坐标。具体来说,如果相对于axs[k]的坐标放置,那么代码可能如下所示:
axs[k].legend(bbox_to_anchor = (m,n), transform=axs[k].transAxes, borderaxespad=0)
其中m和n是控制图例位置的浮点数。这些(以我的经验)通常是通过试错来确定的。
进一步的阅读和实现mpl文档可以在here中找到。

相关问题