]> rtime.felk.cvut.cz Git - hubacji1/iamcar2.git/blob - scripts/plot.py
Fix path not found rate title
[hubacji1/iamcar2.git] / scripts / plot.py
1 """Procedures for plotting graphs."""
2 import sys
3 import matplotlib.pyplot as plt
4 import numpy as np
5
6 import scenario
7
8 def boxplot(w={}, t="", yl=None, lx="Scenario [-]", ly=""):
9     """Plot boxplot graph.
10
11     Keyword arguments:
12     w -- What to plot.
13     t -- Graph title.
14     yl -- Y axis limit [min, max].
15     """
16     f, ax = plt.subplots()
17
18     ax.set_title("{} (log yscale)".format(t))
19     ax.set_yscale("log")
20     ax.set_xlabel(lx)
21     ax.set_ylabel(ly)
22
23     ax.boxplot([v for k, v in w.items()], labels=[k for k, v in w.items()])
24     ax.minorticks_on()
25     ax.grid(
26         which="major",
27         axis="y",
28         linestyle="-",
29         linewidth="0.5",
30         color="gray",
31     )
32     ax.grid(
33         which="minor",
34         axis="y",
35         linestyle=":",
36         linewidth="0.5",
37         color="gray",
38     )
39     if yl:
40         ax.set_ylim(yl)
41     plt.savefig("out.eps", bbox_inches="tight")
42     plt.close()
43
44 def barplot(wl=[], t="", yl=None, lx="Scenario [-]", ly=""):
45     """Plot barplot graph.
46
47     Keyword arguments:
48     wl -- What to plot list.
49     t -- Graph title.
50     yl -- Y axis limit [min, max].
51     """
52     if len(wl) < 1:
53         raise ValueError
54     elif len(wl) == 1:
55         wl.append([])
56     f, ax = plt.subplots()
57     ax.set_title(t)
58     ax.set_xlabel(lx)
59     ax.set_ylabel(ly)
60     w = wl[0]
61     ax.bar(
62         range(len(w)),
63         [v for k, v in w.items()],
64         tick_label=[k for k, v in w.items()],
65         width=1 / (len(wl) + 1),
66     )
67     for i in range(1, len(wl) - 1):
68         w = wl[i]
69         ax.bar(
70             [j + i * 1/len(wl) for j in range(len(w))],
71             [v for k, v in w.items()],
72             width=1 / (len(wl) + 1),
73         )
74     ax.minorticks_on()
75     ax.grid(
76         which="major",
77         axis="y",
78         linestyle="-",
79         linewidth="0.5",
80         color="gray"
81     )
82     ax.grid(
83         which="minor",
84         axis="y",
85         linestyle=":",
86         linewidth="0.5",
87         color="gray",
88     )
89     if yl:
90         ax.set_ylim(yl)
91     plt.savefig("out.eps", bbox_inches="tight")
92     plt.close()
93
94 def histplot(w={}, t=""):
95     """Plot histogram graph.
96
97     Keyword arguments:
98     w -- What to plot.
99     t -- Graph title.
100     """
101     f, ax = plt.subplots()
102     ax.set_title("{} (log yscale)".format(t))
103     ax.set_yscale("log")
104
105     for ck, cv in w.items():
106         ax.hist(
107             [v for k, v in w.items() if k == ck],
108             #alpha = 0.5,
109             histtype = "step",
110             #bins = 100,
111         )
112         X_WHERE = np.percentile([v for k, v in w.items() if k == ck], [95])
113         plt.axvline(X_WHERE, lw=1, linestyle="--")
114
115     ax.minorticks_on()
116     ax.grid(
117         which="major",
118         axis="y",
119         linestyle="-",
120         linewidth="0.5",
121         color="gray",
122     )
123     ax.grid(
124         which="minor",
125         axis="y",
126         linestyle=":",
127         linewidth="0.5",
128         color="gray",
129     )
130     plt.legend([k for k, v in w.items()])
131     plt.savefig("out.eps", bbox_inches="tight")
132     plt.close()
133
134 if __name__ == "__main__":
135     if len(sys.argv) > 1:
136         w = sys.argv[1]
137     else:
138         w = "time"
139
140     plt.rcParams["font.size"] = 22
141     plt.rcParams["font.family"] = "sans-serif"
142     plt.rcParams["figure.figsize"] = [12, 4]
143
144     if w == "time":
145         boxplot(scenario.time(), "Elapsed time", yl=[0.05, 200], ly="Time [s]")
146     elif w == "htime":
147         histplot(scenario.time(), "Elapsed time histogram")
148     elif w == "cost":
149         boxplot(scenario.cost(), "Final path cost", yl=[10, 400], ly="Cost [m]")
150     elif w == "hcost":
151         histplot(scenario.cost(), "Final path cost histogram")
152     elif w == "orig_cost":
153         boxplot(scenario.orig_cost(), "Original path cost", yl=[10, 400], ly="Cost [m]")
154     elif w == "orig_hcost":
155         histplot(scenario.orig_cost(), "Original path cost histogram")
156     elif w == "cusp":
157         boxplot(scenario.cusp(), "Changes in direction", ly="Changes [-]")
158     elif w == "hcusp":
159         histplot(scenario.cusp(), "Changes in direction histogram")
160     elif w == "error":
161         barplot(
162             scenario.error_rate(),
163             "Path not found rate",
164             ly="Path not found [%]",
165         )
166     elif w == "iter":
167         boxplot(
168             scenario.iter(),
169             "Number of iterations",
170             yl=[1, 1000],
171             ly="Iterations [-]",
172         )
173     else:
174         print("""The following arguments are allowed:
175
176         time, htime, cost, hcost, cusp, hcusp, error, iter
177         """)