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

跟風遠走 提交于 2019-12-07 07:55:15

问题


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:

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


回答1:


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.



来源:https://stackoverflow.com/questions/33762891/how-to-change-the-text-of-the-legend-of-a-choropleth-map-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!