diff --git a/gr_grc/scripts/plot_data.py b/gr_grc/scripts/plot_data.py index c4d7c46758586c519edb52312b5551b0c432c3fe..c39f21093af74b5b8e539ceaacbbad23fd0356c4 100644 --- a/gr_grc/scripts/plot_data.py +++ b/gr_grc/scripts/plot_data.py @@ -1,7 +1,14 @@ +#!/usr/bin/env python2.7 +# -*- encoding: utf-8 -*- +from __future__ import unicode_literals + import numpy as np import matplotlib.pyplot as plt import yaml import sys +import matplotlib.patches as mpatches +import matplotlib.lines as mlines +from matplotlib.legend_handler import HandlerLine2D # http://mple.m-artwork.eu/home/posts?offset=10 @@ -9,31 +16,87 @@ if len(sys.argv) != 2: print("Usage: " + sys.argv[0] + " /path/to/data.yml") sys.exit(1) - data = None +fig,ax = plt.subplots() +# x range 0-100 +x_range = np.arange(0,100,1) +# first one and last one +real_transmission_time = [] +# count only ones +transmission_time = [] + with open(sys.argv[1], 'r') as stream: data = yaml.load(stream) -fig,ax = plt.subplots() - -x = np.arange(0,101,1) -values = [] for i, var in enumerate(data['signal_blocks']): - values.append(var['real_transmission_time']) - ax.plot(i+1,var['real_transmission_time'], linestyle="None", marker=".", color="red") + # add values to array for later processing + real_transmission_time.append(var['real_transmission_time']) + transmission_time.append(var['transmission_time']) -y_mean = [np.mean(values) for i in x] -y_med = [np.median(values) for i in x] -print(y_mean) -print(y_med) +real_y_mean = [np.mean(real_transmission_time) for i in x_range] +real_y_med = [np.median(real_transmission_time) for i in x_range] -mean_line = ax.plot(x,y_mean, label='Mittwelwert\t' + str(round(y_mean[0],2)) + 'ms', linestyle='--') -mean_line = ax.plot(x,y_med, label='Median\t' + str(round(y_med[0],2)) + 'ms', linestyle='-') +y_mean = [np.mean(transmission_time) for i in x_range] +y_med = [np.median(transmission_time) for i in x_range] -legend = ax.legend(loc='upper right') +real_median_set = False +median_set = False +for i, var in enumerate(data['signal_blocks']): + # plot data points + if var['real_transmission_time'] == np.median(real_transmission_time) and \ + real_median_set is False: + real_median_plot = ax.plot(i+1, var['real_transmission_time'], + linestyle="None", marker=".", color="red", + label="Transmission [median]") + real_median_set = True + else: + ax.plot(i+1, var['real_transmission_time'], + linestyle="None", marker="x", color="red") + + if var['transmission_time'] == np.median(transmission_time) and \ + median_set is False: + median_plot = ax.plot(i+1, var['transmission_time'], linestyle="None", + marker=".", color="blue", + label="Tastgrad [median]") + median_set = True + else: + ax.plot(i+1, var['transmission_time'], linestyle="None", + marker="x", color="blue") + +ax.plot(x_range,y_mean, linestyle='--', color="blue") +ax.plot(x_range,real_y_mean, linestyle='--', color="red") plt.xlabel("Versuch") plt.ylabel("Frequenzbelegungszeit [ms]") -plt.title(data['tag']) +#plt.title(data['tag']) + +# add tick lines for x and y +ax.yaxis.grid(True, which='major') +ax.yaxis.grid(True, which='minor') +ax.xaxis.grid(True, which='major') +ax.xaxis.grid(True, which='minor') +# Create custom artists +tastgrad_legend = plt.Line2D((0,1), (0,0), + color='blue', linestyle="", marker="x") +uebertragung_legend = plt.Line2D((0,1), (0,0), + color='red', linestyle="", marker="x") +median_legend = plt.Line2D((0,1), (0,0), + color='black', marker='o', linestyle='') +avg_legend = plt.Line2D((0,1), (0,0), + color='black', linestyle="--") + +# Create legend from custom artist/label lists +ax.legend([uebertragung_legend,tastgrad_legend,median_legend, avg_legend], + ['Ãœbertragung','Tastgrad','Median', 'Durchschnitt'], + numpoints=1, loc="best") + +ticks = [ round(x, 1) for x in list(plt.yticks()[0]) ] +if 0 in ticks: + ticks.remove(0) + +ax.set_yticks(ticks + [y_mean[0], real_y_mean[0]], minor=False) +for spine in ['top', 'right']: + ax.spines[spine].set_visible(False) +plt.savefig('figure.pgf') plt.show()