简体   繁体   English

如何在使用 geopandas 到 plot 等值线 map 时创建方形图例标记?

[英]How to create square shaped legend markers while using geopandas to plot a choropleth map?

I am trying create a legend which will have square shaped markers instead of round markers.我正在尝试创建一个具有方形标记而不是圆形标记的图例。 I tried to use ".set_marker('s')" and it is adding multiple markers.我尝试使用“.set_marker('s')”,它添加了多个标记。

Here is my code:这是我的代码:

fig, ax = plt.subplots(figsize=(18, 10))
europe_gdf_without_russia.plot(    
                    column="percentage_2nd_booster_60plus",
                    cmap='Blues',        
                    legend=True,        
                    legend_kwds={"title":"Percentage",
                                 'loc':'center left',
                                 "fmt": "{:.2f}%", 
                                 'bbox_to_anchor':(1,0.5),
                                },
                    edgecolor='black',       
                    linewidth=0.8,          
                    k=4,                      
                    scheme='quantiles',       
                    missing_kwds={           
                           "color": "lightgrey",
                           "label": "No data",
                            },
                    zorder=1,
                    ax=ax
             )

# Customising the texts in the legend
leg = ax.get_legend()
for count, lbl in enumerate(leg.get_texts()):
    label_text = lbl.get_text()             
    lowe1 = label_text.split()[0]            
    lower=lowe1.replace(',',' -')            
    upper = label_text.split()[1]           
    if count==0:
        new_text = ' <{}'.format(upper)
    else:
        new_text = ' {} {}'.format(lower,upper)  
    lbl.set_text(new_text)                   

leg = ax.get_legend()                       
for ea in leg.legendHandles:
    ea.set_marker('s')

I tried to use "ea.set_marker('s')" after getting the legendhandles.获得图例句柄后,我尝试使用“ea.set_marker('s')”。

Here is my code:这是我的代码:

for ea in leg.legendHandles:
    ea.set_marker('s')

And the output I am getting:我得到的 output:

多图例标记

I want to get a single square shape marker for each line of the legend我想为图例的每一行获得一个方形标记

Couldn't find a keyword argument for legend_kwds in geopandas.plot() to solve the issue.geopandas.plot()中找不到legend_kwds的关键字参数来解决问题。 Though I solved this in a tricky way.尽管我以一种棘手的方式解决了这个问题。 After accessing the legend I added the following chunk of code to get the required result.访问图例后,我添加了以下代码块以获得所需的结果。 I set the data of the legend to 1d array using the suitable positional point.我使用合适的位置点将图例data设置为一维数组。 In my case it was (11.,3.85).在我的例子中是 (11.,3.85)。 The purpose of this customise data point is to overlap the built-in legend marker from geopandas.此自定义数据点的目的是与 geopandas 的内置图例标记重叠。 Then make the markersize bigger to fully overlap the built-in legend marker and used lh._legmarker.set_markeredgecolor('None') to remove the built-in edge marker colour.然后使 markersize 更大以完全重叠内置图例标记并使用lh._legmarker.set_markeredgecolor('None')删除内置边缘标记颜色。 Here is the new chunk of code:这是新的代码块:

for lh in leg.legendHandles:
       lh.set_marker('s')
       lh.set_data((11.5,3.85))
       lh.set_markersize(17)
       lh.set_markeredgecolor("None")
       lh._legmarker.set_markeredgecolor('None')

Here is the final version of the full code:这是完整代码的最终版本:

fig, ax = plt.subplots(figsize=(18, 10))
europe_gdf_without_russia.plot( column="percentage_2nd_booster_60plus",
                                cmap='Blues',        
                                legend=True,        
                                legend_kwds={"title":"Percentage",
                                             "loc":"center left",
                                             "fmt": "{:.2f}%", 
                                             "bbox_to_anchor":(1,0.5),},
                                edgecolor='black',       
                                linewidth=0.8,          
                                k=4,                      
                                scheme='quantiles',       
                                missing_kwds={"color": "lightgrey",
                                              "label": "No data"},
                                zorder=1,
                                ax=ax)
leg = ax.get_legend()
for lh in leg.legendHandles:
           lh.set_marker('s')
           lh.set_data((11.5,3.85))
           lh.set_markersize(17)
           lh.set_markeredgecolor("None")
           lh._legmarker.set_markeredgecolor('None')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM