前提
y,z座標の視線データの配列から、pandasでヒートマップを作りたいと思っています。
イメージ:https://pythonbasics.org/seaborn-heatmap/
実現したこと
ここに実現したことを箇条書きで書いてください。
① txtファイルからy,zにデータを格納(連番ではない)
②二次元配列に[y, z, 重複した回数]として格納
データの例(省略):
[['-854.564', '-92.653', 1], ['-856.039', '-85.4', 2], ['-854.958', '-87.016', 2], ['-881.358', '-115.083', 2], ['-881.337', '-117.82', 9], ['-874.907', '-80.494', 2], ['-872.105', '-80.82', 1], ['-871.114', '-81.154', 1], ['-870.602', '-79.852', 1], ['-869.552', '-80.692', 1], ['-869.194', '-79.456', 1]]
該当のソースコード
python
1import pandas as pd 2import numpy as np 3import seaborn as sns 4import re 5import matplotlib.pyplot as plt 6 7#① 8if __name__ == "__main__": 9 pts = [] 10 file_path = "gaze_data_location.txt" 11 with open(file_path) as f: 12 y = [] 13 z = [] 14 i = 0 15 lines = f.read().splitlines() 16 for line in lines: 17 new_str = re.search(r'Y=(.+)',line).group(1) 18 get_y = re.search(r'(.+)\sZ=',new_str).group(1) 19 y.append(float(get_y)*-1) 20 get_z = re.search(r'Z=(.+)',new_str).group(1) 21 z.append(float(get_z)) 22 pts.append((y[i],z[i])) 23 i += 1 24#② 25 y_str = [str(n) for n in y] 26 z_str = [str(n) for n in z] 27 l = [] 28 for i in range(len(y_str)): 29 flag = True 30 for j in range(len(l)): 31 if y_str[i] in l[j][0] and z_str[i] in l[j][1]: 32 flag = False 33 l[j][2] = l[j][2] + 1 34 if flag is True: 35 l.append([y_str[i],z_str[i],1])
考え
イメージとしてはpandasの行、列にy,zを振り分けて値に重複回数を入れたいのですが、やり方がわからないです。
補足
python
1plt.hist2d(y, z, bins=(500, 500), cmap=plt.cm.Greys) 2plt.xlim(-1000, -825) 3plt.ylim(120, 225) 4plt.show()
python
1fig, ax = plt.subplots(figsize =(100, 57)) 2plt.hexbin(y, z, bins = 10000, cmap=plt.cm.binary) 3ax.set_xlabel('X-axis') 4ax.set_ylabel('Y-axis') 5plt.xlim(-1000, -825) 6plt.ylim(120, 225) 7plt.tight_layout() 8plt.show()
