plot_histogram.py
This file
Path
scripts/plot_histogram.py
50 lines · 1.4 KiB · Python
Last changed by
Add parameters to plotting scriptsBenjamin Rosemannbefore adoptionb04a9ec
History of this fileTake it
scripts: modify shebang to use "/usr/bin/env python"muxatorbefore adoption5a80a23
line 1why, one step back1#!/usr/bin/env pythonAdd a script to plot histogramssharkdpbefore adoptionef8a166
lines 2–6why, one step back23"""This program shows `hyperfine` benchmark results as a histogram."""45import argparse6import jsonShow the full range in histogramsDavid Peterbefore adoptiona3f0278
line 7why, one step back7import numpy as npAdd a script to plot histogramssharkdpbefore adoptionef8a166
lines 8–12why, one step back8import 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")Add parameters to plotting scriptsBenjamin Rosemannbefore adoptionb04a9ec
lines 13–15why, one step back13parser.add_argument(14 "--labels", help="Comma-separated list of entries for the plot legend"15)plot_histogram.py: add --type and --binssharkdpbefore adoptiond441eef
lines 16–19why, one step back16parser.add_argument("--bins", help="Number of bins (default: auto)")17parser.add_argument(18 "--type", help="Type of histogram (*bar*, barstacked, step, stepfilled)"19)Add parameters to plotting scriptsBenjamin Rosemannbefore adoptionb04a9ec
line 20why, one step back20parser.add_argument("--imgname", help="Save image to the given filename.")Add a script to plot histogramssharkdpbefore adoptionef8a166
lines 21–25why, one step back21args = parser.parse_args()2223with open(args.file) as f:24 results = json.load(f)["results"]25Add parameters to plotting scriptsBenjamin Rosemannbefore adoptionb04a9ec
lines 26–29why, one step back26if args.labels:27 labels = args.labels.split(",")28else:29 labels = [b["command"] for b in results]Add a script to plot histogramssharkdpbefore adoptionef8a166
lines 30–31why, one step back30all_times = [b["times"] for b in results]31Fix computation of min/max time rangesharkdpbefore adoptiondab1da9
lines 32–34why, one step back32t_min = np.min(list(map(np.min, all_times)))33t_max = np.max(list(map(np.max, all_times)))34plot_histogram.py: add --type and --binssharkdpbefore adoptiond441eef
lines 35–37why, one step back35bins = int(args.bins) if args.bins else "auto"36histtype = args.type if args.type else "bar"37Auto-format Python scriptssharkdpbefore adoption8fff864
line 38why, one step back38plt.hist(Add parameters to plotting scriptsBenjamin Rosemannbefore adoptionb04a9ec
line 39why, one step back39 all_times, label=labels, bins=bins, histtype=histtype, range=(t_min, t_max),Auto-format Python scriptssharkdpbefore adoption8fff864
lines 40–41why, one step back40)41plt.legend(prop={"family": ["Source Code Pro", "Fira Mono", "Courier New"]})Add a script to plot histogramssharkdpbefore adoptionef8a166
lines 42–46why, one step back4243plt.xlabel("Time [s]")44if args.title:45 plt.title(args.title)46Add parameters to plotting scriptsBenjamin Rosemannbefore adoptionb04a9ec
lines 47–50why, one step back47if args.imgname:48 plt.savefig(args.imgname)49else:50 plt.show()