Search code examples
pythonmatplotlibgisgeopandascolorbar

How can colour bar scales be shared amongst subplots?


I have the following code:

f, ax = plt.subplots(1,2, figsize=(20,20))
divider = make_axes_locatable(ax[0])
cax = divider.append_axes("right", size="5%", pad=0.1)
#level_geo.boundary.plot(ax=ax[0], color='black')
level_geo.plot(ax=ax[0], cmap='viridis', column='dem_2001', legend=True, cax=cax)

divider = make_axes_locatable(ax[1])
cax = divider.append_axes("right", size="5%", pad=0.1)
#level_geo.boundary.plot(ax=ax[1], color='black')
level_geo.plot(ax=ax[1], cmap='viridis', column='dem_2030', legend=True, cax=cax)

plt.show()

enter image description here

I am wondering how I share the colour bars so that there is just one bar with a scale relevant to both plots?


Solution

  • If you want just a linear color map, you can just pass vmin and vmax to both plots.

    f, ax = plt.subplots(1,2, figsize=(20,20))
    divider = make_axes_locatable(ax[0])
    cax = divider.append_axes("right", size="5%", pad=0.1)
    #level_geo.boundary.plot(ax=ax[0], color='black')
    level_geo.plot(ax=ax[0], cmap='viridis', column='dem_2001', legend=True, cax=cax, vmin=0, vmax=1)
    
    divider = make_axes_locatable(ax[1])
    cax = divider.append_axes("right", size="5%", pad=0.1)
    #level_geo.boundary.plot(ax=ax[1], color='black')
    level_geo.plot(ax=ax[1], cmap='viridis', column='dem_2030', legend=True, cax=cax, vmin=0, vmax=1)
    
    plt.show()
    

    Or, better, use min and max retrieved from the data (e.g. level_geo['dem_2030'].min())