用mathplotlib从excel(XLRD)中绘制图形

2024-06-25 23:33:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我收到一个错误ValueError: could not convert string to float。我意识到我的数据包含空值('')。如何删除它们?我试过过滤器,但没用。在

book = xlrd.open_workbook('bioreactorfinal.xlsx')
sheet = book.sheets() [1]
data = [[sheet.cell_value(r,c) for c in range (sheet.ncols)] for r in range(sheet.nrows)]
x = sheet.col_values(3, start_rowx=1)
y = sheet.col_values(0, start_rowx=1)

plt.plot(x,y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('test')

plt.show()

#print(data[:100])

[['Hours', 'VCD (Cells/mL)', 'Volume (mL)', 'Cells', 'Container Size'], [0.0, 300000.0, 16.666666666666668, 5000000.0, 'SF100'], [24.0, 600000.0, 16.666666666666668, 10000000.0, 'SF100'], [48.0, 1200000.0, 16.666666666666668, 20000000.0, 'SF100'], [72.0, 2400000.0, 16.666666666666668, 40000000.0, 'SF100'], [72.0, 300000.0, 133.33333333333334, 40000000.0, 'SF1000'], [96.0, 600000.0, 133.33333333333334, 80000000.0, 'SF1000'], [120.0, 1200000.0, 133.33333333333334, 160000000.0, 'SF1000'], [144.0, 2400000.0, 133.33333333333334, 320000000.0, 'SF1000'], [144.0, 300000.0, 1066.6666666666667, 320000000.0, 'BR5'], [168.0, 600000.0, 1066.6666666666667, 640000000.0, 'BR5'], [192.0, 1200000.0, 1066.6666666666667, 1280000000.0, 'BR5'], [216.0, 2400000.0, 1066.6666666666667, 2560000000.0, 'BR5'], [216.0, 300000.0, 8533.333333333334, 2560000000.0, 'BR40'], [240.0, 600000.0, 8533.333333333334, 5120000000.0, 'BR40'], [264.0, 1200000.0, 8533.333333333334, 10240000000.0, 'BR40'], [288.0, 2400000.0, 8533.333333333334, 20480000000.0, 'BR40'], [288.0, 300000.0, 68266.66666666667, 20480000000.0, 'BR200'], [312.0, 600000.0, 68266.66666666667, 40960000000.0, 'BR200'], [336.0, 1200000.0, 68266.66666666667, 81920000000.0, 'BR200'], [360.0, 2400000.0, 68266.66666666667, 163840000000.0, 'BR200'], [360.0, 300000.0, 546133.3333333334, 163840000000.0, 'BR2k'], [384.0, 600000.0, 546133.3333333334, 327680000000.0, 'BR2k'], [408.0, 1200000.0, 546133.3333333334, 655360000000.0, 'BR2k'], [432.0, 2400000.0, 546133.3333333334, 1310720000000.0, 'BR2k'], [432.0, 300000.0, 4369066.666666667, 1310720000000.0, 'BR20k'], [456.0, 600000.0, 4369066.666666667, 2621440000000.0, 'BR20k'], [480.0, 1200000.0, 4369066.666666667, 5242880000000.0, 'BR20k'], [504.0, 2400000.0, 4369066.666666667, 10485760000000.0, 'BR20k'], [528.0, 4800000.0, 4369066.666666667, 20971520000000.0, 'BR20k'], [552.0, 9600000.0, 4369066.666666667, 41943040000000.0, 'BR20k'], ['', 300000.0, 139810133.33333334, '', 'Not Enough Space'], ['', 600000.0, 139810133.33333334, '', 'Not Enough Space'], ['', 1200000.0, 139810133.33333334, '', 'Not Enough Space']]

Tags: fordatanotpltspacesheetbookenough
2条回答

可以排除缺少数据的列。不删除第4行中所有列的浮点值:

new_data = [row for row in data if all(isinstance(item, float) for item in row[:4])]

这将选择xy值进行绘图:

^{pr2}$

现在绘制:

plt.plot(x,y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('test')

plt.show()

定义了x,y之后,在给plt函数赋值之前,可以过滤掉所有非浮点值:

from numbers import Number
def isvalid(a, b):
    return isinstance(a, Number) and isinstance(b, Number)
xy = [xi,yi for xi,yi in zip(x,y) if isvalid(xi,yi)]
x,y = zip(*xy)

任何至少有一个非成员的对将不在给的列表中plt.绘图. 在

相关问题 更多 >