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?

6
  • Is this all the code?
    – Fezter
    Commented May 24, 2017 at 19:33
  • @Fezter that's all the code for drawing the map. There's more code for building the geodataframe but that didn't seem relevant here. Is there anything crucial missing here? Thanks. Commented May 24, 2017 at 19:44
  • What is the field type of Pop_2005? Is it float or int?
    – Fezter
    Commented May 24, 2017 at 19:55
  • 3
    Could it be that you can only set a single marker size, and it is expecting a single float value and not a series/list/tuple or floats? Commented Jul 10, 2017 at 16:38
  • 1
    Would it be worth casting Pop_2005 explicitly to a float or integer? geo_df['Pop_2005'] = geo_df['Pop_2005'].astype(float)
    – om_henners
    Commented Feb 10, 2018 at 10:57

1 Answer 1

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)
1
  • Is there a way to have the legend represent the marker sizes? Passing legend=True is ignored.
    – amball
    Commented Apr 16, 2022 at 1:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.