# -*- coding: iso-8859-1 -*- # script liveDisplay.py ''' read data from file into 1d-array and plot - data vs. ordinal number - frequency distribution This example also illustrates how to read an argument from the command line .. author:: Guenter Quast ''' # pyhton2 - python3 compatibility from __future__ import division from __future__ import absolute_import from __future__ import print_function from __future__ import unicode_literals # bg import sys import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as anim #import Histogram as h #read columns from file: if len(sys.argv)==2: infile=sys.argv[1] else: infile='liveData.dat' print('*==* script ' + sys.argv[0] + ' reading from file ' + infile) # plot the results (as live-update with matplotlib) fig=plt.figure(figsize=(15.,7.5)) axg=fig.add_subplot(1,2,1) axh=fig.add_subplot(1,2,2) def liveplot(n): # read data from file and plot frequency distribution # ---------------------------------------------------- A=np.loadtxt(infile,unpack=True) # (re-)read from file # plot series x=np.array(range(len(A)))+1. axg.clear() axg.plot(x, A,'o--', color='b') axg.set_xlim(0.,len(A)+1.) axg.set_xlabel('Nr. der Messung',size='x-large') axg.set_ylabel('Messwert',size='x-large') # plot as histogram axh.clear() axh.hist(A, 10, color='b') axh.set_xlim(np.amin(A),np.amax(A)) axh.set_title(u'Häufigkeitsverteilung',size='x-large') axh.set_xlabel('Messwert',size='x-large') axh.set_ylabel(u'Häufigkeit (frequency)',size='x-large') nrep=1000 # number of times to repeat interval=1000 # time between updates in ms ani=anim.FuncAnimation(fig, liveplot, nrep, interval=interval) plt.show() # print statistical information at the end A=np.loadtxt(infile,unpack=True) # (re-)read from file print("Mittelwert: %.3f" % np.mean(A) ) print("Standardabweichung: %.3f" % np.std(A) ) print("Unsicherheit auf Mittelwert: %.3f" % (np.std(A)/np.sqrt(len(A))) )