pandas Geopandas 上的颜色条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36008648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Colorbar on Geopandas
提问by Hyman_The_Ripper
I am trying to create a Matplotlib colorbar on GeoPandas.
我正在尝试在 GeoPandas 上创建一个 Matplotlib 颜色条。
import geopandas as gp
import pandas as pd
import matplotlib.pyplot as plt
#Import csv data
df = df.from_csv('data.csv')
#Convert Pandas DataFrame to GeoPandas DataFrame
g_df = g.GeoDataFrame(df)
#Plot
plt.figure(figsize=(15,15))
g_plot = g_df.plot(column='column_name',colormap='hot',alpha=0.08)
plt.colorbar(g_plot)
I get the following error:
我收到以下错误:
AttributeError Traceback (most recent call last)
<ipython-input-55-5f33ecf73ac9> in <module>()
2 plt.figure(figsize=(15,15))
3 g_plot = g_df.plot(column = 'column_name', colormap='hot', alpha=0.08)
----> 4 plt.colorbar(g_plot)
...
AttributeError: 'AxesSubplot' object has no attribute 'autoscale_None'
I am not sure how to get colorbar to work.
我不知道如何让颜色条工作。
回答by joris
EDIT:The PR referenced below has been merged into the geopandas master. Now you can simply do:
编辑:下面引用的 PR 已合并到 geopandas 母版中。现在你可以简单地做:
gdf.plot(column='val', cmap='hot', legend=True)
and the colorbar will be added automatically.
并且颜色条将自动添加。
Notes:
笔记:
legend=True
tells Geopandas to add the colorbar.colormap
is now calledcmap
.vmin
andvmax
are not required anymore.
legend=True
告诉 Geopandas 添加颜色条。colormap
现在称为cmap
.vmin
而vmax
不是不再需要。
See https://geopandas.readthedocs.io/en/latest/mapping.html#creating-a-legendfor more (with an example how to adapt the size and placement of the colorbar).
有关更多信息,请参阅https://geopandas.readthedocs.io/en/latest/mapping.html#creating-a-legend(举例说明如何调整颜色条的大小和位置)。
There is a PR to add this to geoapandas (https://github.com/geopandas/geopandas/pull/172), but for now, you can add it yourself with this workaround:
有一个 PR 可以将此添加到 geoapandas ( https://github.com/geopandas/geopandas/pull/172),但现在,您可以使用以下解决方法自行添加:
## make up some random data
df = pd.DataFrame(np.random.randn(20,3), columns=['x', 'y', 'val'])
df['geometry'] = df.apply(lambda row: shapely.geometry.Point(row.x, row.y), axis=1)
gdf = gpd.GeoDataFrame(df)
## the plotting
vmin, vmax = -1, 1
ax = gdf.plot(column='val', colormap='hot', vmin=vmin, vmax=vmax)
# add colorbar
fig = ax.get_figure()
cax = fig.add_axes([0.9, 0.1, 0.03, 0.8])
sm = plt.cm.ScalarMappable(cmap='hot', norm=plt.Normalize(vmin=vmin, vmax=vmax))
# fake up the array of the scalar mappable. Urgh...
sm._A = []
fig.colorbar(sm, cax=cax)
The workaround comes from Matplotlib - add colorbar to a sequence of line plots. And the reason that you have to supply vmin
and vmax
yourself is because the colorbar is not added based on the data itself, therefore you have to instruct what the link between values and color should be.
解决方法来自Matplotlib - 将颜色条添加到一系列线图。您必须提供vmin
和您vmax
自己的原因是因为颜色条不是根据数据本身添加的,因此您必须指示值和颜色之间的联系应该是什么。