plot_whisker.py
This file
Path
scripts/plot_whisker.py
65 lines · 2.0 KiB · Python
Last changed by
Add PEP-723 inline metadataDavid Peterbefore adoptioncfae9cb
History of this fileTake it
The walk stopped after 200 commits: lines older than ec43213 point at it.
Fix clippy warningsDavid Peterbefore adoptionec43213
line 1why, one step back1#!/usr/bin/env pythonAdd PEP-723 inline metadataDavid Peterbefore adoptioncfae9cb
lines 2–8why, one step back2# /// script3# requires-python = ">=3.10"4# dependencies = [5# "matplotlib",6# "pyqt6",7# ]8# ///Fix clippy warningsDavid Peterbefore adoptionec43213
lines 9–24why, one step back910"""This program shows `hyperfine` benchmark results as a box and whisker plot.1112Quoting from the matplotlib documentation:13 The box extends from the lower to upper quartile values of the data, with14 a line at the median. The whiskers extend from the box to show the range15 of the data. Flier points are those past the end of the whiskers.16"""1718import argparse19import json20import matplotlib.pyplot as plt2122parser = argparse.ArgumentParser(description=__doc__)23parser.add_argument("file", help="JSON file with benchmark results")24parser.add_argument("--title", help="Plot Title")Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 25why, one step back25parser.add_argument("--sort-by", choices=["median"], help="Sort method")Fix clippy warningsDavid Peterbefore adoptionec43213
lines 26–28why, one step back26parser.add_argument(27 "--labels", help="Comma-separated list of entries for the plot legend"28)Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 29why, one step back29parser.add_argument("-o", "--output", help="Save image to the given filename.")Fix clippy warningsDavid Peterbefore adoptionec43213
lines 30–32why, one step back3031args = parser.parse_args()32Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 33why, one step back33with open(args.file, encoding="utf-8") as f:Fix clippy warningsDavid Peterbefore adoptionec43213
lines 34–41why, one step back34 results = json.load(f)["results"]3536if args.labels:37 labels = args.labels.split(",")38else:39 labels = [b["command"] for b in results]40times = [b["times"] for b in results]41Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 42why, one step back42if args.sort_by == "median":Add --sort-by optionSerpent7776before adoption51d056a
lines 43–46why, one step back43 medians = [b["median"] for b in results]44 indices = sorted(range(len(labels)), key=lambda k: medians[k])45 labels = [labels[i] for i in indices]46 times = [times[i] for i in indices]Nicer whiskers plotSerpent7776before adoption5c9d7a8
line 47why, one step back47Fix long labels being cut offSerpent7776before adoption836d173
line 48why, one step back48plt.figure(figsize=(10, 6), constrained_layout=True)Fix clippy warningsDavid Peterbefore adoptionec43213
line 49why, one step back49boxplot = plt.boxplot(times, vert=True, patch_artist=True)Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
line 50why, one step back50cmap = plt.get_cmap("rainbow")Fix clippy warningsDavid Peterbefore adoptionec43213
lines 51–60why, one step back51colors = [cmap(val / len(times)) for val in range(len(times))]5253for patch, color in zip(boxplot["boxes"], colors):54 patch.set_facecolor(color)5556if args.title:57 plt.title(args.title)58plt.legend(handles=boxplot["boxes"], labels=labels, loc="best", fontsize="medium")59plt.ylabel("Time [s]")60plt.ylim(0, None)Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 61why, one step back61plt.xticks(list(range(1, len(labels) + 1)), labels, rotation=45)Fix clippy warningsDavid Peterbefore adoptionec43213
lines 62–65why, one step back62if args.output:63 plt.savefig(args.output)64else:65 plt.show()