plot_whisker.py
This file
Path
scripts/plot_whisker.py
58 lines · 1.9 KiB · Python
Last changed by
Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
History of this fileTake it
The walk stopped after 200 commits: lines older than ec43213 point at it.
Fix clippy warningsDavid Peterbefore adoptionec43213
lines 1–17why, one step back1#!/usr/bin/env python23"""This program shows `hyperfine` benchmark results as a box and whisker plot.45Quoting from the matplotlib documentation:6 The box extends from the lower to upper quartile values of the data, with7 a line at the median. The whiskers extend from the box to show the range8 of the data. Flier points are those past the end of the whiskers.9"""1011import argparse12import json13import matplotlib.pyplot as plt1415parser = argparse.ArgumentParser(description=__doc__)16parser.add_argument("file", help="JSON file with benchmark results")17parser.add_argument("--title", help="Plot Title")Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 18why, one step back18parser.add_argument("--sort-by", choices=["median"], help="Sort method")Fix clippy warningsDavid Peterbefore adoptionec43213
lines 19–21why, one step back19parser.add_argument(20 "--labels", help="Comma-separated list of entries for the plot legend"21)Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 22why, one step back22parser.add_argument("-o", "--output", help="Save image to the given filename.")Fix clippy warningsDavid Peterbefore adoptionec43213
lines 23–25why, one step back2324args = parser.parse_args()25Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 26why, one step back26with open(args.file, encoding="utf-8") as f:Fix clippy warningsDavid Peterbefore adoptionec43213
lines 27–34why, one step back27 results = json.load(f)["results"]2829if args.labels:30 labels = args.labels.split(",")31else:32 labels = [b["command"] for b in results]33times = [b["times"] for b in results]34Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 35why, one step back35if args.sort_by == "median":Add --sort-by optionSerpent7776before adoption51d056a
lines 36–39why, one step back36 medians = [b["median"] for b in results]37 indices = sorted(range(len(labels)), key=lambda k: medians[k])38 labels = [labels[i] for i in indices]39 times = [times[i] for i in indices]Nicer whiskers plotSerpent7776before adoption5c9d7a8
line 40why, one step back40Fix long labels being cut offSerpent7776before adoption836d173
line 41why, one step back41plt.figure(figsize=(10, 6), constrained_layout=True)Fix clippy warningsDavid Peterbefore adoptionec43213
line 42why, one step back42boxplot = plt.boxplot(times, vert=True, patch_artist=True)Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
line 43why, one step back43cmap = plt.get_cmap("rainbow")Fix clippy warningsDavid Peterbefore adoptionec43213
lines 44–53why, one step back44colors = [cmap(val / len(times)) for val in range(len(times))]4546for patch, color in zip(boxplot["boxes"], colors):47 patch.set_facecolor(color)4849if args.title:50 plt.title(args.title)51plt.legend(handles=boxplot["boxes"], labels=labels, loc="best", fontsize="medium")52plt.ylabel("Time [s]")53plt.ylim(0, None)Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 54why, one step back54plt.xticks(list(range(1, len(labels) + 1)), labels, rotation=45)Fix clippy warningsDavid Peterbefore adoptionec43213
lines 55–58why, one step back55if args.output:56 plt.savefig(args.output)57else:58 plt.show()