我有一张我想画的纸条:
my_dict={'a': 0.015015015015015015,
'b': 2.0,
'c': 0.0,
'd-e': 0.14,
'f': 0.0
nan: 0.06
}
此代码给出了一个错误
import matplotlib.pylab as plt
lists = sorted(my_dict.items()) # sorted by key, return a list of tuples
x, y = zip(*lists) # unpack a list of pairs into two tuples
plt.plot(x, y)
plt.show()
-> TypeError: '<' not supported between instances of 'float' and 'str'
而另一个
plt.bar(list(my_dict.keys()), list(my_dict.values()))
返回错误
-> TypeError: 'value' must be an instance of str or bytes, not a float
我该怎么画呢
问题在于
plot
期望数字能够绘制。所以你可以这么做得到
如果字典中有一个
np.nan
作为键,您可以随时用另一个最适合您的键替换它:得到
相关问题 更多 >
编程相关推荐