83 lines
3.8 KiB
Python
83 lines
3.8 KiB
Python
"""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'<svg xmlns="http://www.w3.org/2000/svg" width="{W}" height="{H}" font-family="sans-serif" font-size="12">',
|
|
f'<rect width="{W}" height="{H}" fill="white"/>',
|
|
f'<line x1="{PAD}" y1="{H - PAD}" x2="{W - PAD}" y2="{H - PAD}" stroke="black"/>',
|
|
f'<line x1="{PAD}" y1="{PAD}" x2="{PAD}" y2="{H - PAD}" stroke="black"/>',
|
|
f'<text x="{W / 2}" y="{H - 15}" text-anchor="middle">{html.escape(a.x)}</text>',
|
|
f'<text x="15" y="{H / 2}" text-anchor="middle" transform="rotate(-90 15 {H / 2})">{html.escape(a.y)}</text>',
|
|
f'<text x="{W / 2}" y="25" text-anchor="middle" font-size="14">{html.escape(a.title)}</text>']
|
|
for i in range(6): # five gridlines with their values
|
|
y = y1 * i / 5
|
|
out.append(f'<line x1="{PAD}" y1="{py(y):.1f}" x2="{W - PAD}" y2="{py(y):.1f}" stroke="#ddd"/>'
|
|
f'<text x="{PAD - 6}" y="{py(y) + 4:.1f}" text-anchor="end">{y:.3g}</text>')
|
|
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'<text x="{px(x):.1f}" y="{H - PAD + 16}" text-anchor="middle">{x:g}</text>')
|
|
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'<polyline points="{path}" fill="none" stroke="{color}" stroke-width="2"/>')
|
|
for x, v in sorted(pts.items()):
|
|
if len(v) > 1: # the spread of repeated runs
|
|
out.append(f'<line x1="{px(x):.1f}" y1="{py(min(v)):.1f}" x2="{px(x):.1f}" '
|
|
f'y2="{py(max(v)):.1f}" stroke="{color}" stroke-width="2"/>')
|
|
out.append(f'<text x="{W - PAD - 100}" y="{PAD + 16 * n}" fill="{color}">{html.escape(name)}</text>')
|
|
out.append("</svg>")
|
|
with open(a.out, "w") as f:
|
|
f.write("\n".join(out))
|
|
print(f"wrote {a.out}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|