Search code examples
pythonmatplotlibgeopandascolorbar

Geopandas plot makes label for every datapoint instead of a colorbar


This is my simple code:

gdf_MA_outage.plot(column = 'total_customers_affected_by_city' , cmap = 'OrRd' , figsize = (16,16) , legend = True)

'total_customers_affected_by_city' ranges from 1 to 200000. Instead of making a colorbar, it makes a label for every row in that column. Any help is appreciated.

I can't post image yet so I can only post a link of the image


Solution

  • total_customers_affected_by_city must be a string so it is treated as a categorical. Change it to a numeric column and you will get a color bar. Code below shows what you describe where I have deliberately set column to be a string rather than numeric.

    import geopandas as gpd
    import numpy as np
    
    gdf_MA_outage = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres")).loc[
        lambda d: (d["continent"] == "Europe") & (~d["iso_a3"].isin(["-99", "RUS"]))
    ]
    
    gdf_MA_outage["total_customers_affected_by_city"] = np.random.randint(1, 200000, len(gdf_MA_outage)).astype(str)
    
    gdf_MA_outage.plot(
        column="total_customers_affected_by_city",
        cmap="OrRd",
        figsize=(16, 16),
        legend=True,
    )