Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Custom Choropleth Labels in Geopandas

import geopandas as gp
import matplotlib.pyplot as plt

def update_custom_legend_labels(ax, new_labels):
    current_legend = ax.get_legend()
    for ix_current_label, txt_current_label in enumerate(current_legend.texts):    
        for ix_new_label, txt_new_label in new_labels.items():        
            if ix_current_label == ix_new_label:
                # print(f'current: {ix_current_label, txt_current_label}')
                # print(f'new: {ix_new_label, txt_new_label}')
                txt_current_label.set_text(txt_new_label)
                # print(f'update: {txt_current_label}')

world = gp.read_file(gp.datasets.get_path('naturalearth_lowres'))
fig, ax = plt.subplots()
world.plot(ax=ax,
           column='pop_est',
           cmap='OrRd',
           scheme='quantiles',
           legend=True,
           legend_kwds=dict(loc='upper right',
                            bbox_to_anchor=(1.5, .9),
                            fontsize='small',
                            title="Legend",
                            frameon=False, fmt="{:.0f}")
           )

current_labels = [t.get_text() for t in ax.get_legend().get_texts()]

no_labels = len(current_labels)
new_labels = []
for ix, val in enumerate(current_labels):    
    if ix == 0:  # first item
        val = '<' + val.split(',')[0]
    elif no_labels == ix + 1:  # last item
        val = '>' + val.split(',')[0]        
    else:  # remaining items
        val = val.replace(',', ' -')            
    new_labels.append((ix,val))
new_labels = dict(new_labels)    

update_custom_legend_labels(ax, new_labels)
ax.set_axis_off()
plt.show()
Comment

PREVIOUS NEXT
Code Example
Python :: bson to dataframe pandas 
Python :: python urllib.request.urlretrieve with a progressbar 
Python :: vidgear python video streaming 
Python :: !value in python 
Python :: Run flask on docker with postgres and guinicorn 
Python :: apk calculate python 
Python :: space separated dictionary input in python 
Python :: # find the n smallest and greatest numbers in list 
Python :: pandas from multiindex to single index 
Python :: matplotlib get colorwheel 
Python :: merge more than two dataframes based on column 
Python :: colorutils python 
Python :: nbt python 
Python :: range function without end value 
Python :: how to convert matlab code into python 
Python :: Simple Python Permutation Without Passing any argument 
Python :: droping columns 
Python :: merge sort dictionary python 
Python :: your momma in python 
Python :: python create empty list with size 10 
Python :: cartopy indicate lat lon 
Python :: docker python run subprocess python run docker interactively subprocess 
Python :: jupyter extension 4 
Python :: Python3: Deleting even and only showing uneven numbers from, set list. 
Python :: Python NumPy column_stack Function Example with 2d array 
Python :: python os.listdir attributes 
Python :: Python __ne__ magic method 
Python :: godot knockback 
Python :: django view - apiview decorator (urls.py config) 
Python :: mock connection sqlalchemy 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =