12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- import UnityEngine as ue
- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- import matplotlib.cm as cm
- # import seaborn as sns
- # import matplotlib.patches as patches
- # import matplotlib.colors as colors
- # from sklearn import preprocessing
- # from pylab import *
- # from matplotlib import style
- from matplotlib.colors import LinearSegmentedColormap
- from mpl_toolkits.mplot3d import Axes3D
- WIDTH = int(70)
- HEIGHT = int(35)
- OBSTACLE_PATH = "Assets/Data_image/obstacle.pkl"
- POSITION_PATH = ue.Application.dataPath + "/Data_position/80/Walk4.csv"
- HEATMAP_PATH = "Assets/Data_image/80/heatmap3D4.png"
- # 1. Get position data from csv file
- data = pd.read_csv(POSITION_PATH, sep=';', usecols=["Position x", "Position z"], decimal=',', dtype={'Position x': float, 'Position z': float})
- data = data.round(0)
- # 2. Group by positions and count appearance
- data_count = data.groupby(['Position x', 'Position z']).size().reset_index(name='counts')
- # 3. Assign x, y, z, width, depth, height
- x = data_count["Position x"].tolist()
- y = data_count["Position z"].tolist()
- z = np.zeros_like(len(x))
- dz = data_count["counts"].tolist() # Change
- # 4. Create figure and axes
- fig = plt.figure()
- ax = fig.add_subplot(111, projection='3d')
- # 5.1 Create custom colormap Day1, Day2, Day3
- cmap = LinearSegmentedColormap.from_list(name='day1', colors=[(0.40,0.76,0.65), (0.11,0.62,0.47)])
- # cmap = LinearSegmentedColormap.from_list(name='day2', colors=['grey', (0.99,0.55,0.38), (0.85,0.37,0.01)])
- # cmap = LinearSegmentedColormap.from_list(name='day3', colors=['grey', (0.55,0.63,0.80), (0.46,0.44,0.70)])
- # 5.2 Initialize array for coloring the bars
- dz_array = np.array(data_count['counts'])
- fracs = dz_array.astype(float) / dz_array.max()
- color_values = cmap(fracs.tolist())
- # 6. Create the bars
- # for i in range(len(x)):
- # img = ax.bar3d(x[i], y[i], z, 1, 1, dz[i], color=color_values[i], shade=False)
- img = ax.bar3d(x, y, z, 1, 1, dz, color=color_values, shade=False)
- # 7. Create Colorbar
- color_map = cm.ScalarMappable(cmap=cmap)
- color_map.set_array(dz)
- fig.colorbar(color_map)
- # 8.
- plt.show()
- # 10. Save 3D Heatmap
- # heatmap.get_figure().savefig(HEATMAP_PATH, transparent=True)
- fig.savefig(HEATMAP_PATH, transparent=True)
|