简体   繁体   English

将用Geopandas,Pandas和matplotlib构建的chloropleth地图存储到图像文件

[英]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). 我有这个GeoPandas数据框,它来自shapefile (加载了GeoPandas)和Pandas数据框(包含我要绘制的数据)之间的merge

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: 根据GeoPandas文档(此处为: http : gpd.plotgpd.plot函数文档),如果我想绘制氯gpd.plot图,我应该这样做:

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 ? 我不确定GeoPandas matplotlibGeoPandas一起工作需要做什么?

For the line of code: 对于代码行:

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

cmap=plt , the plt is not a recognized color by matplotlib. cmap=plt ,matplotlib无法识别plt颜色。 It should be a color mentioned in the error message such as cmap='RdPu' or a color found here . 它应该是错误消息中提到的颜色,例如cmap='RdPu'或在此处找到的颜色。

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. 并省略cmap作为默认颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM