如何为剧情中的"Nan"设置特殊颜色? [英] How can I set a special colour for `Nan`s in my plot?

查看:106
本文介绍了如何为剧情中的"Nan"设置特殊颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要可视化的数据示例

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

我想通过根据与每个省相关的数字为每个省着色来形象化数据.当我这样做时,Nan的颜色就像色图的最小值一样.有没有简单的方法可以将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?

这是我的代码:

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')

推荐答案

更新:geopandas中的新功能解决了您的问题:您现在可以使用:

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.

请参见 https://geopandas.readthedocs.io/en/latest/mapping. html (实际上,文档可能会说该参数为missing_kwdsdict,但是上面的内容对我有用)

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)

这篇关于如何为剧情中的"Nan"设置特殊颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆