我有一本这样的字典:
my_dict = {'Southampton': '33.7%', 'Cherbourg': '55.36%', 'Queenstown': '38.96%'}
我怎样才能有一个简单的绘图,用3个条显示字典中每个键的值
我试过:
sns.barplot(x=my_dict.keys(), y = int(my_dict.values()))
但我得到:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'dict_values'
我做了以下工作:
我首先从dict中删除了
%
符号您的代码中有几个问题:
my_dict.values()
将所有值作为dict_values
对象返回int(my_dict.values()))
表示将所有值的集合转换为单个整数,而不是将每个值转换为整数。当然,前者毫无意义李>"float(12.34%"[:-1])
李>字典是不排序的。因此,这在Python中不再是问题>;=3.7和CPython 3.6;见下文@AmphotericLewisAcid的评论李>my_dict.keys()
和my_dict.values()
不能保证以相同的顺序返回键值对中的键和值,例如,您得到的键可能是['Southampton', 'Cherbourg', 'Queenstown']
,而您得到的值可能是"55.36%", "33.7", "38.96%"
。解决了所有这些问题后:
你会得到:
您需要将值转换为数字,现在它们是字符串:
相关问题 更多 >
编程相关推荐