AttributeError: module ‘matplotlib.cm‘ has no attribute ‘register_cm‘

最近项目需要,在画热力图,需要用到matplotlib和seaborn两个包,但是在import seaborn as sns的时候一直报错AttributeError: module 'matplotlib.cm' has no attribute 'register_cm'。换了好几个不同版本的matplotlib和seaborn的搭配,还是不行,最后详细看了下seaborn包中的cm.py文件,直接说结果:

把原来的:

mpl_cm.register_cm(_name, _cmap)
mpl_cm.register_cm(_name + "_r", _cmap_r

替换成:

mpl_cm.ColormapRegistry.get_cmap(_name, _cmap)
mpl_cm.ColormapRegistry.get_cmap(_name + "_r", _cmap_r)

就可以了,原因应该是高版本的matplotlib弃用了register_cm()函数,希望对同样遇到此类问题的人有帮助。

作者:tongji_transport

物联沃分享整理
物联沃-IOTWORD物联网 » AttributeError: module ‘matplotlib.cm‘ has no attribute ‘register_cm‘

发表回复