曲速飞跃 发表于 2020-8-16 13:37:29

绘制图表报错

错误:
Traceback (most recent call last):
File "D:\my.code\CityMorning Tools\TurtleTomato\TurtleTomato.py", line 157, in <module>
    plt.pie(sizes,   autopct='%1.1f%%',
File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\pyplot.py", line 2810, in pie
    return gca().pie(
File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\__init__.py", line 1431, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\axes\_axes.py", line 2995, in pie
    x = np.asarray(x, np.float32)
File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\core\_asarray.py", line 83, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: '1,1,2'


代码:
import matplotlib.pyplot as plt
      sizes =
      plt.pie(sizes,   autopct='%1.1f%%',
      shadow=True, startangle=90)
      plt.axis('equal')
      plt.show()

s = '1,1,2'

Twilight6 发表于 2020-8-16 14:02:52


改成这样,你字符串不能直接拿去用,切割逗号然后转为 int 即可

import matplotlib.pyplot as plt
s = '1,1,2'
sizes = [ int(i) for i in s.split(',')]
plt.pie(sizes,   autopct='%1.1f%%',
shadow=True, startangle=90)
plt.axis('equal')
plt.show()

txxcat 发表于 2020-8-16 14:38:10

如果一定要用变量s,直接把列表赋值给s就可以了。
import matplotlib.pyplot as plt
sizes = s
plt.pie(sizes,   autopct='%1.1f%%',
shadow=True, startangle=90)
plt.axis('equal')
plt.show()

s=
页: [1]
查看完整版本: 绘制图表报错