plot_bench.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import matplotlib.pyplot as plt
4 import numpy as np
5 import csv
6 import sys
7 import os
8 from itertools import chain
9 from math import pi
10 import argparse
11 
12 parser = argparse.ArgumentParser(description="Plot benchmark result csv")
13 parser.add_argument("csv_file", help="csv file")
14 parser.add_argument("--image-suffix", default="eps", help="suffix to save image")
15 parser.add_argument("--only-save-image", action="store_true", help="die right after saving image")
16 parser.add_argument("--min", default=0, type=float, help="minimum value")
17 parser.add_argument("--max", default=0.2, type=float, help="maximum value")
18 args = parser.parse_args()
19 csv_file = args.csv_file
20 methods = ['none']
21 dx = 3
22 dy = 3
23 
24 def plot(theta, one_data, ax):
25  global im
26  xs = [data[0] for data in one_data]
27  ys = [data[1] for data in one_data]
28  zs = {}
29  for x, y, i in zip(xs, ys, range(len(xs))):
30  zs[(x, y)] = one_data[i][2]
31  xs.sort()
32  ys.sort()
33 
34  minz = args.min
35  maxz = args.max
36 
37  img = np.arange(0, len(xs) * len(ys), 1.0).reshape((len(xs), len(ys)))
38  for x, i in zip(xs, range(len(xs))):
39  for y, j in zip(ys, range(len(ys))):
40  try:
41  z = zs[(x, y)]
42  except:
43  print("Failed to find (%f, %f)" % (x, y), file=sys.stderr)
44  img[j][i] = z
45  #im = ax.pcolor(img, vmin=minz, vmax=maxz, cmap="gnuplot")
46  im = ax.imshow(img, vmin=minz, vmax=maxz, cmap="gnuplot")
47  ax.set_xticklabels(np.arange(-dx - 1, dx+1))
48  ax.set_yticklabels(np.arange(-dy - 1, dy+1))
49  ax.set_xlabel("$x$ [m]")
50  ax.set_ylabel("$y$ [m]")
51  ax.set_title('$\\theta = %.0f$ deg' % (theta / pi * 180.0))
52 
53 
54 reader = csv.reader(open(csv_file))
55 fields = reader.next()
56 
57 initialized = False
58 
59 data = {}
60 # theta -> x, y, time
61 
62 for row in reader:
63  # Initialization
64  if not initialized:
65  figure_num = int(row[fields.index("n_theta")])
66  if figure_num % 3 == 0:
67  xnum = figure_num / 3
68  else:
69  xnum = figure_num / 3 + 1
70  fig, axes = plt.subplots(xnum, 3)
71  initialized = True
72  theta_str = row[fields.index("theta")]
73  theta = float(theta_str)
74  if not data.has_key(theta_str):
75  data[theta_str] = []
76  data[theta_str].append((float(row[fields.index("x")]), float(row[fields.index("y")]), float(row[fields.index("one_time")])))
77 
78 counter = 0
79 #for theta, one_data in data.items():
80 for theta in sorted(data.keys()):
81  one_data = data[theta]
82  print("Plotting theta=", theta)
83  plot(float(theta), one_data, axes.flat[counter])
84  counter = counter + 1
85 
86 for a in axes.flat[counter:]:
87  fig.delaxes(a)
88 plt.tight_layout()
89 fig.subplots_adjust(right = 0.8)
90 cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
91 cb = fig.colorbar(im, cax=cbar_ax)
92 cb.set_label("Time [sec]")
93 plt.interactive(True)
94 plt.show()
95 # save figure
96 eps_file = os.path.basename(csv_file) + "." + args.image_suffix
97 print("Saving to %s file: " % (args.image_suffix), eps_file)
98 plt.savefig(eps_file)
99 if args.only_save_image:
100  sys.exit(0)
101 while True:
102  plt.pause(1)
def plot(theta, one_data, ax)
Definition: plot_bench.py:24


jsk_footstep_planner
Author(s): Ryohei Ueda
autogenerated on Fri May 14 2021 02:51:52