Skip to content
Myr

plot_histogram.py

This file

Path

scripts/plot_histogram.py

105 lines · 2.5 KiB · Python

Last changed by

Sort importsDavid Peterbefore adoptionbc37eff

History of this file

Take it

The walk stopped after 200 commits: lines older than c44aa1d point at it.

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

line 1why, one step back
1#!/usr/bin/env python

Add PEP-723 inline metadataDavid Peterbefore adoptioncfae9cb

lines 2–9why, one step back
2# /// script
3# requires-python = ">=3.10"4# dependencies = [5# "matplotlib",6# "pyqt6",7# "numpy",8# ]9# ///

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

lines 10–14why, one step back
10
11"""This program shows `hyperfine` benchmark results as a histogram."""1213import argparse14import json

Sort importsDavid Peterbefore adoptionbc37eff

line 15why, one step back
15

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

line 16why, one step back
16import matplotlib.pyplot as plt

Sort importsDavid Peterbefore adoptionbc37eff

line 17why, one step back
17import numpy as np

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

lines 18–26why, one step back
18
19parser = argparse.ArgumentParser(description=__doc__)20parser.add_argument("file", help="JSON file with benchmark results")21parser.add_argument("--title", help="Plot title")22parser.add_argument(23 "--labels", help="Comma-separated list of entries for the plot legend"24)25parser.add_argument("--bins", help="Number of bins (default: auto)")26parser.add_argument(

Update formatting, use f-stringsDavid Peterbefore adoption65dd84f

lines 27–44why, one step back
27 "--legend-location",
28 help="Location of the legend on plot (default: upper center)",29 choices=[30 "upper center",31 "lower center",32 "right",33 "left",34 "best",35 "upper left",36 "upper right",37 "lower left",38 "lower right",39 "center left",40 "center right",41 "center",42 ],43 default="upper center",44)

add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140

line 45why, one step back
45parser.add_argument(

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

lines 46–47why, one step back
46 "--type", help="Type of histogram (*bar*, barstacked, step, stepfilled)"
47)

Add --log-count option to plot_histogram.pyDavid Peterbefore adoption77e7b6c

line 48why, one step back
48parser.add_argument("-o", "--output", help="Save image to the given filename.")

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

lines 49–54why, one step back
49parser.add_argument(
50 "--t-min", metavar="T", help="Minimum time to be displayed (seconds)"51)52parser.add_argument(53 "--t-max", metavar="T", help="Maximum time to be displayed (seconds)"54)

Add --log-count option to plot_histogram.pyDavid Peterbefore adoption77e7b6c

lines 55–59why, one step back
55parser.add_argument(
56 "--log-count",57 help="Use a logarithmic y-axis for the event count",58 action="store_true",59)

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

lines 60–77why, one step back
60
61args = parser.parse_args()6263with open(args.file) as f:64 results = json.load(f)["results"]6566if args.labels:67 labels = args.labels.split(",")68else:69 labels = [b["command"] for b in results]70all_times = [b["times"] for b in results]7172t_min = float(args.t_min) if args.t_min else np.min(list(map(np.min, all_times)))73t_max = float(args.t_max) if args.t_max else np.max(list(map(np.max, all_times)))7475bins = int(args.bins) if args.bins else "auto"76histtype = args.type if args.type else "bar"77

add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140

line 78why, one step back
78plt.figure(figsize=(10, 5))

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

line 79why, one step back
79plt.hist(

Add --log-count option to plot_histogram.pyDavid Peterbefore adoption77e7b6c

lines 80–84why, one step back
80 all_times,
81 label=labels,82 bins=bins,83 histtype=histtype,84 range=(t_min, t_max),

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

line 85why, one step back
85)

add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140

line 86why, one step back
86plt.legend(

Update formatting, use f-stringsDavid Peterbefore adoption65dd84f

lines 87–91why, one step back
87 loc=args.legend_location,
88 fancybox=True,89 shadow=True,90 prop={"size": 10, "family": ["Source Code Pro", "Fira Mono", "Courier New"]},91)

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

lines 92–95why, one step back
92
93plt.xlabel("Time [s]")94if args.title:95 plt.title(args.title)

Add --log-count option to plot_histogram.pyDavid Peterbefore adoption77e7b6c

lines 96–100why, one step back
96
97if args.log_count:98 plt.yscale("log")99else:100 plt.ylim(0, None)

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

lines 101–102why, one step back
101
102if args.output:

add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140

line 103why, one step back
103 plt.savefig(args.output, dpi=600)

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

lines 104–105why, one step back
104else:
105 plt.show()