7

I'm trying to make a choropleth map from polygons in a Geopandas GeoDataFrame. I want to symbolize the polygons by quantiles of a value in one of the GeoDataFrame columns. I'm trying to figure out different options and see what best fits my needs. Any advice on this would be greatly appreciated.

It appears that Geopandas does have some ability to do this already: http://nbviewer.ipython.org/github/geopandas/geopandas/blob/master/examples/choropleths.ipynb

tracts.plot(column='CRIME', scheme='QUANTILES', k=3, colormap='OrRd')

This works, although I can't find much documentation. I'd like to be able to add a legend that shows the quantile cut-off values, but it appears that the Geopandas plot currently only allows for legends based of categorical data. Does anyone have a work around for this?

Additionally, I'd like to be able to adjust the polygon outline width. Is this possible?

As an alternative option I've been playing around with is using polygon patches in matplotlib. This appears much more involved but does seem to offer more options to customize. If necessary to go down this route in order to build a legend I can follow-up with another question and will include my code so far.

Thanks for the help.

AJG519
  • 3,249
  • 10
  • 36
  • 62
  • The `tracts.plot(..)` returns an Axes object, from which you can start to adapt the figure using matplotlib. – joris Jul 31 '15 at 23:23
  • @joris yes I see that. I've used that to add a title, remove axis labels, etc. However, I'm not that familiar with matplotlib and am not sure how I can add a legend or change the thickness of the polygon outlines. Thanks – AJG519 Jul 31 '15 at 23:35
  • For adding a colorbar as legend, you need the colormap. And I was looking at the source code, and this colormap only seems accessible from within the `plot_dataframe` function. So maybe it would be easier for now to patch `plot_dataframe` a bit to suit your needs. – joris Aug 01 '15 at 00:15
  • @joris, thanks for the response. So to clarify, you're suggesting that I edit the code for the plot_dataframe function (found here: https://github.com/geopandas/geopandas/blob/master/geopandas/plotting.py) to add a color bar legend as well as gain the ability to adjust the polygon outline width? – AJG519 Aug 03 '15 at 19:13
  • I have an initial version, will put it on a gist tomorrow if you're interested – joris Aug 03 '15 at 22:30
  • @joris, definitely interested. Thanks for the help. – AJG519 Aug 03 '15 at 22:41
  • Added an answer for the legend. Is this what you had in mind? And can you specify what you mean with the 'outline' of the polygon. Is this the linewidth of the edge? I also added the ability to change that, see the notebook. – joris Aug 04 '15 at 09:38
  • Yes, this is exactly what I had in mind. This looks great, thanks for the help! – AJG519 Aug 04 '15 at 16:46

2 Answers2

20

The below patch is integrated in geopandas, so you can do now just:

tracts.plot(column='CRIME', scheme='QUANTILES', k=3, colormap='OrRd', legend=True)

I made a small patch to the plot_dataframe function of geopandas to enable a legend when using a PySAL scheme. You can find it here: http://nbviewer.ipython.org/gist/jorisvandenbossche/d4e6efedfa1e4e91ab65 (the adjustment is only in the few lines after if scheme is not None:).

This lets you do the following:

ax = plot_dataframe(tracts, column='CRIME', scheme='QUANTILES', k=3, colormap='OrRd', legend=True)

to get such a figure:

enter image description here

joris
  • 133,120
  • 36
  • 247
  • 202
  • This is fantastic - such an improvement. Has there been any talk of getting something based off of this code into Geopandas directly? – Owen Dec 16 '16 at 01:43
  • 1
    Yes, this is merged in geopandas in the meantime: https://github.com/geopandas/geopandas/pull/210 (I'll update my answer) – joris Dec 16 '16 at 14:11
0

To adjust the polygon outline width, you can use "linewidth" like so:

tracts.plot(column='CRIME', scheme='QUANTILES', k=3, colormap='OrRd', linewidth=0.1)
Dan
  • 27
  • 5