【发布时间】:2020-11-26 01:45:18
【问题描述】:
所以我在这里得到了一些代码,这个代码获取被承认的动物的物种,将它与分类单元相关联并计算分类单元。这是通过字典完成的,但是,当我在饼图中绘制它们时,它无法形成一个。
我确定字典是:Taxon:#N ofoccurrence,Taxon:#N ofoccurrence,等等。所以我可以将它转换为浮点数。但我尝试使用
#imports csv and mathplot
import csv
import matplotlib.pyplot as plt
#creates a correlation table as a dict
with open('species_taxon.csv', 'r', encoding = 'utf8', newline='') as f:
cf = csv.DictReader(f)
correlation = {row['Species']: row['Taxon'] for row in cf}
#then uses it to count the taxons
import collections
with open('Accessions-Jan-2018.csv','r', encoding = 'utf8', newline='') as f:
cf = csv.DictReader(f)
count = collections.Counter(correlation.get(row['species'], 'Unknown') for row in cf)
#prints the counted taxons
print(count)
当我尝试使用时
plt.pie([float(v) for v in count], labels=[float(k) for k in keys], autopct=None)
我得到了错误 ValueError: could not convert string to float: '#The name of the Taxon'
【问题讨论】:
-
我认为问题出在按键上。您是否尝试过不将键转换为浮点数?喜欢
labels=keys
标签: python csv dictionary pie-chart