plotmpl.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. y_ = None
  208. else:
  209. try:
  210. y_ = dat(r[y])
  211. except ValueError:
  212. y_ = None
  213. else:
  214. y_ = None
  215. if y_ is not None:
  216. dataset[x_] = y_ + dataset.get(x_, 0)
  217. else:
  218. dataset[x_] = y_ or dataset.get(x_, None)
  219. return dataset
  220. def datasets(results, by=None, x=None, y=None, define=[]):
  221. # filter results by matching defines
  222. results_ = []
  223. for r in results:
  224. if all(k in r and r[k] in vs for k, vs in define):
  225. results_.append(r)
  226. results = results_
  227. # if y not specified, try to guess from data
  228. if y is None:
  229. y = co.OrderedDict()
  230. for r in results:
  231. for k, v in r.items():
  232. if (by is None or k not in by) and v.strip():
  233. try:
  234. dat(v)
  235. y[k] = True
  236. except ValueError:
  237. y[k] = False
  238. y = list(k for k,v in y.items() if v)
  239. if by is not None:
  240. # find all 'by' values
  241. ks = set()
  242. for r in results:
  243. ks.add(tuple(r.get(k, '') for k in by))
  244. ks = sorted(ks)
  245. # collect all datasets
  246. datasets = co.OrderedDict()
  247. for ks_ in (ks if by is not None else [()]):
  248. for x_ in (x if x is not None else [None]):
  249. for y_ in y:
  250. # hide x/y if there is only one field
  251. k_x = x_ if len(x or []) > 1 else ''
  252. k_y = y_ if len(y or []) > 1 or (not ks_ and not k_x) else ''
  253. datasets[ks_ + (k_x, k_y)] = dataset(
  254. results,
  255. x_,
  256. y_,
  257. [(by_, k_) for by_, k_ in zip(by, ks_)]
  258. if by is not None else [])
  259. return datasets
  260. def main(csv_paths, output, *,
  261. svg=False,
  262. png=False,
  263. quiet=False,
  264. by=None,
  265. x=None,
  266. y=None,
  267. define=[],
  268. points=False,
  269. points_and_lines=False,
  270. colors=None,
  271. formats=None,
  272. width=WIDTH,
  273. height=HEIGHT,
  274. xlim=(None,None),
  275. ylim=(None,None),
  276. xlog=False,
  277. ylog=False,
  278. x2=False,
  279. y2=False,
  280. xticks=None,
  281. yticks=None,
  282. xunits=None,
  283. yunits=None,
  284. xlabel=None,
  285. ylabel=None,
  286. xticklabels=None,
  287. yticklabels=None,
  288. title=None,
  289. legend=None,
  290. dark=False,
  291. ggplot=False,
  292. xkcd=False,
  293. font=None,
  294. font_size=FONT_SIZE,
  295. background=None):
  296. # guess the output format
  297. if not png and not svg:
  298. if output.endswith('.png'):
  299. png = True
  300. else:
  301. svg = True
  302. # allow shortened ranges
  303. if len(xlim) == 1:
  304. xlim = (0, xlim[0])
  305. if len(ylim) == 1:
  306. ylim = (0, ylim[0])
  307. # separate out renames
  308. renames = list(it.chain.from_iterable(
  309. ((k, v) for v in vs)
  310. for k, vs in it.chain(by or [], x or [], y or [])))
  311. if by is not None:
  312. by = [k for k, _ in by]
  313. if x is not None:
  314. x = [k for k, _ in x]
  315. if y is not None:
  316. y = [k for k, _ in y]
  317. # what colors/alphas/formats to use?
  318. if colors is not None:
  319. colors_ = colors
  320. elif dark:
  321. colors_ = COLORS_DARK
  322. else:
  323. colors_ = COLORS
  324. if formats is not None:
  325. formats_ = formats
  326. elif points_and_lines:
  327. formats_ = FORMATS_POINTS_AND_LINES
  328. elif points:
  329. formats_ = FORMATS_POINTS
  330. else:
  331. formats_ = FORMATS
  332. if background is not None:
  333. background_ = background
  334. elif dark:
  335. background_ = mpl.style.library['dark_background']['figure.facecolor']
  336. else:
  337. background_ = plt.rcParams['figure.facecolor']
  338. # allow escape codes in labels/titles
  339. if title is not None:
  340. title = codecs.escape_decode(title.encode('utf8'))[0].decode('utf8')
  341. if xlabel is not None:
  342. xlabel = codecs.escape_decode(xlabel.encode('utf8'))[0].decode('utf8')
  343. if ylabel is not None:
  344. ylabel = codecs.escape_decode(ylabel.encode('utf8'))[0].decode('utf8')
  345. # first collect results from CSV files
  346. results = collect(csv_paths, renames)
  347. # then extract the requested datasets
  348. datasets_ = datasets(results, by, x, y, define)
  349. # configure some matplotlib settings
  350. if xkcd:
  351. plt.xkcd()
  352. # turn off the white outline, this breaks some things
  353. plt.rc('path', effects=[])
  354. if ggplot:
  355. plt.style.use('ggplot')
  356. plt.rc('patch', linewidth=0)
  357. plt.rc('axes', edgecolor=background_)
  358. plt.rc('grid', color=background_)
  359. # fix the the gridlines when ggplot+xkcd
  360. if xkcd:
  361. plt.rc('grid', linewidth=1)
  362. plt.rc('axes.spines', bottom=False, left=False)
  363. if dark:
  364. plt.style.use('dark_background')
  365. plt.rc('savefig', facecolor='auto')
  366. # fix ggplot when dark
  367. if ggplot:
  368. plt.rc('axes',
  369. facecolor='#333333',
  370. edgecolor=background_,
  371. labelcolor='#aaaaaa')
  372. plt.rc('xtick', color='#aaaaaa')
  373. plt.rc('ytick', color='#aaaaaa')
  374. plt.rc('grid', color=background_)
  375. if font is not None:
  376. plt.rc('font', family=font)
  377. plt.rc('font', size=font_size)
  378. plt.rc('figure', titlesize='medium')
  379. plt.rc('axes', titlesize='medium', labelsize='small')
  380. plt.rc('xtick', labelsize='small')
  381. plt.rc('ytick', labelsize='small')
  382. plt.rc('legend',
  383. fontsize='small',
  384. fancybox=False,
  385. framealpha=None,
  386. borderaxespad=0)
  387. plt.rc('axes.spines', top=False, right=False)
  388. plt.rc('figure', facecolor=background_, edgecolor=background_)
  389. if not ggplot:
  390. plt.rc('axes', facecolor='#00000000')
  391. # create a matplotlib plot
  392. fig = plt.figure(figsize=(
  393. width/plt.rcParams['figure.dpi'],
  394. height/plt.rcParams['figure.dpi']),
  395. # note we need a linewidth to keep xkcd mode happy
  396. linewidth=8)
  397. ax = fig.subplots()
  398. for i, (name, dataset) in enumerate(datasets_.items()):
  399. dats = sorted((x,y) for x,y in dataset.items())
  400. ax.plot([x for x,_ in dats], [y for _,y in dats],
  401. formats_[i % len(formats_)],
  402. color=colors_[i % len(colors_)],
  403. label=','.join(k for k in name if k))
  404. # axes scaling
  405. if xlog:
  406. ax.set_xscale('symlog')
  407. ax.xaxis.set_minor_locator(mpl.ticker.NullLocator())
  408. if ylog:
  409. ax.set_yscale('symlog')
  410. ax.yaxis.set_minor_locator(mpl.ticker.NullLocator())
  411. # axes limits
  412. ax.set_xlim(
  413. xlim[0] if xlim[0] is not None
  414. else min(it.chain([0], (k
  415. for r in datasets_.values()
  416. for k, v in r.items()
  417. if v is not None))),
  418. xlim[1] if xlim[1] is not None
  419. else max(it.chain([0], (k
  420. for r in datasets_.values()
  421. for k, v in r.items()
  422. if v is not None))))
  423. ax.set_ylim(
  424. ylim[0] if ylim[0] is not None
  425. else min(it.chain([0], (v
  426. for r in datasets_.values()
  427. for _, v in r.items()
  428. if v is not None))),
  429. ylim[1] if ylim[1] is not None
  430. else max(it.chain([0], (v
  431. for r in datasets_.values()
  432. for _, v in r.items()
  433. if v is not None))))
  434. # axes ticks
  435. if x2:
  436. ax.xaxis.set_major_formatter(lambda x, pos:
  437. si2(x)+(xunits if xunits else ''))
  438. if xticklabels is not None:
  439. ax.xaxis.set_ticklabels(xticklabels)
  440. if xticks is None:
  441. ax.xaxis.set_major_locator(AutoMultipleLocator(2))
  442. elif isinstance(xticks, list):
  443. ax.xaxis.set_major_locator(mpl.ticker.FixedLocator(xticks))
  444. elif xticks != 0:
  445. ax.xaxis.set_major_locator(AutoMultipleLocator(2, xticks-1))
  446. else:
  447. ax.xaxis.set_major_locator(mpl.ticker.NullLocator())
  448. else:
  449. ax.xaxis.set_major_formatter(lambda x, pos:
  450. si(x)+(xunits if xunits else ''))
  451. if xticklabels is not None:
  452. ax.xaxis.set_ticklabels(xticklabels)
  453. if xticks is None:
  454. ax.xaxis.set_major_locator(mpl.ticker.AutoLocator())
  455. elif isinstance(xticks, list):
  456. ax.xaxis.set_major_locator(mpl.ticker.FixedLocator(xticks))
  457. elif xticks != 0:
  458. ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(xticks-1))
  459. else:
  460. ax.xaxis.set_major_locator(mpl.ticker.NullLocator())
  461. if y2:
  462. ax.yaxis.set_major_formatter(lambda x, pos:
  463. si2(x)+(yunits if yunits else ''))
  464. if yticklabels is not None:
  465. ax.yaxis.set_ticklabels(yticklabels)
  466. if yticks is None:
  467. ax.yaxis.set_major_locator(AutoMultipleLocator(2))
  468. elif isinstance(yticks, list):
  469. ax.yaxis.set_major_locator(mpl.ticker.FixedLocator(yticks))
  470. elif yticks != 0:
  471. ax.yaxis.set_major_locator(AutoMultipleLocator(2, yticks-1))
  472. else:
  473. ax.yaxis.set_major_locator(mpl.ticker.NullLocator())
  474. else:
  475. ax.yaxis.set_major_formatter(lambda x, pos:
  476. si(x)+(yunits if yunits else ''))
  477. if yticklabels is not None:
  478. ax.yaxis.set_ticklabels(yticklabels)
  479. if yticks is None:
  480. ax.yaxis.set_major_locator(mpl.ticker.AutoLocator())
  481. elif isinstance(yticks, list):
  482. ax.yaxis.set_major_locator(mpl.ticker.FixedLocator(yticks))
  483. elif yticks != 0:
  484. ax.yaxis.set_major_locator(mpl.ticker.MaxNLocator(yticks-1))
  485. else:
  486. ax.yaxis.set_major_locator(mpl.ticker.NullLocator())
  487. # axes labels
  488. if xlabel is not None:
  489. ax.set_xlabel(xlabel)
  490. if ylabel is not None:
  491. ax.set_ylabel(ylabel)
  492. if ggplot:
  493. ax.grid(sketch_params=None)
  494. if title is not None:
  495. ax.set_title(title)
  496. # pre-render so we can derive some bboxes
  497. fig.tight_layout()
  498. # it's not clear how you're actually supposed to get the renderer if
  499. # get_renderer isn't supported
  500. try:
  501. renderer = fig.canvas.get_renderer()
  502. except AttributeError:
  503. renderer = fig._cachedRenderer
  504. # add a legend? this actually ends up being _really_ complicated
  505. if legend == 'right':
  506. l_pad = fig.transFigure.inverted().transform((
  507. mpl.font_manager.FontProperties('small')
  508. .get_size_in_points()/2,
  509. 0))[0]
  510. legend_ = ax.legend(
  511. bbox_to_anchor=(1+l_pad, 1),
  512. loc='upper left',
  513. fancybox=False,
  514. borderaxespad=0)
  515. if ggplot:
  516. legend_.get_frame().set_linewidth(0)
  517. fig.tight_layout()
  518. elif legend == 'left':
  519. l_pad = fig.transFigure.inverted().transform((
  520. mpl.font_manager.FontProperties('small')
  521. .get_size_in_points()/2,
  522. 0))[0]
  523. # place legend somewhere to get its bbox
  524. legend_ = ax.legend(
  525. bbox_to_anchor=(0, 1),
  526. loc='upper right',
  527. fancybox=False,
  528. borderaxespad=0)
  529. # first make space for legend without the legend in the figure
  530. l_bbox = (legend_.get_tightbbox(renderer)
  531. .transformed(fig.transFigure.inverted()))
  532. legend_.remove()
  533. fig.tight_layout(rect=(0, 0, 1-l_bbox.width-l_pad, 1))
  534. # place legend after tight_layout computation
  535. bbox = (ax.get_tightbbox(renderer)
  536. .transformed(ax.transAxes.inverted()))
  537. legend_ = ax.legend(
  538. bbox_to_anchor=(bbox.x0-l_pad, 1),
  539. loc='upper right',
  540. fancybox=False,
  541. borderaxespad=0)
  542. if ggplot:
  543. legend_.get_frame().set_linewidth(0)
  544. elif legend == 'above':
  545. l_pad = fig.transFigure.inverted().transform((
  546. 0,
  547. mpl.font_manager.FontProperties('small')
  548. .get_size_in_points()/2))[1]
  549. # try different column counts until we fit in the axes
  550. for ncol in reversed(range(1, len(datasets_)+1)):
  551. legend_ = ax.legend(
  552. bbox_to_anchor=(0.5, 1+l_pad),
  553. loc='lower center',
  554. ncol=ncol,
  555. fancybox=False,
  556. borderaxespad=0)
  557. if ggplot:
  558. legend_.get_frame().set_linewidth(0)
  559. l_bbox = (legend_.get_tightbbox(renderer)
  560. .transformed(ax.transAxes.inverted()))
  561. if l_bbox.x0 >= 0:
  562. break
  563. # fix the title
  564. if title is not None:
  565. t_bbox = (ax.title.get_tightbbox(renderer)
  566. .transformed(ax.transAxes.inverted()))
  567. ax.set_title(None)
  568. fig.tight_layout(rect=(0, 0, 1, 1-t_bbox.height))
  569. l_bbox = (legend_.get_tightbbox(renderer)
  570. .transformed(ax.transAxes.inverted()))
  571. ax.set_title(title, y=1+l_bbox.height+l_pad)
  572. elif legend == 'below':
  573. l_pad = fig.transFigure.inverted().transform((
  574. 0,
  575. mpl.font_manager.FontProperties('small')
  576. .get_size_in_points()/2))[1]
  577. # try different column counts until we fit in the axes
  578. for ncol in reversed(range(1, len(datasets_)+1)):
  579. legend_ = ax.legend(
  580. bbox_to_anchor=(0.5, 0),
  581. loc='upper center',
  582. ncol=ncol,
  583. fancybox=False,
  584. borderaxespad=0)
  585. l_bbox = (legend_.get_tightbbox(renderer)
  586. .transformed(ax.transAxes.inverted()))
  587. if l_bbox.x0 >= 0:
  588. break
  589. # first make space for legend without the legend in the figure
  590. l_bbox = (legend_.get_tightbbox(renderer)
  591. .transformed(fig.transFigure.inverted()))
  592. legend_.remove()
  593. fig.tight_layout(rect=(0, 0, 1, 1-l_bbox.height-l_pad))
  594. bbox = (ax.get_tightbbox(renderer)
  595. .transformed(ax.transAxes.inverted()))
  596. legend_ = ax.legend(
  597. bbox_to_anchor=(0.5, bbox.y0-l_pad),
  598. loc='upper center',
  599. ncol=ncol,
  600. fancybox=False,
  601. borderaxespad=0)
  602. if ggplot:
  603. legend_.get_frame().set_linewidth(0)
  604. # compute another tight_layout for good measure, because this _does_
  605. # fix some things... I don't really know why though
  606. fig.tight_layout()
  607. plt.savefig(output, format='png' if png else 'svg', bbox_inches='tight')
  608. # some stats
  609. if not quiet:
  610. print('updated %s, %s datasets, %s points' % (
  611. output,
  612. len(datasets_),
  613. sum(len(dataset) for dataset in datasets_.values())))
  614. if __name__ == "__main__":
  615. import sys
  616. import argparse
  617. parser = argparse.ArgumentParser(
  618. description="Plot CSV files with matplotlib.",
  619. allow_abbrev=False)
  620. parser.add_argument(
  621. 'csv_paths',
  622. nargs='*',
  623. help="Input *.csv files.")
  624. parser.add_argument(
  625. '-o', '--output',
  626. required=True,
  627. help="Output *.svg/*.png file.")
  628. parser.add_argument(
  629. '--svg',
  630. action='store_true',
  631. help="Output an svg file. By default this is infered.")
  632. parser.add_argument(
  633. '--png',
  634. action='store_true',
  635. help="Output a png file. By default this is infered.")
  636. parser.add_argument(
  637. '-q', '--quiet',
  638. action='store_true',
  639. help="Don't print info.")
  640. parser.add_argument(
  641. '-b', '--by',
  642. action='append',
  643. type=lambda x: (
  644. lambda k,v=None: (k, v.split(',') if v is not None else ())
  645. )(*x.split('=', 1)),
  646. help="Group by this field. Can rename fields with new_name=old_name.")
  647. parser.add_argument(
  648. '-x',
  649. action='append',
  650. type=lambda x: (
  651. lambda k,v=None: (k, v.split(',') if v is not None else ())
  652. )(*x.split('=', 1)),
  653. help="Field to use for the x-axis. Can rename fields with "
  654. "new_name=old_name.")
  655. parser.add_argument(
  656. '-y',
  657. action='append',
  658. type=lambda x: (
  659. lambda k,v=None: (k, v.split(',') if v is not None else ())
  660. )(*x.split('=', 1)),
  661. help="Field to use for the y-axis. Can rename fields with "
  662. "new_name=old_name.")
  663. parser.add_argument(
  664. '-D', '--define',
  665. type=lambda x: (lambda k,v: (k, set(v.split(','))))(*x.split('=', 1)),
  666. action='append',
  667. help="Only include results where this field is this value. May include "
  668. "comma-separated options.")
  669. parser.add_argument(
  670. '-.', '--points',
  671. action='store_true',
  672. help="Only draw data points.")
  673. parser.add_argument(
  674. '-!', '--points-and-lines',
  675. action='store_true',
  676. help="Draw data points and lines.")
  677. parser.add_argument(
  678. '--colors',
  679. type=lambda x: [x.strip() for x in x.split(',')],
  680. help="Comma-separated hex colors to use.")
  681. parser.add_argument(
  682. '--formats',
  683. type=lambda x: [x.strip().replace('0',',') for x in x.split(',')],
  684. help="Comma-separated matplotlib formats to use. Allows '0' as an "
  685. "alternative for ','.")
  686. parser.add_argument(
  687. '-W', '--width',
  688. type=lambda x: int(x, 0),
  689. help="Width in pixels. Defaults to %r." % WIDTH)
  690. parser.add_argument(
  691. '-H', '--height',
  692. type=lambda x: int(x, 0),
  693. help="Height in pixels. Defaults to %r." % HEIGHT)
  694. parser.add_argument(
  695. '-X', '--xlim',
  696. type=lambda x: tuple(
  697. dat(x) if x.strip() else None
  698. for x in x.split(',')),
  699. help="Range for the x-axis.")
  700. parser.add_argument(
  701. '-Y', '--ylim',
  702. type=lambda x: tuple(
  703. dat(x) if x.strip() else None
  704. for x in x.split(',')),
  705. help="Range for the y-axis.")
  706. parser.add_argument(
  707. '--xlog',
  708. action='store_true',
  709. help="Use a logarithmic x-axis.")
  710. parser.add_argument(
  711. '--ylog',
  712. action='store_true',
  713. help="Use a logarithmic y-axis.")
  714. parser.add_argument(
  715. '--x2',
  716. action='store_true',
  717. help="Use base-2 prefixes for the x-axis.")
  718. parser.add_argument(
  719. '--y2',
  720. action='store_true',
  721. help="Use base-2 prefixes for the y-axis.")
  722. parser.add_argument(
  723. '--xticks',
  724. type=lambda x: int(x, 0) if ',' not in x
  725. else [dat(x) for x in x.split(',')],
  726. help="Ticks for the x-axis. This can be explicit comma-separated "
  727. "ticks, the number of ticks, or 0 to disable.")
  728. parser.add_argument(
  729. '--yticks',
  730. type=lambda x: int(x, 0) if ',' not in x
  731. else [dat(x) for x in x.split(',')],
  732. help="Ticks for the y-axis. This can be explicit comma-separated "
  733. "ticks, the number of ticks, or 0 to disable.")
  734. parser.add_argument(
  735. '--xunits',
  736. help="Units for the x-axis.")
  737. parser.add_argument(
  738. '--yunits',
  739. help="Units for the y-axis.")
  740. parser.add_argument(
  741. '--xlabel',
  742. help="Add a label to the x-axis.")
  743. parser.add_argument(
  744. '--ylabel',
  745. help="Add a label to the y-axis.")
  746. parser.add_argument(
  747. '--xticklabels',
  748. type=lambda x: [x.strip() for x in x.split(',')],
  749. help="Comma separated xticklabels.")
  750. parser.add_argument(
  751. '--yticklabels',
  752. type=lambda x: [x.strip() for x in x.split(',')],
  753. help="Comma separated yticklabels.")
  754. parser.add_argument(
  755. '-t', '--title',
  756. help="Add a title.")
  757. parser.add_argument(
  758. '-l', '--legend',
  759. nargs='?',
  760. choices=['above', 'below', 'left', 'right'],
  761. const='right',
  762. help="Place a legend here.")
  763. parser.add_argument(
  764. '--dark',
  765. action='store_true',
  766. help="Use the dark style.")
  767. parser.add_argument(
  768. '--ggplot',
  769. action='store_true',
  770. help="Use the ggplot style.")
  771. parser.add_argument(
  772. '--xkcd',
  773. action='store_true',
  774. help="Use the xkcd style.")
  775. parser.add_argument(
  776. '--font',
  777. type=lambda x: [x.strip() for x in x.split(',')],
  778. help="Font family for matplotlib.")
  779. parser.add_argument(
  780. '--font-size',
  781. help="Font size for matplotlib. Defaults to %r." % FONT_SIZE)
  782. parser.add_argument(
  783. '--background',
  784. help="Background color to use.")
  785. sys.exit(main(**{k: v
  786. for k, v in vars(parser.parse_intermixed_args()).items()
  787. if v is not None}))