我正在尝试将 IR 传感器的读数转换为 python 中的热图。我已使用 Arduino 将读数转换为 csv 文件。 csv 文件包含多个值“表”,分为 24 行和 32 列
有没有一种方法可以为 csv 文件中的每个读数创建单独的热图?文件中共有 15 个读数。
我最初的方法是将读数转换为 24x32 数组并以这种方式绘制热图,但由于某种原因文件中的值是 dtype
import numpy as np
import csv
with open("serial_data.csv", "r") as file:
reader = list(csv.reader(file, delimiter=','))
reading1 = reader[2:26]
reading1_array = np.array(reading1)
map(lambda x: float(x),reading1_array)
print(reading1_array)
reading1_array.dtype
new_arr = reading1_array.astype(float)
print(new_arr)
然后我收到一条错误消息
ValueError: could not convert string to float: '24.0, 24.0, 24.6, 24.1, 23.1, 22.6, 22.5, 22.6, 22.7, 22.5, 22.7, 22.7, 22.9, 22.8, 23.2, 22.8, 23.2, 23.0, 23.1, 23.0, 23.1, 22.9, 23.4, 25.1, 24.3, 27.8, 26.0, 28.0, 26.4, 26.2, 25.5, 25.3,'
text = '''Ambient temperature =
""
"24.0, 24.0, 24.6, 24.1, 23.1, 22.6, 22.5, 22.6, 22.7, 22.5, 22.7, 22.7, 22.9, 22.8, 23.2, 22.8, 23.2, 23.0, 23.1, 23.0, 23.1, 22.9, 23.4, 25.1, 24.3, 27.8, 26.0, 28.0, 26.4, 26.2, 25.5, 25.3,"
"24.0, 24.0, 24.6, 24.1, 23.1, 22.6, 22.5, 22.6, 22.7, 22.5, 22.7, 22.7, 22.9, 22.8, 23.2, 22.8, 23.2, 23.0, 23.1, 23.0, 23.1, 22.9, 23.4, 25.1, 24.3, 27.8, 26.0, 28.0, 26.4, 26.2, 25.5, 25.3,"
'''
import csv
import io
import numpy as np
#with open("serial_data.csv", "r") as file:
with io.StringIO(text) as file:
reader = list(csv.reader(file, delimiter=','))
#print(reader)
data = reader[2:26]
data = [line[0].strip(' ,').split(', ') for line in data] # split string to list of strings
data = [list(map(float, row)) for row in data] # convert every row to float values
print('rows:')
for row in data:
print(row)
array = np.array(data)
print('array:')
print(array)
结果:
rows:
[24.0, 24.0, 24.6, 24.1, 23.1, 22.6, 22.5, 22.6, 22.7, 22.5, 22.7, 22.7, 22.9, 22.8, 23.2, 22.8, 23.2, 23.0, 23.1, 23.0, 23.1, 22.9, 23.4, 25.1, 24.3, 27.8, 26.0, 28.0, 26.4, 26.2, 25.5, 25.3]
[24.0, 24.0, 24.6, 24.1, 23.1, 22.6, 22.5, 22.6, 22.7, 22.5, 22.7, 22.7, 22.9, 22.8, 23.2, 22.8, 23.2, 23.0, 23.1, 23.0, 23.1, 22.9, 23.4, 25.1, 24.3, 27.8, 26.0, 28.0, 26.4, 26.2, 25.5, 25.3]
array:
[[24. 24. 24.6 24.1 23.1 22.6 22.5 22.6 22.7 22.5 22.7 22.7 22.9 22.8
23.2 22.8 23.2 23. 23.1 23. 23.1 22.9 23.4 25.1 24.3 27.8 26. 28.
26.4 26.2 25.5 25.3]
[24. 24. 24.6 24.1 23.1 22.6 22.5 22.6 22.7 22.5 22.7 22.7 22.9 22.8
23.2 22.8 23.2 23. 23.1 23. 23.1 22.9 23.4 25.1 24.3 27.8 26. 28.
26.4 26.2 25.5 25.3]]