我试图从一个.txt
文件与数据集绘制一个饼图的样子:馅饼情节matplotlib在Python
asp: 2.11
glu: 1.11
arg: 0.99
his: 5.11
acid: 11.1
base: 2.11
现在, 1)我想绘制与前4饼图条目和适当的标签。
2)然后使用最后2个条目的另一个饼图。
我试着用下面的代码,但我得到错误。我的代码是:
from pylab import *
inp = open('c:/users/rox/desktop/xx.txt','r').read().strip().replace(': ',' ').split('\n')
for line in map(str.split,inp):
x = line[0]
z = line[1]
fracs = [x]
labels = [z]
pie(fracs,labels=labels,explode=None,autopct='%1.1f%%,shadow=False)
show()
但此代码生成错误报告:无法将字符串转换为浮动...
,做我需要使用tempfile
绘制存在于.txt
文件第4项。
如果我想使用最后两行数据集绘制饼图,那么可以使用slicing
来完成。
发布回溯 –
如何解决没有2)最后一行中有 – Ovisek