Here is a sample of the data I am trying to visualize
Prince Edward Island 2.333
Manitoba 2.529
Alberta 2.6444
British Columbia 2.7902
Saskatchewan 2.9205
Ontario 3.465
New Brunswick 3.63175
Newfoundland and Labrador 3.647
Nova Scotia 4.25333333333
Quebec 4.82614285714
Nunavut NaN
Yukon NaN
Northwest Territories NaN
I want to visualize the data by colouring each province according to the number it is associated with. When I do this, the Nan's are coloured like the minimum value of the colormap. Is there an easy way to map Nan to white?
Here is my code:
plt.figure(figsize=(15,15))
vmin, vmax = canada.Partying.min(), canada.Partying.max()
ax = canada.plot(column='Partying', cmap='viridis', vmin=vmin, vmax=vmax)
# add colorbar
fig = ax.get_figure()
cax = fig.add_axes([0.9, 0.1, 0.03, 0.8])
sm = plt.cm.ScalarMappable(cmap='viridis', norm=plt.Normalize(vmin=vmin, vmax=vmax))
# fake up the array of the scalar mappable. Urgh...
sm._A = []
fig.colorbar(sm, cax=cax)
plt.savefig('Canada.pdf')
Update: New feature in geopandas
solves your problem: You can now use:
ax = canada.plot(column='Partying', cmap='viridis', vmin=vmin, vmax=vmax,
missing_kwds= dict(color = "lightgrey",) )
To make all missing data regions light grey.
See https://geopandas.readthedocs.io/en/latest/mapping.html
(actually, the documentation may say that the parameter is missing_kwdsdict
, but the above is what works for me)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With