Search code examples
pythonpandasmatplotlibgraphicsgeopandas

Set same scale in legend matplotlib


I'm working with geospacial data and I have two pandas dataframes with two different regions, both have a column geometry with the (multi)polygons and a column SCORE with a value for each of the (multi)polygons I want to plot.

For example, this is the plot for the provinces of Spain excluding Canary Islands:

shapefile_prov.plot(column="SCORE", legend=True)
plt.xticks([])
plt.yticks([])

enter image description here

And this is the plot of the dataframe with Canary Island:

shapefile_prov_can.plot(column="SCORE", legend=True)
plt.xticks([])
plt.yticks([])

enter image description here

Is there a way to colour the graph for Canary Islands with the same scale of colours of the first graph? What I mean is that yellow colour should be associated to values of SCORE roughly above 0.5 and purple for values below 0.325 approximately.


Solution

  • A quick/simple option would be to agg the min/max of scores and pass it to the plot :

    cmap_range = shapefile_prov["SCORE"].agg({"vmin": "min", "vmax": "max"})
    
    shapefile_prov_can.plot(column="SCORE", legend=True, **cmap_range)
    

    NB: The plot below uses an AxesDivider to improve the appearance of the colorbar.

    enter image description here