5

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

Fan Wu
  • 159
  • 1
  • 2
  • 6
  • Which library are you using for this? – 9000 Nov 17 '15 at 17:11
  • I'm a rookie programmer so I'm not sure about what actually it is >.< All libraries I 'm using are seaborn, pandas, pysal, geopandas, bumpy and matplotlib.pyplot. – Fan Wu Nov 17 '15 at 17:22
  • Should I provide more codes? – Fan Wu Nov 17 '15 at 17:44
  • Yes, a slightly longer fragment that actually produces the chart would be nice. – 9000 Nov 17 '15 at 17:49
  • Thank you very much. I have added them in the question. Here is a related course: http://darribas.org/gds15/content/labs/lab_04.html – Fan Wu Nov 17 '15 at 18:06
  • I suspect you could find something useful in [matplotlib legend guide](http://matplotlib.org/users/legend_guide.html), but I don't have a complete answer. See how label names can be given. – 9000 Nov 17 '15 at 19:34
  • Hmmm. I might be wrong, but it looks like you're skipping the code that has the plot creation. Where is i`md_plot` defined? I suspect the answer to that will help find the docs needed to solve your problem. – Juan Carlos Coto Nov 17 '15 at 22:44
  • Thanks for 9000's help. I was reading the guide but still has no correct idea >3<. – Fan Wu Nov 18 '15 at 03:36
  • Oh, it should be imd.plot rather than imd_plot. I write it wrong and change it now. – Fan Wu Nov 18 '15 at 03:37
  • @FanWu: your example is still not reproducible (where do you define `ax1`?), so try to give a minimum reproducible one and indicate the version of geopandas you are using. What does `ax1.get_legend_handles_labels()` give you? Setting `ax1.legend(handles, labels)` by keeping the handles and just changing the labels might do the trick. – cd98 Nov 28 '15 at 21:04

1 Answers1

5

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.