plot_whisker.py
This file
Path
scripts/plot_whisker.py
60 lines · 1.9 KiB · Python
Last changed by
Add --sort-by optionSerpent7776before adoption51d056a
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")Add --sort-by optionSerpent7776before adoption51d056a
line 18why, one step back18parser.add_argument("--sort-by", choices=['median'], help="Sort method")Fix clippy warningsDavid Peterbefore adoptionec43213
lines 19–27why, one step back19parser.add_argument(20 "--labels", help="Comma-separated list of entries for the plot legend"21)22parser.add_argument(23 "-o", "--output", help="Save image to the given filename."24)2526args = parser.parse_args()27Nicer whiskers plotSerpent7776before adoption5c9d7a8
line 28why, one step back28with open(args.file, encoding='utf-8') as f:Fix clippy warningsDavid Peterbefore adoptionec43213
lines 29–36why, one step back29 results = json.load(f)["results"]3031if args.labels:32 labels = args.labels.split(",")33else:34 labels = [b["command"] for b in results]35times = [b["times"] for b in results]36Add --sort-by optionSerpent7776before adoption51d056a
lines 37–41why, one step back37if args.sort_by == 'median':38 medians = [b["median"] for b in results]39 indices = sorted(range(len(labels)), key=lambda k: medians[k])40 labels = [labels[i] for i in indices]41 times = [times[i] for i in indices]Nicer whiskers plotSerpent7776before adoption5c9d7a8
line 42why, one step back42Fix long labels being cut offSerpent7776before adoption836d173
line 43why, one step back43plt.figure(figsize=(10, 6), constrained_layout=True)Fix clippy warningsDavid Peterbefore adoptionec43213
line 44why, one step back44boxplot = plt.boxplot(times, vert=True, patch_artist=True)Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
line 45why, one step back45cmap = plt.get_cmap("rainbow")Fix clippy warningsDavid Peterbefore adoptionec43213
lines 46–55why, one step back46colors = [cmap(val / len(times)) for val in range(len(times))]4748for patch, color in zip(boxplot["boxes"], colors):49 patch.set_facecolor(color)5051if args.title:52 plt.title(args.title)53plt.legend(handles=boxplot["boxes"], labels=labels, loc="best", fontsize="medium")54plt.ylabel("Time [s]")55plt.ylim(0, None)Nicer whiskers plotSerpent7776before adoption5c9d7a8
line 56why, one step back56plt.xticks(list(range(1, len(labels)+1)), labels, rotation=45)Fix clippy warningsDavid Peterbefore adoptionec43213
lines 57–60why, one step back57if args.output:58 plt.savefig(args.output)59else:60 plt.show()