plotmpl.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. #!/usr/bin/env python3
  2. #
  3. # Plot CSV files with matplotlib.
  4. #
  5. # Example:
  6. # ./scripts/plotmpl.py bench.csv -xSIZE -ybench_read -obench.svg
  7. #
  8. # Copyright (c) 2022, The littlefs authors.
  9. # SPDX-License-Identifier: BSD-3-Clause
  10. #
  11. import codecs
  12. import collections as co
  13. import csv
  14. import io
  15. import itertools as it
  16. import math as m
  17. import numpy as np
  18. import os
  19. import shutil
  20. import time
  21. import matplotlib as mpl
  22. import matplotlib.pyplot as plt
  23. # some nicer colors borrowed from Seaborn
  24. # note these include a non-opaque alpha
  25. COLORS = [
  26. '#4c72b0bf', # blue
  27. '#dd8452bf', # orange
  28. '#55a868bf', # green
  29. '#c44e52bf', # red
  30. '#8172b3bf', # purple
  31. '#937860bf', # brown
  32. '#da8bc3bf', # pink
  33. '#8c8c8cbf', # gray
  34. '#ccb974bf', # yellow
  35. '#64b5cdbf', # cyan
  36. ]
  37. COLORS_DARK = [
  38. '#a1c9f4bf', # blue
  39. '#ffb482bf', # orange
  40. '#8de5a1bf', # green
  41. '#ff9f9bbf', # red
  42. '#d0bbffbf', # purple
  43. '#debb9bbf', # brown
  44. '#fab0e4bf', # pink
  45. '#cfcfcfbf', # gray
  46. '#fffea3bf', # yellow
  47. '#b9f2f0bf', # cyan
  48. ]
  49. ALPHAS = [0.75]
  50. FORMATS = ['-']
  51. FORMATS_POINTS = ['.']
  52. FORMATS_POINTS_AND_LINES = ['.-']
  53. WIDTH = 735
  54. HEIGHT = 350
  55. FONT_SIZE = 11
  56. SI_PREFIXES = {
  57. 18: 'E',
  58. 15: 'P',
  59. 12: 'T',
  60. 9: 'G',
  61. 6: 'M',
  62. 3: 'K',
  63. 0: '',
  64. -3: 'm',
  65. -6: 'u',
  66. -9: 'n',
  67. -12: 'p',
  68. -15: 'f',
  69. -18: 'a',
  70. }
  71. SI2_PREFIXES = {
  72. 60: 'Ei',
  73. 50: 'Pi',
  74. 40: 'Ti',
  75. 30: 'Gi',
  76. 20: 'Mi',
  77. 10: 'Ki',
  78. 0: '',
  79. -10: 'mi',
  80. -20: 'ui',
  81. -30: 'ni',
  82. -40: 'pi',
  83. -50: 'fi',
  84. -60: 'ai',
  85. }
  86. # formatter for matplotlib
  87. def si(x):
  88. if x == 0:
  89. return '0'
  90. # figure out prefix and scale
  91. p = 3*int(m.log(abs(x), 10**3))
  92. p = min(18, max(-18, p))
  93. # format with 3 digits of precision
  94. s = '%.3f' % (abs(x) / (10.0**p))
  95. s = s[:3+1]
  96. # truncate but only digits that follow the dot
  97. if '.' in s:
  98. s = s.rstrip('0')
  99. s = s.rstrip('.')
  100. return '%s%s%s' % ('-' if x < 0 else '', s, SI_PREFIXES[p])
  101. # formatter for matplotlib
  102. def si2(x):
  103. if x == 0:
  104. return '0'
  105. # figure out prefix and scale
  106. p = 10*int(m.log(abs(x), 2**10))
  107. p = min(30, max(-30, p))
  108. # format with 3 digits of precision
  109. s = '%.3f' % (abs(x) / (2.0**p))
  110. s = s[:3+1]
  111. # truncate but only digits that follow the dot
  112. if '.' in s:
  113. s = s.rstrip('0')
  114. s = s.rstrip('.')
  115. return '%s%s%s' % ('-' if x < 0 else '', s, SI2_PREFIXES[p])
  116. # we want to use MaxNLocator, but since MaxNLocator forces multiples of 10
  117. # to be an option, we can't really...
  118. class AutoMultipleLocator(mpl.ticker.MultipleLocator):
  119. def __init__(self, base, nbins=None):
  120. # note base needs to be floats to avoid integer pow issues
  121. self.base = float(base)
  122. self.nbins = nbins
  123. super().__init__(self.base)
  124. def __call__(self):
  125. # find best tick count, conveniently matplotlib has a function for this
  126. vmin, vmax = self.axis.get_view_interval()
  127. vmin, vmax = mpl.transforms.nonsingular(vmin, vmax, 1e-12, 1e-13)
  128. if self.nbins is not None:
  129. nbins = self.nbins
  130. else:
  131. nbins = np.clip(self.axis.get_tick_space(), 1, 9)
  132. # find the best power, use this as our locator's actual base
  133. scale = self.base ** (m.ceil(m.log((vmax-vmin) / (nbins+1), self.base)))
  134. self.set_params(scale)
  135. return super().__call__()
  136. def openio(path, mode='r', buffering=-1):
  137. # allow '-' for stdin/stdout
  138. if path == '-':
  139. if mode == 'r':
  140. return os.fdopen(os.dup(sys.stdin.fileno()), mode, buffering)
  141. else:
  142. return os.fdopen(os.dup(sys.stdout.fileno()), mode, buffering)
  143. else:
  144. return open(path, mode, buffering)
  145. # parse different data representations
  146. def dat(x):
  147. # allow the first part of an a/b fraction
  148. if '/' in x:
  149. x, _ = x.split('/', 1)
  150. # first try as int
  151. try:
  152. return int(x, 0)
  153. except ValueError:
  154. pass
  155. # then try as float
  156. try:
  157. return float(x)
  158. # just don't allow infinity or nan
  159. if m.isinf(x) or m.isnan(x):
  160. raise ValueError("invalid dat %r" % x)
  161. except ValueError:
  162. pass
  163. # else give up
  164. raise ValueError("invalid dat %r" % x)
  165. def collect(csv_paths, renames=[]):
  166. # collect results from CSV files
  167. results = []
  168. for path in csv_paths:
  169. try:
  170. with openio(path) as f:
  171. reader = csv.DictReader(f, restval='')
  172. for r in reader:
  173. results.append(r)
  174. except FileNotFoundError:
  175. pass
  176. if renames:
  177. for r in results:
  178. # make a copy so renames can overlap
  179. r_ = {}
  180. for new_k, old_k in renames:
  181. if old_k in r:
  182. r_[new_k] = r[old_k]
  183. r.update(r_)
  184. return results
  185. def dataset(results, x=None, y=None, define=[]):
  186. # organize by 'by', x, and y
  187. dataset = {}
  188. i = 0
  189. for r in results:
  190. # filter results by matching defines
  191. if not all(k in r and r[k] in vs for k, vs in define):
  192. continue
  193. # find xs
  194. if x is not None:
  195. if x not in r:
  196. continue
  197. try:
  198. x_ = dat(r[x])
  199. except ValueError:
  200. continue
  201. else:
  202. x_ = i
  203. i += 1
  204. # find ys
  205. if y is not None:
  206. if y not in r:
  207. continue
  208. try:
  209. y_ = dat(r[y])
  210. except ValueError:
  211. continue
  212. else:
  213. y_ = None
  214. if y_ is not None:
  215. dataset[x_] = y_ + dataset.get(x_, 0)
  216. else:
  217. dataset[x_] = y_ or dataset.get(x_, None)
  218. return dataset
  219. def datasets(results, by=None, x=None, y=None, define=[]):
  220. # filter results by matching defines
  221. results_ = []
  222. for r in results:
  223. if all(k in r and r[k] in vs for k, vs in define):
  224. results_.append(r)
  225. results = results_
  226. # if y not specified, try to guess from data
  227. if y is None:
  228. y = co.OrderedDict()
  229. for r in results:
  230. for k, v in r.items():
  231. if (by is None or k not in by) and v.strip():
  232. try:
  233. dat(v)
  234. y[k] = True
  235. except ValueError:
  236. y[k] = False
  237. y = list(k for k,v in y.items() if v)
  238. if by is not None:
  239. # find all 'by' values
  240. ks = set()
  241. for r in results:
  242. ks.add(tuple(r.get(k, '') for k in by))
  243. ks = sorted(ks)
  244. # collect all datasets
  245. datasets = co.OrderedDict()
  246. for ks_ in (ks if by is not None else [()]):
  247. for x_ in (x if x is not None else [None]):
  248. for y_ in y:
  249. # hide x/y if there is only one field
  250. k_x = x_ if len(x or []) > 1 else ''
  251. k_y = y_ if len(y or []) > 1 or (not ks_ and not k_x) else ''
  252. datasets[ks_ + (k_x, k_y)] = dataset(
  253. results,
  254. x_,
  255. y_,
  256. [(by_, {k_}) for by_, k_ in zip(by, ks_)]
  257. if by is not None else [])
  258. return datasets
  259. def main(csv_paths, output, *,
  260. svg=False,
  261. png=False,
  262. quiet=False,
  263. by=None,
  264. x=None,
  265. y=None,
  266. define=[],
  267. points=False,
  268. points_and_lines=False,
  269. colors=None,
  270. formats=None,
  271. width=WIDTH,
  272. height=HEIGHT,
  273. xlim=(None,None),
  274. ylim=(None,None),
  275. xlog=False,
  276. ylog=False,
  277. x2=False,
  278. y2=False,
  279. xticks=None,
  280. yticks=None,
  281. xunits=None,
  282. yunits=None,
  283. xlabel=None,
  284. ylabel=None,
  285. xticklabels=None,
  286. yticklabels=None,
  287. title=None,
  288. legend=None,
  289. dark=False,
  290. ggplot=False,
  291. xkcd=False,
  292. font=None,
  293. font_size=FONT_SIZE,
  294. background=None):
  295. # guess the output format
  296. if not png and not svg:
  297. if output.endswith('.png'):
  298. png = True
  299. else:
  300. svg = True
  301. # allow shortened ranges
  302. if len(xlim) == 1:
  303. xlim = (0, xlim[0])
  304. if len(ylim) == 1:
  305. ylim = (0, ylim[0])
  306. # separate out renames
  307. renames = list(it.chain.from_iterable(
  308. ((k, v) for v in vs)
  309. for k, vs in it.chain(by or [], x or [], y or [])))
  310. if by is not None:
  311. by = [k for k, _ in by]
  312. if x is not None:
  313. x = [k for k, _ in x]
  314. if y is not None:
  315. y = [k for k, _ in y]
  316. # what colors/alphas/formats to use?
  317. if colors is not None:
  318. colors_ = colors
  319. elif dark:
  320. colors_ = COLORS_DARK
  321. else:
  322. colors_ = COLORS
  323. if formats is not None:
  324. formats_ = formats
  325. elif points_and_lines:
  326. formats_ = FORMATS_POINTS_AND_LINES
  327. elif points:
  328. formats_ = FORMATS_POINTS
  329. else:
  330. formats_ = FORMATS
  331. if background is not None:
  332. background_ = background
  333. elif dark:
  334. background_ = mpl.style.library['dark_background']['figure.facecolor']
  335. else:
  336. background_ = plt.rcParams['figure.facecolor']
  337. # allow escape codes in labels/titles
  338. if title is not None:
  339. title = codecs.escape_decode(title.encode('utf8'))[0].decode('utf8')
  340. if xlabel is not None:
  341. xlabel = codecs.escape_decode(xlabel.encode('utf8'))[0].decode('utf8')
  342. if ylabel is not None:
  343. ylabel = codecs.escape_decode(ylabel.encode('utf8'))[0].decode('utf8')
  344. # first collect results from CSV files
  345. results = collect(csv_paths, renames)
  346. # then extract the requested datasets
  347. datasets_ = datasets(results, by, x, y, define)
  348. # configure some matplotlib settings
  349. if xkcd:
  350. plt.xkcd()
  351. # turn off the white outline, this breaks some things
  352. plt.rc('path', effects=[])
  353. if ggplot:
  354. plt.style.use('ggplot')
  355. plt.rc('patch', linewidth=0)
  356. plt.rc('axes', edgecolor=background_)
  357. plt.rc('grid', color=background_)
  358. # fix the the gridlines when ggplot+xkcd
  359. if xkcd:
  360. plt.rc('grid', linewidth=1)
  361. plt.rc('axes.spines', bottom=False, left=False)
  362. if dark:
  363. plt.style.use('dark_background')
  364. plt.rc('savefig', facecolor='auto')
  365. # fix ggplot when dark
  366. if ggplot:
  367. plt.rc('axes',
  368. facecolor='#333333',
  369. edgecolor=background_,
  370. labelcolor='#aaaaaa')
  371. plt.rc('xtick', color='#aaaaaa')
  372. plt.rc('ytick', color='#aaaaaa')
  373. plt.rc('grid', color=background_)
  374. if font is not None:
  375. plt.rc('font', family=font)
  376. plt.rc('font', size=font_size)
  377. plt.rc('figure', titlesize='medium')
  378. plt.rc('axes', titlesize='medium', labelsize='small')
  379. plt.rc('xtick', labelsize='small')
  380. plt.rc('ytick', labelsize='small')
  381. plt.rc('legend',
  382. fontsize='small',
  383. fancybox=False,
  384. framealpha=None,
  385. borderaxespad=0)
  386. plt.rc('axes.spines', top=False, right=False)
  387. plt.rc('figure', facecolor=background_, edgecolor=background_)
  388. if not ggplot:
  389. plt.rc('axes', facecolor='#00000000')
  390. # create a matplotlib plot
  391. fig = plt.figure(figsize=(
  392. width/plt.rcParams['figure.dpi'],
  393. height/plt.rcParams['figure.dpi']),
  394. # note we need a linewidth to keep xkcd mode happy
  395. linewidth=8)
  396. ax = fig.subplots()
  397. for i, (name, dataset) in enumerate(datasets_.items()):
  398. dats = sorted((x,y) for x,y in dataset.items())
  399. ax.plot([x for x,_ in dats], [y for _,y in dats],
  400. formats_[i % len(formats_)],
  401. color=colors_[i % len(colors_)],
  402. label=','.join(k for k in name if k))
  403. # axes scaling
  404. if xlog:
  405. ax.set_xscale('symlog')
  406. ax.xaxis.set_minor_locator(mpl.ticker.NullLocator())
  407. if ylog:
  408. ax.set_yscale('symlog')
  409. ax.yaxis.set_minor_locator(mpl.ticker.NullLocator())
  410. # axes limits
  411. ax.set_xlim(
  412. xlim[0] if xlim[0] is not None
  413. else min(it.chain([0], (k
  414. for r in datasets_.values()
  415. for k, v in r.items()
  416. if v is not None))),
  417. xlim[1] if xlim[1] is not None
  418. else max(it.chain([0], (k
  419. for r in datasets_.values()
  420. for k, v in r.items()
  421. if v is not None))))
  422. ax.set_ylim(
  423. ylim[0] if ylim[0] is not None
  424. else min(it.chain([0], (v
  425. for r in datasets_.values()
  426. for _, v in r.items()
  427. if v is not None))),
  428. ylim[1] if ylim[1] is not None
  429. else max(it.chain([0], (v
  430. for r in datasets_.values()
  431. for _, v in r.items()
  432. if v is not None))))
  433. # axes ticks
  434. if x2:
  435. ax.xaxis.set_major_formatter(lambda x, pos:
  436. si2(x)+(xunits if xunits else ''))
  437. if xticklabels is not None:
  438. ax.xaxis.set_ticklabels(xticklabels)
  439. if xticks is None:
  440. ax.xaxis.set_major_locator(AutoMultipleLocator(2))
  441. elif isinstance(xticks, list):
  442. ax.xaxis.set_major_locator(mpl.ticker.FixedLocator(xticks))
  443. elif xticks != 0:
  444. ax.xaxis.set_major_locator(AutoMultipleLocator(2, xticks-1))
  445. else:
  446. ax.xaxis.set_major_locator(mpl.ticker.NullLocator())
  447. else:
  448. ax.xaxis.set_major_formatter(lambda x, pos:
  449. si(x)+(xunits if xunits else ''))
  450. if xticklabels is not None:
  451. ax.xaxis.set_ticklabels(xticklabels)
  452. if xticks is None:
  453. ax.xaxis.set_major_locator(mpl.ticker.AutoLocator())
  454. elif isinstance(xticks, list):
  455. ax.xaxis.set_major_locator(mpl.ticker.FixedLocator(xticks))
  456. elif xticks != 0:
  457. ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(xticks-1))
  458. else:
  459. ax.xaxis.set_major_locator(mpl.ticker.NullLocator())
  460. if y2:
  461. ax.yaxis.set_major_formatter(lambda x, pos:
  462. si2(x)+(yunits if yunits else ''))
  463. if yticklabels is not None:
  464. ax.yaxis.set_ticklabels(yticklabels)
  465. if yticks is None:
  466. ax.yaxis.set_major_locator(AutoMultipleLocator(2))
  467. elif isinstance(yticks, list):
  468. ax.yaxis.set_major_locator(mpl.ticker.FixedLocator(yticks))
  469. elif yticks != 0:
  470. ax.yaxis.set_major_locator(AutoMultipleLocator(2, yticks-1))
  471. else:
  472. ax.yaxis.set_major_locator(mpl.ticker.NullLocator())
  473. else:
  474. ax.yaxis.set_major_formatter(lambda x, pos:
  475. si(x)+(yunits if yunits else ''))
  476. if yticklabels is not None:
  477. ax.yaxis.set_ticklabels(yticklabels)
  478. if yticks is None:
  479. ax.yaxis.set_major_locator(mpl.ticker.AutoLocator())
  480. elif isinstance(yticks, list):
  481. ax.yaxis.set_major_locator(mpl.ticker.FixedLocator(yticks))
  482. elif yticks != 0:
  483. ax.yaxis.set_major_locator(mpl.ticker.MaxNLocator(yticks-1))
  484. else:
  485. ax.yaxis.set_major_locator(mpl.ticker.NullLocator())
  486. # axes labels
  487. if xlabel is not None:
  488. ax.set_xlabel(xlabel)
  489. if ylabel is not None:
  490. ax.set_ylabel(ylabel)
  491. if ggplot:
  492. ax.grid(sketch_params=None)
  493. if title is not None:
  494. ax.set_title(title)
  495. # pre-render so we can derive some bboxes
  496. fig.tight_layout()
  497. # it's not clear how you're actually supposed to get the renderer if
  498. # get_renderer isn't supported
  499. try:
  500. renderer = fig.canvas.get_renderer()
  501. except AttributeError:
  502. renderer = fig._cachedRenderer
  503. # add a legend? this actually ends up being _really_ complicated
  504. if legend == 'right':
  505. l_pad = fig.transFigure.inverted().transform((
  506. mpl.font_manager.FontProperties('small')
  507. .get_size_in_points()/2,
  508. 0))[0]
  509. legend_ = ax.legend(
  510. bbox_to_anchor=(1+l_pad, 1),
  511. loc='upper left',
  512. fancybox=False,
  513. borderaxespad=0)
  514. if ggplot:
  515. legend_.get_frame().set_linewidth(0)
  516. fig.tight_layout()
  517. elif legend == 'left':
  518. l_pad = fig.transFigure.inverted().transform((
  519. mpl.font_manager.FontProperties('small')
  520. .get_size_in_points()/2,
  521. 0))[0]
  522. # place legend somewhere to get its bbox
  523. legend_ = ax.legend(
  524. bbox_to_anchor=(0, 1),
  525. loc='upper right',
  526. fancybox=False,
  527. borderaxespad=0)
  528. # first make space for legend without the legend in the figure
  529. l_bbox = (legend_.get_tightbbox(renderer)
  530. .transformed(fig.transFigure.inverted()))
  531. legend_.remove()
  532. fig.tight_layout(rect=(0, 0, 1-l_bbox.width-l_pad, 1))
  533. # place legend after tight_layout computation
  534. bbox = (ax.get_tightbbox(renderer)
  535. .transformed(ax.transAxes.inverted()))
  536. legend_ = ax.legend(
  537. bbox_to_anchor=(bbox.x0-l_pad, 1),
  538. loc='upper right',
  539. fancybox=False,
  540. borderaxespad=0)
  541. if ggplot:
  542. legend_.get_frame().set_linewidth(0)
  543. elif legend == 'above':
  544. l_pad = fig.transFigure.inverted().transform((
  545. 0,
  546. mpl.font_manager.FontProperties('small')
  547. .get_size_in_points()/2))[1]
  548. # try different column counts until we fit in the axes
  549. for ncol in reversed(range(1, len(datasets_)+1)):
  550. legend_ = ax.legend(
  551. bbox_to_anchor=(0.5, 1+l_pad),
  552. loc='lower center',
  553. ncol=ncol,
  554. fancybox=False,
  555. borderaxespad=0)
  556. if ggplot:
  557. legend_.get_frame().set_linewidth(0)
  558. l_bbox = (legend_.get_tightbbox(renderer)
  559. .transformed(ax.transAxes.inverted()))
  560. if l_bbox.x0 >= 0:
  561. break
  562. # fix the title
  563. if title is not None:
  564. t_bbox = (ax.title.get_tightbbox(renderer)
  565. .transformed(ax.transAxes.inverted()))
  566. ax.set_title(None)
  567. fig.tight_layout(rect=(0, 0, 1, 1-t_bbox.height))
  568. l_bbox = (legend_.get_tightbbox(renderer)
  569. .transformed(ax.transAxes.inverted()))
  570. ax.set_title(title, y=1+l_bbox.height+l_pad)
  571. elif legend == 'below':
  572. l_pad = fig.transFigure.inverted().transform((
  573. 0,
  574. mpl.font_manager.FontProperties('small')
  575. .get_size_in_points()/2))[1]
  576. # try different column counts until we fit in the axes
  577. for ncol in reversed(range(1, len(datasets_)+1)):
  578. legend_ = ax.legend(
  579. bbox_to_anchor=(0.5, 0),
  580. loc='upper center',
  581. ncol=ncol,
  582. fancybox=False,
  583. borderaxespad=0)
  584. l_bbox = (legend_.get_tightbbox(renderer)
  585. .transformed(ax.transAxes.inverted()))
  586. if l_bbox.x0 >= 0:
  587. break
  588. # first make space for legend without the legend in the figure
  589. l_bbox = (legend_.get_tightbbox(renderer)
  590. .transformed(fig.transFigure.inverted()))
  591. legend_.remove()
  592. fig.tight_layout(rect=(0, 0, 1, 1-l_bbox.height-l_pad))
  593. bbox = (ax.get_tightbbox(renderer)
  594. .transformed(ax.transAxes.inverted()))
  595. legend_ = ax.legend(
  596. bbox_to_anchor=(0.5, bbox.y0-l_pad),
  597. loc='upper center',
  598. ncol=ncol,
  599. fancybox=False,
  600. borderaxespad=0)
  601. if ggplot:
  602. legend_.get_frame().set_linewidth(0)
  603. # compute another tight_layout for good measure, because this _does_
  604. # fix some things... I don't really know why though
  605. fig.tight_layout()
  606. plt.savefig(output, format='png' if png else 'svg', bbox_inches='tight')
  607. # some stats
  608. if not quiet:
  609. print('updated %s, %s datasets, %s points' % (
  610. output,
  611. len(datasets_),
  612. sum(len(dataset) for dataset in datasets_.values())))
  613. if __name__ == "__main__":
  614. import sys
  615. import argparse
  616. parser = argparse.ArgumentParser(
  617. description="Plot CSV files with matplotlib.",
  618. allow_abbrev=False)
  619. parser.add_argument(
  620. 'csv_paths',
  621. nargs='*',
  622. help="Input *.csv files.")
  623. parser.add_argument(
  624. '-o', '--output',
  625. required=True,
  626. help="Output *.svg/*.png file.")
  627. parser.add_argument(
  628. '--svg',
  629. action='store_true',
  630. help="Output an svg file. By default this is infered.")
  631. parser.add_argument(
  632. '--png',
  633. action='store_true',
  634. help="Output a png file. By default this is infered.")
  635. parser.add_argument(
  636. '-q', '--quiet',
  637. action='store_true',
  638. help="Don't print info.")
  639. parser.add_argument(
  640. '-b', '--by',
  641. action='append',
  642. type=lambda x: (
  643. lambda k,v=None: (k, v.split(',') if v is not None else ())
  644. )(*x.split('=', 1)),
  645. help="Group by this field. Can rename fields with new_name=old_name.")
  646. parser.add_argument(
  647. '-x',
  648. action='append',
  649. type=lambda x: (
  650. lambda k,v=None: (k, v.split(',') if v is not None else ())
  651. )(*x.split('=', 1)),
  652. help="Field to use for the x-axis. Can rename fields with "
  653. "new_name=old_name.")
  654. parser.add_argument(
  655. '-y',
  656. action='append',
  657. type=lambda x: (
  658. lambda k,v=None: (k, v.split(',') if v is not None else ())
  659. )(*x.split('=', 1)),
  660. help="Field to use for the y-axis. Can rename fields with "
  661. "new_name=old_name.")
  662. parser.add_argument(
  663. '-D', '--define',
  664. type=lambda x: (lambda k,v: (k, set(v.split(','))))(*x.split('=', 1)),
  665. action='append',
  666. help="Only include results where this field is this value. May include "
  667. "comma-separated options.")
  668. parser.add_argument(
  669. '-.', '--points',
  670. action='store_true',
  671. help="Only draw data points.")
  672. parser.add_argument(
  673. '-!', '--points-and-lines',
  674. action='store_true',
  675. help="Draw data points and lines.")
  676. parser.add_argument(
  677. '--colors',
  678. type=lambda x: [x.strip() for x in x.split(',')],
  679. help="Comma-separated hex colors to use.")
  680. parser.add_argument(
  681. '--formats',
  682. type=lambda x: [x.strip().replace('0',',') for x in x.split(',')],
  683. help="Comma-separated matplotlib formats to use. Allows '0' as an "
  684. "alternative for ','.")
  685. parser.add_argument(
  686. '-W', '--width',
  687. type=lambda x: int(x, 0),
  688. help="Width in pixels. Defaults to %r." % WIDTH)
  689. parser.add_argument(
  690. '-H', '--height',
  691. type=lambda x: int(x, 0),
  692. help="Height in pixels. Defaults to %r." % HEIGHT)
  693. parser.add_argument(
  694. '-X', '--xlim',
  695. type=lambda x: tuple(
  696. dat(x) if x.strip() else None
  697. for x in x.split(',')),
  698. help="Range for the x-axis.")
  699. parser.add_argument(
  700. '-Y', '--ylim',
  701. type=lambda x: tuple(
  702. dat(x) if x.strip() else None
  703. for x in x.split(',')),
  704. help="Range for the y-axis.")
  705. parser.add_argument(
  706. '--xlog',
  707. action='store_true',
  708. help="Use a logarithmic x-axis.")
  709. parser.add_argument(
  710. '--ylog',
  711. action='store_true',
  712. help="Use a logarithmic y-axis.")
  713. parser.add_argument(
  714. '--x2',
  715. action='store_true',
  716. help="Use base-2 prefixes for the x-axis.")
  717. parser.add_argument(
  718. '--y2',
  719. action='store_true',
  720. help="Use base-2 prefixes for the y-axis.")
  721. parser.add_argument(
  722. '--xticks',
  723. type=lambda x: int(x, 0) if ',' not in x
  724. else [dat(x) for x in x.split(',')],
  725. help="Ticks for the x-axis. This can be explicit comma-separated "
  726. "ticks, the number of ticks, or 0 to disable.")
  727. parser.add_argument(
  728. '--yticks',
  729. type=lambda x: int(x, 0) if ',' not in x
  730. else [dat(x) for x in x.split(',')],
  731. help="Ticks for the y-axis. This can be explicit comma-separated "
  732. "ticks, the number of ticks, or 0 to disable.")
  733. parser.add_argument(
  734. '--xunits',
  735. help="Units for the x-axis.")
  736. parser.add_argument(
  737. '--yunits',
  738. help="Units for the y-axis.")
  739. parser.add_argument(
  740. '--xlabel',
  741. help="Add a label to the x-axis.")
  742. parser.add_argument(
  743. '--ylabel',
  744. help="Add a label to the y-axis.")
  745. parser.add_argument(
  746. '--xticklabels',
  747. type=lambda x: [x.strip() for x in x.split(',')],
  748. help="Comma separated xticklabels.")
  749. parser.add_argument(
  750. '--yticklabels',
  751. type=lambda x: [x.strip() for x in x.split(',')],
  752. help="Comma separated yticklabels.")
  753. parser.add_argument(
  754. '-t', '--title',
  755. help="Add a title.")
  756. parser.add_argument(
  757. '-l', '--legend',
  758. nargs='?',
  759. choices=['above', 'below', 'left', 'right'],
  760. const='right',
  761. help="Place a legend here.")
  762. parser.add_argument(
  763. '--dark',
  764. action='store_true',
  765. help="Use the dark style.")
  766. parser.add_argument(
  767. '--ggplot',
  768. action='store_true',
  769. help="Use the ggplot style.")
  770. parser.add_argument(
  771. '--xkcd',
  772. action='store_true',
  773. help="Use the xkcd style.")
  774. parser.add_argument(
  775. '--font',
  776. type=lambda x: [x.strip() for x in x.split(',')],
  777. help="Font family for matplotlib.")
  778. parser.add_argument(
  779. '--font-size',
  780. help="Font size for matplotlib. Defaults to %r." % FONT_SIZE)
  781. parser.add_argument(
  782. '--background',
  783. help="Background color to use.")
  784. sys.exit(main(**{k: v
  785. for k, v in vars(parser.parse_intermixed_args()).items()
  786. if v is not None}))