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
The walk stopped after 200 commits: lines older than 0f91d0c point at it.
Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
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# ///Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
lines 9–18why, 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 argparseSort importsDavid Peterbefore adoptionbc37eff
line 19why, one step back19Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
lines 20–21why, one step back20import matplotlib.pyplot as plt21Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 22why, one step back22from plot_utils import METRICS, add_metric_argument, load_resultsSupport all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 23why, one step back23Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 24why, one step back24Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 25–34why, one step back25def 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.")Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 35why, one step back35Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 36–38why, one step back36 args = parser.parse_args()37 metric, [results] = load_results(parser, [args.file], args.metric)38 metric_info = METRICS[metric]Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 39why, one step back39Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 40–47why, one step back40 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 ]Nicer whiskers plotSerpent7776before adoption5c9d7a8
line 48why, one step back48Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 49–53why, one step back49 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]Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 54why, one step back54Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 55–58why, one step back55 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))]Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 59why, one step back59Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 60–76why, one step back60 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()