我已使用xlrd
从Excel电子表格中读取了一系列单元格,并将其插入数组中,并且我想使用matplotlib在图形上绘制该数组,但出现此错误消息:
TypeError: float() argument must be a string or a number
代码:
import xlrd
import matplotlib.pyplot as plt
workbook = xlrd.open_workbook('values.xls')
worksheet = workbook.sheet_by_index(0)
xvals = worksheet.col_slice(colx=0, start_rowx=11, end_rowx=75)
yvals = worksheet.col_slice(colx=1, start_rowx=11, end_rowx=75)
plt.plot(xvals, yvals)
plt.xlabel('xvals')
plt.ylabel('yvals')
plt.show()
我可以打印数组并查看值,并且我确认数组中每个值的类型都是float。
但是当我尝试绘制数组时,我得到了错误消息和空白图
答案 0 :(得分:0)
尝试一下:
import xlrd
import matplotlib.pyplot as plt
workbook = xlrd.open_workbook('values.xls')
worksheet = workbook.sheet_by_index(0)
xvals = worksheet.col_slice(colx=0, start_rowx=11, end_rowx=75)
yvals = worksheet.col_slice(colx=1, start_rowx=11, end_rowx=75)
xvals = [int(i) for i in xvals ]
xvals = [int(i) for i in yvals ]
plt.plot(xvals, yvals)
plt.xlabel('xvals')
plt.ylabel('yvals')
plt.show()
答案 1 :(得分:0)
我改变了
worksheet.col_slice
到
worksheet.col_values
它按预期工作。