Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colorbar on Geopandas

I am trying to create a Matplotlib colorbar on 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)

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.

like image 329
Jack_The_Ripper Avatar asked Mar 15 '16 10:03

Jack_The_Ripper


People also ask

How do you plot Choropleth with Geopandas?

Choropleth Maps Simply use the plot command with the column argument set to the column whose values you want used to assign colors.

What is CMAP in Geopandas?

Geopandas plot takes a colormap parameter. The parameter is called cmap . There's an alias for it called colormap but it should not be used anymore. GeoDataFrame.plot(column=None, cmap=None, alpha=0.5, categorical=False, legend=False, axes=None)

What can you do with Geopandas?

GeoPandas is an open source project to make working with geospatial data in python easier. GeoPandas extends the datatypes used by pandas to allow spatial operations on geometric types. Geometric operations are performed by shapely. Geopandas further depends on fiona for file access and matplotlib for plotting.

How do I add a legend on Geopandas?

If you plot your data using the standard geopandas . plot() , geopandas will select colors for your lines. You can add a legend using the legend=True argument however notice that the legend is composed of circles representing each line type rather than a line.


1 Answers

EDIT: The PR referenced below has been merged into the geopandas master. Now you can simply do:

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 called cmap.
  • vmin and vmax are not required anymore.

See https://geopandas.readthedocs.io/en/latest/mapping.html#creating-a-legend for more (with an example how to adapt the size and placement of the colorbar).


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:

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

like image 143
joris Avatar answered Sep 16 '22 16:09

joris