plot_histogram.py
This file
Path
scripts/plot_histogram.py
83 lines · 2.3 KiB · Python
Last changed by
add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140
History of this fileTake it
1#!/usr/bin/env python23"""This program shows `hyperfine` benchmark results as a histogram."""45import argparse6import json7import numpy as np8import matplotlib.pyplot as plt9import matplotlib.font_manager1011parser = argparse.ArgumentParser(description=__doc__)12parser.add_argument("file", help="JSON file with benchmark results")13parser.add_argument("--title", help="Plot title")14parser.add_argument(15 "--labels", help="Comma-separated list of entries for the plot legend"16)17parser.add_argument("--bins", help="Number of bins (default: auto)")18parser.add_argument(19 "--legend-location", help="Location of the legend on plot (default: upper center)",20 choices=["upper center", "lower center", "right", "left", "best", "upper left", "upper right", "lower left", "lower right", "center left", "center right", "center"],21 default="upper center"22 )23parser.add_argument(24 "--type", help="Type of histogram (*bar*, barstacked, step, stepfilled)"25)26parser.add_argument("-o", "--output", help="Save image to the given filename.")27parser.add_argument(28 "--t-min", metavar="T", help="Minimum time to be displayed (seconds)"29)30parser.add_argument(31 "--t-max", metavar="T", help="Maximum time to be displayed (seconds)"32)33parser.add_argument(34 "--log-count",35 help="Use a logarithmic y-axis for the event count",36 action="store_true",37)3839args = parser.parse_args()4041with open(args.file) as f:42 results = json.load(f)["results"]4344if args.labels:45 labels = args.labels.split(",")46else:47 labels = [b["command"] for b in results]48all_times = [b["times"] for b in results]4950t_min = float(args.t_min) if args.t_min else np.min(list(map(np.min, all_times)))51t_max = float(args.t_max) if args.t_max else np.max(list(map(np.max, all_times)))5253bins = int(args.bins) if args.bins else "auto"54histtype = args.type if args.type else "bar"5556plt.figure(figsize=(10, 5))57plt.hist(58 all_times,59 label=labels,60 bins=bins,61 histtype=histtype,62 range=(t_min, t_max),63)64plt.legend(65 loc=args.legend_location,66 fancybox=True,67 shadow=True,68 prop={"size": 7, "family": ["Source Code Pro", "Fira Mono", "Courier New"]}69 )7071plt.xlabel("Time [s]")72if args.title:73 plt.title(args.title)7475if args.log_count:76 plt.yscale("log")77else:78 plt.ylim(0, None)7980if args.output:81 plt.savefig(args.output, dpi=600)82else:83 plt.show()