Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the text of the legend of a choropleth map in Python?

I get one choropleth map using the following code:

%matplotlib inline

import seaborn as sns
import pandas as pd
import pysal as ps
import geopandas as gpd
import numpy as np
import matplotlib.pyplot as pet

imd_shp = 'desktop/sgfwu/E07000222_IMD/shapefiles/E07000222.shp'
imd = gpd.read_file(imd_shp)
imd = imd.set_index('LSOA11CD') 
imd.plot(column='imd_score', scheme='fisher_jenks', alpha=0.8, k=7,
         colormap=plt.cm.Blues, legend=True, axes=ax1)

The result is:

enter image description here

But how can I change the text of the legend to the words like the map below, rather than numbers?

enter image description here

like image 779
Fan Wu Avatar asked Oct 19 '22 22:10

Fan Wu


1 Answers

This question has been here for a while, but I just had the same problem. This solved it for me:

leg = ax1.get_legend()
leg.get_texts()[0].set_text('New label 1')
leg.get_texts()[1].set_text('New label 2')

and so on for as many labels as you want to change.

like image 138
Catherine Nelson Avatar answered Oct 22 '22 10:10

Catherine Nelson