]> rtime.felk.cvut.cz Git - hubacji1/iamcar2.git/blob - scripts/plot.py
Add limit to y axis when plot
[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):
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
21     ax.set_title(t)
22     ax.boxplot([v for k, v in w.items()], labels=[k for k, v in w.items()])
23     ax.minorticks_on()
24     ax.grid(
25         which="major",
26         axis="y",
27         linestyle="-",
28         linewidth="0.5",
29         color="gray",
30     )
31     ax.grid(
32         which="minor",
33         axis="y",
34         linestyle=":",
35         linewidth="0.5",
36         color="gray",
37     )
38     if yl:
39         ax.set_ylim(yl)
40     plt.xticks(rotation="vertical")
41     plt.savefig("out.eps", bbox_inches="tight")
42     plt.close()
43
44 def barplot(wl=[], t="", yl=None):
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     w = wl[0]
59     ax.bar(
60         range(len(w)),
61         [v for k, v in w.items()],
62         tick_label=[k for k, v in w.items()],
63         width=1 / (len(wl) + 1),
64     )
65     for i in range(1, len(wl) - 1):
66         w = wl[i]
67         ax.bar(
68             [j + i * 1/len(wl) for j in range(len(w))],
69             [v for k, v in w.items()],
70             width=1 / (len(wl) + 1),
71         )
72     ax.minorticks_on()
73     ax.grid(
74         which="major",
75         axis="y",
76         linestyle="-",
77         linewidth="0.5",
78         color="gray"
79     )
80     ax.grid(
81         which="minor",
82         axis="y",
83         linestyle=":",
84         linewidth="0.5",
85         color="gray",
86     )
87     if yl:
88         ax.set_ylim(yl)
89     plt.xticks(rotation="vertical")
90     plt.savefig("out.eps", bbox_inches="tight")
91     plt.close()
92
93 def histplot(w={}, t=""):
94     """Plot histogram graph.
95
96     Keyword arguments:
97     w -- What to plot.
98     t -- Graph title.
99     """
100     f, ax = plt.subplots()
101     ax.set_title("{} (log yscale)".format(t))
102     ax.set_yscale("log")
103
104     for ck, cv in w.items():
105         ax.hist(
106             [v for k, v in w.items() if k == ck],
107             #alpha = 0.5,
108             histtype = "step",
109             #bins = 100,
110         )
111         X_WHERE = np.percentile([v for k, v in w.items() if k == ck], [95])
112         plt.axvline(X_WHERE, lw=1, linestyle="--")
113
114     ax.minorticks_on()
115     ax.grid(
116         which="major",
117         axis="y",
118         linestyle="-",
119         linewidth="0.5",
120         color="gray",
121     )
122     ax.grid(
123         which="minor",
124         axis="y",
125         linestyle=":",
126         linewidth="0.5",
127         color="gray",
128     )
129     plt.legend([k for k, v in w.items()])
130     plt.savefig("out.eps", bbox_inches="tight")
131     plt.close()
132
133 if __name__ == "__main__":
134     if len(sys.argv) > 1:
135         w = sys.argv[1]
136     else:
137         w = "time"
138
139     plt.rcParams["font.size"] = 24
140
141     if w == "time":
142         boxplot(scenario.time(), "Elapsed time", yl=[0.05, 100])
143     elif w == "htime":
144         histplot(scenario.time(), "Elapsed time histogram")
145     elif w == "cost":
146         boxplot(scenario.cost(), "Final path cost", yl=[1, 400])
147     elif w == "hcost":
148         histplot(scenario.cost(), "Final path cost histogram")
149     elif w == "cusp":
150         boxplot(scenario.cusp(), "Changes in direction")
151     elif w == "hcusp":
152         histplot(scenario.cusp(), "Changes in direction histogram")
153     elif w == "error":
154         barplot(scenario.error_rate(), "Error rate")
155     elif w == "iter":
156         boxplot(scenario.iter(), "Number of iterations", yl=[1, 1000])
157     else:
158         print("""The following arguments are allowed:
159
160         time, htime, cost, hcost, cusp, hcusp, error, iter
161         """)