进口
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,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 lineAttributeError: UnkNown property shadow # this error
为什么这种方法不起作用?
解决方法 您正试图将图例kwargs传递给绘图函数.需要单独调用.legend().import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0,501)y = np.sin(x)legend_dict = dict(ncol=1,shadow=True)label = 'xy data sample'#label = Noneplt.plot(x,label=label) plt.legend(**legend_dict)plt.show()
注意也不需要if语句 – 标签为None是好的,因为这是默认值!
总结以上是内存溢出为你收集整理的如何将字典kwargs输入matplotlib传奇例程?全部内容,希望文章能够帮你解决如何将字典kwargs输入matplotlib传奇例程?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)