plot_whisker.py
This file
Path
scripts/plot_whisker.py
76 lines · 2.4 KiB · Python
Last changed by
Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
History of this fileTake it
1#!/usr/bin/env python2# /// script3# requires-python = ">=3.10"4# dependencies = [5# "matplotlib",6# "pyqt6",7# ]8# ///910"""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 argparse1920import matplotlib.pyplot as plt2122from plot_utils import METRICS, add_metric_argument, load_results232425def main():26 parser = argparse.ArgumentParser(description=__doc__)27 add_metric_argument(parser)28 parser.add_argument("file", help="JSON file with benchmark results")29 parser.add_argument("--title", help="Plot Title")30 parser.add_argument("--sort-by", choices=["median"], help="Sort method")31 parser.add_argument(32 "--labels", help="Comma-separated list of entries for the plot legend"33 )34 parser.add_argument("-o", "--output", help="Save image to the given filename.")3536 args = parser.parse_args()37 metric, [results] = load_results(parser, [args.file], args.metric)38 metric_info = METRICS[metric]3940 if args.labels:41 labels = args.labels.split(",")42 else:43 labels = [b.get("name", b["command"]) for b in results]44 values = [45 [m[metric]["value"] / metric_info.scale for m in b["measurements"]]46 for b in results47 ]4849 if args.sort_by == "median":50 medians = [b["summary"][metric]["median"] for b in results]51 indices = sorted(range(len(labels)), key=lambda k: medians[k])52 labels = [labels[i] for i in indices]53 values = [values[i] for i in indices]5455 plt.figure(figsize=(10, 6), constrained_layout=True)56 boxplot = plt.boxplot(values, vert=True, patch_artist=True)57 cmap = plt.get_cmap("rainbow")58 colors = [cmap(val / len(values)) for val in range(len(values))]5960 for patch, color in zip(boxplot["boxes"], colors):61 patch.set_facecolor(color)6263 if args.title:64 plt.title(args.title)65 plt.legend(handles=boxplot["boxes"], labels=labels, loc="best", fontsize="medium")66 plt.ylabel(metric_info.axis_label)67 plt.ylim(0, None)68 plt.xticks(list(range(1, len(labels) + 1)), labels, rotation=45)69 if args.output:70 plt.savefig(args.output)71 else:72 plt.show()737475if __name__ == "__main__":76 main()