如何将字典kwargs输入matplotlib传奇例程?
发布时间:2020-12-20 11:02:57 所属栏目:Python 来源:网络整理
导读:我想在输出绘图之前编写一个接受图例参数字典的函数.我在下面列举了一个小例子. 进口 import numpy as npimport matplotlib.pyplot as plt 数据 x = np.linspace(0,100,501)y = np.sin(x) 图例参数 legend_dict = dict(ncol=1,loc='best',fancybox=True,shad
我想在输出绘图之前编写一个接受图例参数字典的函数.我在下面列举了一个小例子.
进口 import numpy as np import matplotlib.pyplot as plt 数据 x = np.linspace(0,100,501) y = np.sin(x) 图例参数 legend_dict = dict(ncol=1,loc='best',fancybox=True,shadow=True) label = 'xy data sample' # label = None 情节 if label is not None: plt.plot(x,y,label=label,**legend_dict) else: plt.plot(x,y) plt.show() 这给了我以下错误(可以通过取消注释label = None来避免). plt.plot(x,**legend_dict) # this line AttributeError: Unknown property shadow # this error 为什么这种方法不起作用? 解决方法
您正试图将图例kwargs传递给绘图函数.需要单独调用.legend().
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0,501) y = np.sin(x) legend_dict = dict(ncol=1,shadow=True) label = 'xy data sample' #label = None plt.plot(x,label=label) plt.legend(**legend_dict) plt.show() 注意也不需要if语句 – 标签为None是好的,因为这是默认值! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |