I have this code here for plotting 2 variables with different values

  1. Min:0 Max:33
  2. Min:0 Max:13
 import geopandas as gpd
 import pandas as pd
 import matplotlib.pyplot as plt

 pm10_base = gpd.read_file('~/Desktop/wrfpy/benmap/BenMAP/Shapefiles/pm10/baseline.shp')
 pm10_euro = gpd.read_file('~/Desktop/wrfpy/benmap/BenMAP/Shapefiles/pm10/euro4/Control.shp')

 # set the value column that will be visualised
 variable = 'D24HourMean'
 # set the range for the choropleth values
 vmin, vmax = 0, 35
 # create figure and axes for Matplotlib
 fig, ax = plt.subplots(1, figsize=(8, 10))
 # remove the axis
 # add a title and annotation
 ax.set_title('PM\u2081\u2080 (Baseline)', fontdict={'fontsize': '20', 'fontweight' : '4'}, y=1.08)
 # Create colorbar legend
 sm = plt.cm.ScalarMappable(cmap='autumn_r', norm=plt.Normalize(vmin=vmin, vmax=vmax))
 # empty array for the data range
 # add the colorbar to the figure
 a = fig.colorbar(sm)

 # create map
 pm10_base.plot(column=variable, cmap='autumn_r', linewidth=0.8, ax=ax, edgecolor='0.8')

 # Add Labels
 pm10_base['coords'] = pm10_base['geometry'].apply(lambda x: x.representative_point().coords[:])
 pm10_base['coords'] = [coords[0] for coords in pm10_base['coords']]
 for idx, row in pm10_base.iterrows():
     plt.annotate(s=row['LGU'], xy=row['coords'],horizontalalignment='center')

 plt.savefig('/Users/jacob/Desktop/wrfpy/benmap/test.png', bbox_inches = 'tight', pad_inches = 0)

Now, I have set the vmin and vmax to 0 to 35, which is exactly the minimum and maximum value for my first data pm10_base['D24HourMean'].

Now, I use the same code with a different variable to plot the second one

I did not change vmin and vmax, to plot another set of data that has a different (LOWER) minimum and maximum.

I want the same scale to represent the next data. however, it seems like it does not follow as seen in the plots below. The result should be that the second plot has the same scale, but has different colors based on the scale set.

Any tips?

First plot

Second plot

1 Answer 1


You need to pass the vmin and vmax to the geopandas .plot() call:

# create map
pm10_base.plot(column=variable, cmap='autumn_r', vmin=vmin, vmax=vmax, linewidth=0.8, ax=ax, edgecolor='0.8')

Note that you can also get such a colorbar by passing legend=True to the plot() call. That should do more or less the same as your code with ScalarMappable and fig.colorbar.

  • Thanks! It works!
    – meteo_96
    Dec 4, 2019 at 12:55

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.