plot_histogram.py
This file
Path
scripts/plot_histogram.py
131 lines · 3.3 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–9why, one step back2# /// script3# requires-python = ">=3.10"4# dependencies = [5# "matplotlib",6# "pyqt6",7# "numpy",8# ]9# ///Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
lines 10–13why, one step back1011"""This program shows `hyperfine` benchmark results as a histogram."""1213import argparseSort importsDavid Peterbefore adoptionbc37eff
line 14why, one step back14Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 15why, one step back15import matplotlib.pyplot as pltSort importsDavid Peterbefore adoptionbc37eff
line 16why, one step back16import numpy as npBump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 17why, one step back17Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 18why, one step back18from plot_utils import METRICS, add_metric_argument, load_resultsSupport all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 19why, one step back19Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 20why, one step back20Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 21–71why, one step back21def main():22 parser = argparse.ArgumentParser(description=__doc__)23 add_metric_argument(parser)24 parser.add_argument("file", help="JSON file with benchmark results")25 parser.add_argument("--title", help="Plot title")26 parser.add_argument(27 "--labels", help="Comma-separated list of entries for the plot legend"28 )29 parser.add_argument("--bins", help="Number of bins (default: auto)")30 parser.add_argument(31 "--legend-location",32 help="Location of the legend on plot (default: upper center)",33 choices=[34 "upper center",35 "lower center",36 "right",37 "left",38 "best",39 "upper left",40 "upper right",41 "lower left",42 "lower right",43 "center left",44 "center right",45 "center",46 ],47 default="upper center",48 )49 parser.add_argument(50 "--type", help="Type of histogram (*bar*, barstacked, step, stepfilled)"51 )52 parser.add_argument("-o", "--output", help="Save image to the given filename.")53 parser.add_argument(54 "--min",55 "--t-min",56 dest="value_min",57 type=float,58 help="Minimum metric value to display",59 )60 parser.add_argument(61 "--max",62 "--t-max",63 dest="value_max",64 type=float,65 help="Maximum metric value to display",66 )67 parser.add_argument(68 "--log-count",69 help="Use a logarithmic y-axis for the event count",70 action="store_true",71 )Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 72why, one step back72Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 73–75why, one step back73 args = parser.parse_args()74 metric, [results] = load_results(parser, [args.file], args.metric)75 metric_info = METRICS[metric]Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 76why, one step back76Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 77–84why, one step back77 if args.labels:78 labels = args.labels.split(",")79 else:80 labels = [b.get("name", b["command"]) for b in results]81 all_values = [82 [m[metric]["value"] / metric_info.scale for m in b["measurements"]]83 for b in results84 ]Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 85why, one step back85Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 86–95why, one step back86 value_min = (87 args.value_min88 if args.value_min is not None89 else np.min(list(map(np.min, all_values)))90 )91 value_max = (92 args.value_max93 if args.value_max is not None94 else np.max(list(map(np.max, all_values)))95 )Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 96why, one step back96Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 97–98why, one step back97 bins = int(args.bins) if args.bins else "auto"98 histtype = args.type if args.type else "bar"Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 99why, one step back99Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 100–113why, one step back100 plt.figure(figsize=(10, 5))101 plt.hist(102 all_values,103 label=labels,104 bins=bins,105 histtype=histtype,106 range=(value_min, value_max),107 )108 plt.legend(109 loc=args.legend_location,110 fancybox=True,111 shadow=True,112 prop={"size": 10, "family": ["Source Code Pro", "Fira Mono", "Courier New"]},113 )Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 114why, one step back114Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 115–117why, one step back115 plt.xlabel(metric_info.axis_label)116 if args.title:117 plt.title(args.title)Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 118why, one step back118Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 119–131why, one step back119 if args.log_count:120 plt.yscale("log")121 else:122 plt.ylim(0, None)123124 if args.output:125 plt.savefig(args.output, dpi=600)126 else:127 plt.show()128129130if __name__ == "__main__":131 main()