我有这个GeoPandas数据框,它来自merge
(已加载GeoPandas)和Pandas数据框(包含我要绘制的数据)之间的shapefile
。
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.html和
gpd.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
我不确定要使
matplotlib
与GeoPandas
一起工作需要做什么? 最佳答案
对于代码行:
df_for_map.plot(column='mean', cmap=plt)
cmap=plt
,是matplotlib无法识别的颜色。它应该是错误消息中提到的颜色,例如plt
或找到的颜色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='RdPu'
作为默认颜色。关于python - 将用Geopandas,Pandas和matplotlib构建的Choropleth map 存储到图像文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40388400/