1

I am plotting a choropleth map in python from a shapefile and i want to customize the legend of the plot, i am using the code bellow:

import pandas as pd
import pysal as ps
import geopandas as gp
import numpy as np
import matplotlib.pyplot as plt

pth = 'outcom.shp'
tracts = gp.GeoDataFrame.from_file(pth)
ax = plot_dataframe(tracts, column='Density', scheme='QUANTILES', k=4, colormap=plt.cm.Blues, legend=True)
plt.show()

Besides, i am using a small patch that i found here http://nbviewer.ipython.org/gist/jorisvandenbossche/d4e6efedfa1e4e91ab65 in order to visualize the legend.

here's my result : enter image description here But, i need something similar to this :

enter image description here

so my question now, is how can i have a customized legend

1 Answer 1

1

You may use a plt.table as a legend.

enter image description here

import matplotlib.pyplot as plt
import numpy as np

valeur = np.array([.1,.45,.7])
text=[["Faible","Ng<1,5" ],["Moyenne","1,5<Ng<2,5"],[u"Elevée", "Ng>2,5"]]
colLabels = ["Exposition", u"Densité"]


tab=plt.table(cellText=text, colLabels=colLabels, 
                    colWidths = [0.2,0.2], loc='lower right', 
                    cellColours=plt.cm.hot_r(np.c_[valeur,valeur]))

plt.show()

In order to link this table to a contourf plot, you may do as follows:

from matplotlib import pyplot as plt
import numpy as np

a = np.sort(np.random.rand(100)).reshape(10,10)*4

levels = np.array([0,1.5,2.5,4])
sm = plt.contourf(a, levels = levels, cmap=plt.cm.hot_r )


text=[["Faible","Ng<1,5" ],["Moyenne","1,5<Ng<2,5"],[u"Elevée", "Ng>2,5"]]
colLabels = ["Exposition", u"Densité"]

col = levels[:-1] + np.diff(levels)/2.
cellcol = sm.cmap(sm.norm(np.c_[col,col]))

tax = plt.gcf().add_axes([0,0,1,1])
tab=tax.table(cellText=text, colLabels=colLabels, 
                    colWidths = [0.2,0.2], loc='lower left', 
                    cellColours=cellcol )

tax.axis("off")
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

I did as you said , but it's like there is no link between the map and the table, i got a result showing a map with the default colors and in the bottom there is the table you coded1.
I updated the answer to make the legend use colors from a contourplot.
well I really appreciate your help, but i'm not using contourf plot , it's a choropleth map and i'm using this : ax = plot_dataframe(tracts, column='Density', scheme='QUANTILES', k=4, colormap=plt.cm.Blues, legend=True) , and the dataframe does not support the 'levels'.
I understand that, but this geopandas function you're using is a little complicated and you already asked a new question about that, so I decided to give the general solution as answer to this question.
For how to use this legend in an actual geopandas plot, see this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.