]> rtime.felk.cvut.cz Git - hubacji1/iamcar2.git/blob - scripts/plot.py
Make predictable colors in histplot
[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     ax.set_xlim(-10, 500)
105     ax.set_xlabel(lx)
106     ax.set_ylabel(ly)
107
108     COLORS = ["tab:orange", "tab:red", "tab:blue", "tab:green"]
109     i = 0
110
111     for ck, cv in w.items():
112         ax.hist(
113             [v for k, v in w.items() if k == ck],
114             #alpha = 0.5,
115             histtype = "step",
116             #bins = 30,
117             color=COLORS[i]
118         )
119         X_WHERE = np.percentile([v for k, v in w.items() if k == ck], [95])
120         print("percentile is {}".format(X_WHERE))
121         plt.axvline(X_WHERE, lw=1, linestyle="--", color=COLORS[i])
122         i += 1
123
124     ax.minorticks_on()
125     ax.grid(
126         which="major",
127         axis="y",
128         linestyle="-",
129         linewidth="0.5",
130         color="gray",
131     )
132     ax.grid(
133         which="minor",
134         axis="y",
135         linestyle=":",
136         linewidth="0.5",
137         color="gray",
138     )
139     plt.legend([k for k, v in w.items()])
140     plt.savefig("out.eps", bbox_inches="tight")
141     plt.close()
142
143 if __name__ == "__main__":
144     if len(sys.argv) > 1:
145         w = sys.argv[1]
146     else:
147         w = "time"
148     if len(sys.argv) > 2:
149         scenario.DNAME = sys.argv[2]
150
151     plt.rcParams["font.size"] = 22
152     plt.rcParams["font.family"] = "sans-serif"
153     plt.rcParams["figure.figsize"] = [12, 4]
154
155     if w == "time":
156         boxplot(scenario.time(), "Elapsed time", yl=[0.05, 200], ly="Time [s]")
157     elif w == "htime":
158         histplot(scenario.time(), "Elapsed time histogram")
159     elif w == "cost":
160         boxplot(scenario.cost(), "Final path cost", yl=[10, 400], ly="Cost [m]")
161     elif w == "hcost":
162         histplot(scenario.cost(), "Final path cost histogram")
163     elif w == "orig_cost":
164         boxplot(scenario.orig_cost(), "Original path cost", yl=[10, 400], ly="Cost [m]")
165     elif w == "orig_hcost":
166         histplot(scenario.orig_cost(), "Original path cost histogram")
167     elif w == "cusp":
168         boxplot(scenario.cusp(), "Changes in direction", ly="Changes [-]")
169     elif w == "hcusp":
170         histplot(scenario.cusp(), "Changes in direction histogram")
171     elif w == "error":
172         barplot(
173             scenario.error_rate(),
174             "Path not found rate",
175             ly="Path not found [%]",
176         )
177     elif w == "iter":
178         boxplot(
179             scenario.iter(),
180             "Number of iterations",
181             yl=[1, 1000],
182             ly="Iterations [-]",
183         )
184     else:
185         print("""The following arguments are allowed:
186
187         time, htime, cost, hcost, cusp, hcusp, error, iter
188         """)