Skip to content
Myr

plot_histogram.py

This file

Path

scripts/plot_histogram.py

71 lines · 1.9 KiB · Python

Last changed by

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

History of this file

Take it

1#!/usr/bin/env python23"""This program shows `hyperfine` benchmark results as a histogram."""45import argparse6import json7import numpy as np8import matplotlib.pyplot as plt910parser = argparse.ArgumentParser(description=__doc__)11parser.add_argument("file", help="JSON file with benchmark results")12parser.add_argument("--title", help="Plot title")13parser.add_argument(14    "--labels", help="Comma-separated list of entries for the plot legend"15)16parser.add_argument("--bins", help="Number of bins (default: auto)")17parser.add_argument(18    "--type", help="Type of histogram (*bar*, barstacked, step, stepfilled)"19)20parser.add_argument("-o", "--output", help="Save image to the given filename.")21parser.add_argument(22    "--t-min", metavar="T", help="Minimum time to be displayed (seconds)"23)24parser.add_argument(25    "--t-max", metavar="T", help="Maximum time to be displayed (seconds)"26)27parser.add_argument(28    "--log-count",29    help="Use a logarithmic y-axis for the event count",30    action="store_true",31)3233args = parser.parse_args()3435with open(args.file) as f:36    results = json.load(f)["results"]3738if args.labels:39    labels = args.labels.split(",")40else:41    labels = [b["command"] for b in results]42all_times = [b["times"] for b in results]4344t_min = float(args.t_min) if args.t_min else np.min(list(map(np.min, all_times)))45t_max = float(args.t_max) if args.t_max else np.max(list(map(np.max, all_times)))4647bins = int(args.bins) if args.bins else "auto"48histtype = args.type if args.type else "bar"4950plt.hist(51    all_times,52    label=labels,53    bins=bins,54    histtype=histtype,55    range=(t_min, t_max),56)57plt.legend(prop={"family": ["Source Code Pro", "Fira Mono", "Courier New"]})5859plt.xlabel("Time [s]")60if args.title:61    plt.title(args.title)6263if args.log_count:64    plt.yscale("log")65else:66    plt.ylim(0, None)6768if args.output:69    plt.savefig(args.output)70else:71    plt.show()