2016-03-15 2 views
5

J'essaie de créer une barre de couleurs Matplotlib sur GeoPandas.Barre de couleurs sur Geopandas

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) 

je reçois l'erreur suivante:

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' 

Je ne sais pas comment obtenir colorbar travailler.

+1

GeoDataFrame renvoie un objet 'Axes' matplotlib. 'plt.colorbar' nécessite un objet mappable (un' Axes' n'est pas un objet mappable). – tom

Répondre

10

Il y a un PR à ajouter à geoapandas (https://github.com/geopandas/geopandas/pull/172), mais pour l'instant, vous pouvez ajouter vous-même avec cette solution de contournement:

## 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) 

La solution vient de Matplotlib - add colorbar to a sequence of line plots. Et la raison pour laquelle vous devez fournir vmin et vmax vous-même est que la barre de couleurs n'est pas ajoutée en fonction des données elles-mêmes, vous devez donc indiquer le lien entre les valeurs et la couleur.