简体   繁体   English

当我尝试 plot dict 时出现 TypeErrors

[英]TypeErrors when I try to plot dict

I have a dict that I want to plot:我有一个我想要 plot 的字典:

my_dict={'a': 0.015015015015015015,
 'b': 2.0,
 'c': 0.0,
 'd-e': 0.14,
 'f': 0.0
 nan: 0.06
}

This code is giving an error此代码给出错误

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' -> TypeError: 'float' 和 'str' 的实例之间不支持'<'

while this other而这另一个

plt.bar(list(my_dict.keys()), list(my_dict.values()))

returns the error返回错误

-> TypeError: 'value' must be an instance of str or bytes, not a float -> TypeError: 'value' 必须是 str 或 bytes 的实例,而不是浮点数

How can I plot it?我怎么能plot呢?

The problem lies in the fact that plot expects numbers to plot.问题在于plot期望数字为 plot。 So you can just do所以你可以做

x, y = zip(*lists) # unpack a list of pairs into two tuples

x_num = np.arange(len(x)) # Numbers to plot

plt.plot(x_num, y)
plt.xticks(x_num, labels=x) # Replace x_num with your labels in x
plt.show()

And get并得到

阴谋

If there is a np.nan as a key in your dictionary, you can always replace it with another key that suits you best:如果你的字典中有一个np.nan作为键,你总是可以用另一个最适合你的键来替换它:

my_dict['not nan'] = my_dict.pop(np.nan) # Replace

# Plot
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.bar(list(my_dict.keys()), list(my_dict.values()))

And get并得到

条形图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 KeyError当我想尝试绘图时 - KeyError When I want try to plot 当我尝试在dict中追加值时,显示关键错误 - when i try to append value in dict its showing key error 当我尝试绘制情节时,spyder 的内核死了 - spyder's kernel dies when I try to make a plot 当我尝试导入存储孩子的字典时,我收到 ValueError: Duplicated timeseries in CollectorRegistry 错误 - I get the ValueError: Duplicated timeseries in CollectorRegistry error, when i try to import the Dict where the childs are stored 在进行向量化计算时如何处理typeError? - How to handle typeErrors when doing vectorize calculations? 为什么当我尝试定义带注释的类型 class 成员(例如列表或字典)时 PyPy 失败? - Why PyPy fails when I try to define type annotated class member (e.g. list or dict)? 当我尝试用一个(线)图和一个图上的条形图 plot 制作一个图时,我得到一个奇怪的图 - I get an odd figure when I try to make a figure with both one (line)plot and a bar plot at one figure 当我尝试 label 我的 plot 时出现这种类型的错误 - I'm getting this type error when I try to label my plot 尝试仅查看txt文件中的特定行TypeError:&#39;in <string> &#39;需要字符串作为左操作数,而不是我尝试时的dict引发 - Trying to only look at specific lines in txt file TypeError: 'in <string>' requires string as left operand, not dict raises when I try 当我尝试上传图片时,Tweepy 抛出错误。 (AttributeError: 'dict' object 没有属性 'media_id_string') - Tweepy is throwing an error when I try to upload an image. (AttributeError: 'dict' object has no attribute 'media_id_string')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM