9

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?

  • Is this all the code? – Fezter May 24 '17 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. – Juan Francisco Saldarriaga May 24 '17 at 19:44
  • What is the field type of Pop_2005? Is it float or int? – Fezter May 24 '17 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? – derNincompoop Jul 10 '17 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 Feb 10 '18 at 10:57
6

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)
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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