1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#!/usr/bin/env python3
# Copyright 2011-2023 David Robillard <d@drobilla.net>
# SPDX-License-Identifier: ISC
"""
Plot a benchmark result.
"""
import math
import os
import sys
import matplotlib
from matplotlib import pyplot
matplotlib.rc("text", **{"usetex": True})
matplotlib.rc(
"font",
**{
"family": "serif",
"serif": "Times",
"sans-serif": "Helvetica",
"monospace": "Courier",
},
)
pyplot.subplots_adjust(wspace=0.2, hspace=0.2)
class SensibleScalarFormatter(matplotlib.ticker.ScalarFormatter):
"ScalarFormatter which rounds order of magnitude to a multiple of 3"
def __init__(self):
matplotlib.ticker.ScalarFormatter.__init__(self)
self.set_powerlimits([-6, 6])
self.set_scientific(True)
def _set_order_of_magnitude(self):
# Calculate "best" order in the usual way
super()._set_order_of_magnitude()
# Round down to sensible (millions, billions, etc) order
self.orderOfMagnitude = self.orderOfMagnitude - (
self.orderOfMagnitude % 3
)
self.set_scientific(True)
if __name__ == "__main__":
file_prefix = os.path.commonprefix(sys.argv[1:])
N_PLOTS = len(sys.argv) - 2
for i in range(N_PLOTS):
filename = sys.argv[i + 2]
with open(filename, "r", encoding="utf-8") as in_file:
ax = pyplot.subplot(
math.ceil(math.sqrt(N_PLOTS)),
math.ceil(math.sqrt(N_PLOTS)),
i + 1,
)
ax.xaxis.set_major_formatter(SensibleScalarFormatter())
ax.yaxis.set_major_formatter(SensibleScalarFormatter())
for a in ["x", "y"]:
ax.grid(
which="major",
axis=a,
zorder=1,
linewidth=0.5,
linestyle=":",
color="0",
dashes=[0.5, 8.0],
)
header = in_file.readline()
columns = header[1:].split()
pyplot.xlabel("Elements")
pyplot.ylabel("Time (s)")
times = [[] for i in range(len(columns))]
for line in in_file:
if line[0] == "#":
continue
fields = line.split()
for index, field in enumerate(fields):
times[index].append([float(field)])
for t in range(len(times) - 1):
matplotlib.pyplot.plot(
times[0], times[t + 1], "-o", label=columns[t + 1]
)
pyplot.legend(
loc="upper left",
handletextpad=0.15,
borderpad=0.20,
borderaxespad=0,
labelspacing=0.10,
columnspacing=0,
framealpha=0.90,
)
file_prefix_len = len(file_prefix)
pyplot.title(os.path.splitext(filename[file_prefix_len:])[0].title())
print(f"Writing {sys.argv[1]}")
matplotlib.pyplot.tight_layout()
matplotlib.pyplot.savefig(sys.argv[1])
|