如何消除Shell运行时显示的警告MatplotlibDeprecationWarning?

新手上路,请多包涵

为了方便大家观察只截取了关键部分的代码块。

程序运行时没有错误,只是每次脚本运行到这个函数的时候

from bisect import bisect_right
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib as mpl
import random
import time
def show(G, List, city):       
    startcolor = '#00ffff'   #蓝绿色
    midcolor = '#ffff00'     #绿色
    endcolor = '#ff0000'    #红色
    cmap2 = **mpl**.colors.LinearSegmentedColormap.from_list('own2',[startcolor,midcolor,endcolor])   # extra arguments are N=256, gamma=1.0
    **mpl**.cm.register_cmap(cmap=cmap2)
         
    edges,p = zip(*nx.get_edge_attributes(G,'people').items())     #zip(*)拆开tuple提取属性
    nx.draw_networkx_nodes(G,G.pos,node_size=25, node_color = 'g')
    nx.draw_networkx_edges(G, G.pos, edge_color=p, width=2,
                                       alpha=0.8,edge_cmap=**mpl**.cm.get_cmap('own2'))

    print(nx.get_edge_attributes(G,'people'))   #客流量
        
    vmin = min(p)                               #颜色映射范围
    vmax = max(p)



    sm = plt.cm.ScalarMappable(cmap=**mpl**.cm.get_cmap('own2'), norm=plt.Normalize(vmin=vmin, vmax=vmax))        #显示 colorbar
    sm.set_array([])
    plt.colorbar(sm)    
    plt.style.use('dark_background')
    plt.axis('off')
    plt.show()  

会显示如下警告:

MatplotlibDeprecationWarning: 
The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.
  if not cb.iterable(width):
MatplotlibDeprecationWarning: 
The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.
  and cb.iterable(edge_color) \
MatplotlibDeprecationWarning: 
The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.
  for c in edge_color]):

警告里要我用np.iterable但是这似乎是numpy的函数,我检查了每一个使用过mpl模块的地方,不明白哪里是可迭代函数,而且没有标注行数以至于我不知道要改动哪一个地方。

因为这是我毕业设计里出现的情况,而Shell运行时出现这一大段警告影响美观,请问各位大佬应该如何修改程序,或者更改配置不显示警告呢?

第一次在思否提问,如果有不规范的地方望见谅!
图片描述

阅读 18.2k
2 个回答

可以指定忽略warning类型,也可以忽略所有的。

import warnings
import matplotlib.cbook
warnings.filterwarnings("ignore",category=matplotlib.cbook.mplDeprecation)

请将你的
clipboard.png
文件中对应位置的 cb.iterable 改为 np.iterable 再试

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题