0

Having trouble selecting 'male' and 'female' for hue when creating a cartogram with geoplot (using geopandas). I have only managed to select the total population but I would like to compare the state populations of male to female. I understand that the 'hue' assignment is probably what I need to modify but so far I can only display the total population (Tot_P_P) rather than both "Tot_P_M" and "Tot_P_F".

This is the code so far that works for total population. I've tried for the past 2 weeks looking through tutorials and websites but there isn't much info on cartograms.

working code for total population:

ax = gplt.polyplot(gda2020, 
                   projection=gplt.crs.AlbersEqualArea(), 
                   figsize = (25, 15)
                  )
gplt.cartogram(gda2020, 
                  scale="Tot_P_P", limits=(0.2, 1), scale_func=None, 
                  hue="Tot_P_P", 
                  cmap='inferno', 
                  norm=None, 
                  scheme=None, 
                  legend=True, 
                  legend_values=None, 
                  legend_labels=None, 
                  legend_kwargs=None, 
                  legend_var='hue', 
                  extent=None, 
                  ax=ax)
ndorosey
  • 1
  • 2
  • a bit hard to debug without a [mre], but usually the hue argument is a categorical column. so I think the way to do this would be something along the lines of `df_with_hue = pd.concat([df.rename(columns={"Tot_P_M": "pop"}).assign(gender="M"), df.rename(columns={"Tot_P_F": "pop"}).assign(gender="F")])` and then pass `hue="gender", scale="pop"`. this won't work exactly - you'll need to align them before concatenating, but I think you get the idea - they should be stacked rather than columns. – Michael Delgado Nov 01 '22 at 22:54

0 Answers0