-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
executable file
·94 lines (77 loc) · 3.19 KB
/
Copy pathplot.py
File metadata and controls
executable file
·94 lines (77 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import argparse
import glob
from itertools import cycle
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
sns.set(
style="darkgrid",
rc={
"figure.figsize": (7.2, 4.45),
"text.usetex": False,
"xtick.labelsize": 16,
"ytick.labelsize": 16,
"font.size": 15,
"figure.autolayout": True,
"axes.titlesize": 16,
"axes.labelsize": 17,
"lines.linewidth": 2,
"lines.markersize": 6,
"legend.fontsize": 15,
},
)
colors = sns.color_palette("colorblind", 4)
dashes_styles = cycle(["-", "-.", "--", ":"])
sns.set_palette(colors)
colors = cycle(colors)
def moving_average(interval, window_size):
if window_size == 1:
return interval
window = np.ones(int(window_size)) / float(window_size)
return np.convolve(interval, window, "same")
def plot_df(df, color, xaxis, yaxis, ma=1, label=""):
df[yaxis] = pd.to_numeric(df[yaxis], errors="coerce") # convert NaN string to NaN value
mean = df.groupby(xaxis).mean()[yaxis]
std = df.groupby(xaxis).std()[yaxis]
if ma > 1:
mean = moving_average(mean, ma)
std = moving_average(std, ma)
x = df.groupby(xaxis)[xaxis].mean().keys().values
plt.plot(x, mean, label=label, color=color, linestyle=next(dashes_styles))
plt.fill_between(x, mean + std, mean - std, alpha=0.25, color=color, rasterized=True)
if __name__ == "__main__":
prs = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter, description="""Plot Traffic Signal Metrics"""
)
prs.add_argument("-f", nargs="+", required=True, help="Measures files\n")
prs.add_argument("-l", nargs="+", default=None, help="File's legends\n")
prs.add_argument("-t", type=str, default="", help="Plot title\n")
prs.add_argument("-yaxis", type=str, default="system_mean_waiting_time", help="The column to plot.\n")
prs.add_argument("-xaxis", type=str, default="step", help="The x axis.\n")
prs.add_argument("-ma", type=int, default=1, help="Moving Average Window.\n")
prs.add_argument("-sep", type=str, default=",", help="Values separator on file.\n")
prs.add_argument("-xlabel", type=str, default="Time step (seconds)", help="X axis label.\n")
prs.add_argument("-ylabel", type=str, default="Mean waiting", help="Y axis label.\n")
prs.add_argument("-output", type=str, default=None, help="PDF output filename.\n")
args = prs.parse_args()
labels = cycle(args.l) if args.l is not None else cycle([str(i) for i in range(len(args.f))])
plt.figure()
# File reading and grouping
for file in args.f:
main_df = pd.DataFrame()
for f in glob.glob(file + "*"):
df = pd.read_csv(f, sep=args.sep)
if main_df.empty:
main_df = df
else:
main_df = pd.concat((main_df, df))
# Plot DataFrame
plot_df(main_df, xaxis=args.xaxis, yaxis=args.yaxis, label=next(labels), color=next(colors), ma=args.ma)
plt.title(args.t)
plt.ylabel(args.ylabel)
plt.xlabel(args.xlabel)
plt.ylim(bottom=0)
if args.output is not None:
plt.savefig(args.output + ".pdf", bbox_inches="tight")
plt.show()