将使用 Geopandas、Pandas 和 matplotlib 构建的等值线图存储到图像文件

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

我有这个 GeoPandas 数据框,它来自 shapefile(加载了 GeoPandas)和 Pandas 数据框(包含我想要绘制的数据)。

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)

给出:

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

          mean
0  ZZ.ZZZ

根据 GeoPandas 文档(此处:http://geopandas.readthedocs.io/en/stable/mapping.htmlgpd.plot 函数文档)如果我想绘制叶绿素图,我应该这样做:

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

但这会产生以下错误:

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

我不确定要让 matplotlibGeoPandas 一起工作需要做什么?

对于代码行:

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

cmap=pltplt 不是 matplotlib 可识别的颜色。它应该是错误消息中提到的颜色,例如 cmap='RdPu' 或找到的颜色 here

如果您想创建自己的颜色图,您始终可以通过选择颜色的十六进制代码来创建自己的颜色列表:

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

然后做:

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

或:

df_for_map.plot(column='mean') 

并省略默认颜色 cmap