#!/usr/bin/env python # -*- coding: utf-8 -*- import csv import glob import itertools import math import matplotlib.pyplot as plt import sys def get_dashes(): """Generator for plot line dash patterns.""" dash = 2.0 space = dot = 0.75 yield [] # Solid yield [dash, space] # Dashed yield [dot, space] # Dotted # Dash-dots, with increasing number of dots for each line for i in itertools.count(2): yield [dash, space] + [dot, space] * (i - 1) def plot(in_file, out_filename, x_label, y_max=None): fig_height = 4.0 dashes = get_dashes() markers = itertools.cycle( ['o', 's', 'v', 'D', '*', 'p', 'P', 'h', 'X', '+', 'x']) reader = csv.reader(in_file, delimiter='\t') header = next(reader) cols = [x for x in zip(*list(reader))] plt.clf() fig = plt.figure(figsize=(fig_height * math.sqrt(2), fig_height)) ax = fig.add_subplot(111) ax.set_xlabel(x_label) ax.set_ylabel(u'Time (μs)') if y_max is not None: ax.set_ylim([0.0, y_max]) ax.grid(linewidth=0.25, linestyle=':', color='0', dashes=[0.2, 1.6]) ax.tick_params(axis='both', width=0.75) x = cols[0] for i, y in enumerate(cols[1::]): ax.plot(x, list(map(float, y)), label=header[i + 1], marker=next(markers), dashes=next(dashes), markersize=3.0, linewidth=1.0) plt.legend(loc='upper left') plt.savefig(out_filename, bbox_inches='tight', pad_inches=0.025) plt.close() sys.stderr.write('wrote {}\n'.format(out_filename)) def plot_section(x_label, plots): # Get maximum value for all Y axes y_max = 0.0 for p in plots: name = p[0] filename = name + '.txt' with open(filename, 'r') as in_file: reader = csv.reader(in_file, delimiter='\t') next(reader) for row in reader: y_max = max(y_max, *map(float, row[1::])) html = '' for p in plots: name = p[0] caption = p[1] filename = name + '.txt' out_filename = filename.replace('.txt', '.svg') with open(filename, 'r') as in_file: plot(in_file, filename.replace('.txt', '.svg'), x_label, y_max) html += ''.join([' \n']) return html if __name__ == '__main__': files = glob.glob('*.txt') html = '''