TPPZ TPPZ - 1 month ago 14
Python Question

Storing to an image file a chloropleth map built with Geopandas, Pandas and matplotlib

I have this GeoPandas dataframe that comes from a

merge
between a
shapefile
(loaded with GeoPandas) and a Pandas dataframe (containing the data I would like to plot).

import matplotlib.pyplot as plt
import geopandas as gpd
import pandas as pd

[...]

df_for_map = geopandas_shp_df.merge(pandas_data_df, on='neighborhood')
print df_for_map.head(1)


gives:

geometry id neighborhood \
0 POLYGON ((XX.XXX YY.YYY, ... 1 My_Neighborhood_Key_Representing_A_Polygon

mean
0 ZZ.ZZZ


According to the GeoPandas documentation (here: http://geopandas.readthedocs.io/en/stable/mapping.html and the
gpd.plot
function docs) if I want to plot a chloropleth map I should do:

df_for_map.plot(column='mean', cmap=plt)
plt.savefig('figure_file_path')


But this gives the following error:

Traceback (most recent call last):
File "./MY_PYTHON_SCRIPT.py", line XXX, in <module>
df_for_map.plot(column='mean', cmap=plt)
File "/usr/local/lib/python2.7/site-packages/geopandas/geodataframe.py", line 447, in plot
return plot_dataframe(self, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/geopandas/plotting.py", line 261, in plot_dataframe
cmap = norm_cmap(values, cmap, Normalize, cm, vmin=vmin, vmax=vmax)
File "/usr/local/lib/python2.7/site-packages/geopandas/plotting.py", line 372, in norm_cmap
n_cmap = cm.ScalarMappable(norm=norm, cmap=cmap)
File "/usr/local/lib/python2.7/site-packages/matplotlib/cm.py", line 201, in __init__
self.cmap = get_cmap(cmap)
File "/usr/local/lib/python2.7/site-packages/matplotlib/cm.py", line 166, in get_cmap
% (name, ', '.join(sorted(cmap_d.keys()))))
ValueError: Colormap <module 'matplotlib.pyplot' from '/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.pyc'> is not recognized. Possible values are: Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, inferno, inferno_r, jet, jet_r, magma, magma_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, seismic, seismic_r, spectral, spectral_r, spring, spring_r, summer, summer_r, terrain, terrain_r, viridis, viridis_r, winter, winter_r


I am not sure what needs to be done to get
matplotlib
working with
GeoPandas
?

Answer

For the line of code:

df_for_map.plot(column='mean', cmap=plt)

cmap=plt, the plt is not a recognized color by matplotlib. It should be a color mentioned in the error message such as cmap='RdPu' or a color found here.

If you want to create your own colormap you can always create your own color list by choosing the hex code of colors as such:

my_colors = ['#000000', '#0080ff', '#2a3050', '#f0ff00', '#00ff7c', '#d100ce',
             '#dd3333', '#00ce18', '#7f9690', '#df85ff', '#96b813', '#2533fb',
             '#66d4ff', '#fd7f00', '#9f5000', '#ff0000', '#4d0c57', '#900303',
             '#00780e', '#41fd30', '#dae700']

and then do:

df_for_map.plot(column='mean', color=my_colors)

or:

df_for_map.plot(column='mean') 

and leave out cmap for the default color.