Joshua Croff
Joshua Croff

Reputation: 15

Geopandas Explore - Reorder Items in Legend

I'm working on plotting some Census data on a map using the Geopandas Explore method and am running into some issues with customizing the legend. For background, I've pulled together Household tract-level income data form the Census and created a household income classification column where I'm assigning one of 5 income categories to each tract. When I plot the dataset on a map, the legend seems to default to alphanumeric order which I'd like to override.

Here is my code:

hhi_gdf.explore(column='med_hh_inc_test',
                   cmap=['#2c7fb8','#a1dab4','#41b6c4','#253494','#ffffcc'],
                   tiles="CartoDB positron",
                   style_kwds={'opacity':.40,
                               'fillOpacity':.60},
                   legend_kwds={'caption':'Median Household Income'}) 

Here is the default result: default legend order

I've tried this to override the default legend order the documentation says you can override default legend by providing a dictionary to the legend_kwds param, specifying a 'list-like' list of legend labels to the 'labels' key which seems like the only way to change the legend labels at all.:

hhi_gdf.explore(column='med_hh_inc_test',
                   cmap=['#2c7fb8','#a1dab4','#41b6c4','#253494','#ffffcc'],
                   tiles="CartoDB positron",
                   style_kwds={'opacity':.40,
                               'fillOpacity':.60},
                   legend_kwds={'colorbar':False,
                                'caption':'Median Household Income',
                                'labels':[['Less than $45,000'],
                                         ['$45,000 - $74,999'],
                                         ['$75,000 - $124,999'],
                                         ['$125,000 - $199,999'],
                                         ['Greater than $200,000']]})

Here is the resulting plot (no difference in legend order): no change in legend order

Any help with this would be greatly appreciated. This is an awesome method if only it was a little easier to customize.

Upvotes: 1

Views: 1043

Answers (1)

martinfleis
martinfleis

Reputation: 7814

This is currently not possible using the public API as the order is hard-coded in the code. But you can try using the private function that creates the legend to get the desired outcome. Just try not to rely on it in a long-term. I'll open an issue on this in GeoPandas to implement this kind of customisation directly there.

from geopandas.explore import _categorical_legend

m = hhi_gdf.explore(
    column='med_hh_inc_test',
    cmap=['#2c7fb8','#a1dab4','#41b6c4','#253494','#ffffcc'],
    tiles="CartoDB positron",
    style_kwds={'opacity':.40,'fillOpacity':.60},
    legend=False
)
_categorical_legend(
    m, 
    title="Median Household Income", 
    categories=[
        "Less than $45,000",
        "$45,000 - $74,999",
        "$75,000 - $124,999",
        "$125,000 - $199,999",
        "Greater than $200,000"],
    colors=['#2c7fb8','#a1dab4','#41b6c4','#253494','#ffffcc']
)
m

You may need to do some shuffling with cmap to ensure it is all properly mapped.

Upvotes: 2

Related Questions