plot_histogram.py
This file
Path
scripts/plot_histogram.py
104 lines · 2.5 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–9why, one step back2# /// script3# requires-python = ">=3.10"4# dependencies = [5# "matplotlib",6# "pyqt6",7# "numpy",8# ]9# ///Fix clippy warningsDavid Peterbefore adoptionec43213
lines 10–25why, one step back1011"""This program shows `hyperfine` benchmark results as a histogram."""1213import argparse14import json15import numpy as np16import matplotlib.pyplot as plt1718parser = argparse.ArgumentParser(description=__doc__)19parser.add_argument("file", help="JSON file with benchmark results")20parser.add_argument("--title", help="Plot title")21parser.add_argument(22 "--labels", help="Comma-separated list of entries for the plot legend"23)24parser.add_argument("--bins", help="Number of bins (default: auto)")25parser.add_argument(Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
lines 26–43why, one step back26 "--legend-location",27 help="Location of the legend on plot (default: upper center)",28 choices=[29 "upper center",30 "lower center",31 "right",32 "left",33 "best",34 "upper left",35 "upper right",36 "lower left",37 "lower right",38 "center left",39 "center right",40 "center",41 ],42 default="upper center",43)add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140
line 44why, one step back44parser.add_argument(Fix clippy warningsDavid Peterbefore adoptionec43213
lines 45–46why, one step back45 "--type", help="Type of histogram (*bar*, barstacked, step, stepfilled)"46)Add --log-count option to plot_histogram.pyDavid Peterbefore adoption77e7b6c
line 47why, one step back47parser.add_argument("-o", "--output", help="Save image to the given filename.")Fix clippy warningsDavid Peterbefore adoptionec43213
lines 48–53why, one step back48parser.add_argument(49 "--t-min", metavar="T", help="Minimum time to be displayed (seconds)"50)51parser.add_argument(52 "--t-max", metavar="T", help="Maximum time to be displayed (seconds)"53)Add --log-count option to plot_histogram.pyDavid Peterbefore adoption77e7b6c
lines 54–58why, one step back54parser.add_argument(55 "--log-count",56 help="Use a logarithmic y-axis for the event count",57 action="store_true",58)Fix clippy warningsDavid Peterbefore adoptionec43213
lines 59–76why, one step back5960args = parser.parse_args()6162with open(args.file) as f:63 results = json.load(f)["results"]6465if args.labels:66 labels = args.labels.split(",")67else:68 labels = [b["command"] for b in results]69all_times = [b["times"] for b in results]7071t_min = float(args.t_min) if args.t_min else np.min(list(map(np.min, all_times)))72t_max = float(args.t_max) if args.t_max else np.max(list(map(np.max, all_times)))7374bins = int(args.bins) if args.bins else "auto"75histtype = args.type if args.type else "bar"76add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140
line 77why, one step back77plt.figure(figsize=(10, 5))Fix clippy warningsDavid Peterbefore adoptionec43213
line 78why, one step back78plt.hist(Add --log-count option to plot_histogram.pyDavid Peterbefore adoption77e7b6c
lines 79–83why, one step back79 all_times,80 label=labels,81 bins=bins,82 histtype=histtype,83 range=(t_min, t_max),Fix clippy warningsDavid Peterbefore adoptionec43213
line 84why, one step back84)add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140
line 85why, one step back85plt.legend(Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
lines 86–90why, one step back86 loc=args.legend_location,87 fancybox=True,88 shadow=True,89 prop={"size": 10, "family": ["Source Code Pro", "Fira Mono", "Courier New"]},90)Fix clippy warningsDavid Peterbefore adoptionec43213
lines 91–94why, one step back9192plt.xlabel("Time [s]")93if args.title:94 plt.title(args.title)Add --log-count option to plot_histogram.pyDavid Peterbefore adoption77e7b6c
lines 95–99why, one step back9596if args.log_count:97 plt.yscale("log")98else:99 plt.ylim(0, None)Fix clippy warningsDavid Peterbefore adoptionec43213
lines 100–101why, one step back100101if args.output:add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140
line 102why, one step back102 plt.savefig(args.output, dpi=600)Fix clippy warningsDavid Peterbefore adoptionec43213
lines 103–104why, one step back103else:104 plt.show()