"""Draw one line chart from a CSV file as an SVG image. Given; any computer, no container. python3 plot.py results.csv loss mbps --series cc --out throughput.svg python3 plot.py cwnd.csv t cwnd --series cc --out cwnd.svg X and Y are column names. With --series, each value of that column (reno, cubic) becomes its own line. Several rows with the same x in one series are drawn as their mean, with a bar from the smallest to the largest value, so repeated runs show their spread. Open the .svg in any web browser. You may use any other tool for your figure (a spreadsheet, matplotlib on your own computer). What matters is the question it answers, which you write in its caption in your report. """ import argparse import csv import html import statistics W, H, PAD = 720, 440, 60 COLORS = ["#1f6fb2", "#d9480f", "#2b8a3e", "#862e9c"] def main(): p = argparse.ArgumentParser(description=__doc__.splitlines()[0]) p.add_argument("csv") p.add_argument("x") p.add_argument("y") p.add_argument("--series", help="column whose values each get their own line") p.add_argument("--out", default="figure.svg") p.add_argument("--title", default="") a = p.parse_args() with open(a.csv, newline="") as f: rows = list(csv.DictReader(f)) lines = {} for r in rows: key = r[a.series] if a.series else a.y lines.setdefault(key, {}).setdefault(float(r[a.x]), []).append(float(r[a.y])) xs = [x for pts in lines.values() for x in pts] ys = [y for pts in lines.values() for v in pts.values() for y in v] x0, x1 = min(xs), max(xs) if max(xs) > min(xs) else min(xs) + 1 y1 = max(ys) * 1.1 or 1 def px(x): return PAD + (x - x0) / (x1 - x0) * (W - 2 * PAD) def py(y): return H - PAD - y / y1 * (H - 2 * PAD) out = [f'', f'', f'', f'', f'{html.escape(a.x)}', f'{html.escape(a.y)}', f'{html.escape(a.title)}'] for i in range(6): # five gridlines with their values y = y1 * i / 5 out.append(f'' f'{y:.3g}') for x in sorted(set(xs)) if len(set(xs)) <= 12 else [x0 + (x1 - x0) * i / 5 for i in range(6)]: out.append(f'{x:g}') for n, (name, pts) in enumerate(sorted(lines.items())): color = COLORS[n % len(COLORS)] mean = [(x, statistics.mean(v)) for x, v in sorted(pts.items())] path = " ".join(f"{px(x):.1f},{py(y):.1f}" for x, y in mean) out.append(f'') for x, v in sorted(pts.items()): if len(v) > 1: # the spread of repeated runs out.append(f'') out.append(f'{html.escape(name)}') out.append("") with open(a.out, "w") as f: f.write("\n".join(out)) print(f"wrote {a.out}") if __name__ == "__main__": main()