18

I have a geodataframe with points and some associated data. I want to plot it on a map using geopandas and have the size of the points correspond to one of the columns in the geodataframe.

So far I have the following code:

base = world.plot(color='white', figsize=(20,10))
geo_df.plot(ax=base, marker='.', color='red', markersize = 
geo_df['Pop_2005'])
plt.xlim([-85, -60])
plt.ylim([-5, 12.5]);

But I'm getting the following error: TypeError: cannot convert the series to <class 'float'>

Any ideas?

Fezter
  • 21,867
  • 11
  • 68
  • 123

1 Answers1

19

In geopandas >= 0.3 (released September 2017), the plotting of points is based on the scatter plot method of matplotlib under the hood, and this accepts a variable markersize.

So now you can actually pass a column to markersize, what the OP did in the original question:

import geopandas

cities = geopandas.read_file(geopandas.datasets.get_path('naturalearth_cities'))
# adding a column with random values for the size
cities['values'] = np.abs(np.random.randn(len(cities))) * 50

cities.plot(markersize=cities['values'])

gives:

enter image description here

Of course, if your goal is simply to change the markersize to a different constant value, you can still pass a single float to the keyword:

cities.plot(markersize=10)
joris
  • 3,903
  • 22
  • 28
  • Is there a way to have the legend represent the marker sizes? Passing legend=True is ignored. – amball Apr 16 '22 at 01:20