4 Visualize csv file of ik-grid in 2d heatmap 6 import matplotlib.pyplot
as plt
9 from itertools
import chain
14 if len(sys.argv) == 3:
15 image_file = sys.argv[2]
22 with open(csvfile)
as f:
23 reader = csv.reader(f)
26 x_index = index.index(
"x")
27 y_index = index.index(
"y")
28 z_index = index.index(
"z")
29 i_index = index.index(
"i")
30 j_index = index.index(
"j")
31 k_index = index.index(
"k")
32 value_index = index.index(
"value")
34 (x, y, z, i, j, k, value) = [float(row[x_index]),
40 float(row[value_index])]
48 if data_by_k.has_key(k):
49 data_by_k[k].append(data)
52 k_num = len(data_by_k.keys())
57 z_values = np.array(list(chain.from_iterable([[data[
"z"]
for data
in data_array]
58 for data_array
in data_by_k.values()])))
59 min_z = np.amin(z_values)
60 max_z = np.amax(z_values)
62 step = (max_z - min_z) / (k_num - 1)
65 fig, axes = plt.subplots(int(math.ceil(k_num / 3)), 3)
68 for k, counter
in zip(data_by_k, range(k_num)):
69 data_array = data_by_k[k]
70 i_s = sorted(set([data[
"i"]
for data
in data_array]))
71 j_s = sorted(set([data[
"j"]
for data
in data_array]))
74 min_i = min([data[
"i"]
for data
in data_array])
75 min_j = min([data[
"j"]
for data
in data_array])
76 min_value = min([data[
"value"]
for data
in data_array])
77 max_value = max([data[
"value"]
for data
in data_array])
78 min_x = min([data[
"x"]
for data
in data_array])
79 max_x = max([data[
"x"]
for data
in data_array])
80 min_y = min([data[
"y"]
for data
in data_array])
81 max_y = max([data[
"y"]
for data
in data_array])
83 image = np.arange(0, xnum * ynum, 1.0).reshape((xnum, ynum))
84 for data
in data_array:
87 image[i - min_i][j - min_j] = data[
"value"]
88 if data[
"value"] != 0:
89 non_zero_counter = non_zero_counter + 1
91 ax = axes.flat[counter]
95 ax.set_title(
"$z = {0}$mm".format(data_array[0][
"z"]))
96 ax.set_xlabel(
"$x$ [mm]")
97 ax.set_ylabel(
"$y$ [mm]")
98 ax.tick_params(labelsize=8)
99 im = ax.imshow(image, vmin=min_value, vmax=max_value, cmap=
"gnuplot", interpolation=
"none",
101 extent=[min_x-step/2, max_x+step/2, min_y-step/2, max_y+step/2])
104 print(
"{0} reached regions".format(non_zero_counter))
105 fig.suptitle(
"{0} reached regions".format(non_zero_counter), fontsize=8)
109 plt.savefig(image_file)