Advertisement
phirippu

Plot CSV with averaging

Nov 13th, 2015
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. #!/usr/bin/python3.5
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import sys
  5.  
  6. #print (sys.argv)
  7. #quit()
  8.  
  9. if (len(sys.argv) > 1):
  10.     fname = sys.argv[1]
  11. else:
  12.     print("Usage: "+sys.argv[0]+" datafile Xcolumn[default 0] Ycolumn[default 1] NumLinestoSkip[default 0] NumAverage[default 1, no averaging]")
  13.     quit()
  14.  
  15. if (len(sys.argv) > 3):
  16.     xcol = int(sys.argv[2])
  17.     ycol = int(sys.argv[3])
  18. else:
  19.     xcol = 0
  20.     ycol = 1
  21.  
  22. if (len(sys.argv) > 4):
  23.     skip = int(sys.argv[4])
  24. else:
  25.     skip = 0
  26.  
  27. if (len(sys.argv) > 5):
  28.     sum_count = int(sys.argv[5])
  29. else:
  30.     sum_count = 1
  31.  
  32.  
  33. x, y = np.loadtxt(fname, skiprows=skip, usecols=(xcol, ycol), unpack=True)
  34. xx = []
  35. yy = []
  36.  
  37. for j in range(0, len(x)//sum_count):
  38.     xx.append(x[j*sum_count])
  39.     yy.append(np.average(y[j*sum_count:j*sum_count+sum_count]))
  40.  
  41. fig = plt.figure()
  42.  
  43. ax1 = fig.add_subplot(111)
  44.  
  45. ax1.set_title(fname+" data")    
  46. ax1.set_xlabel('X')
  47. ax1.set_ylabel('Y')
  48.  
  49. ax1.plot(xx, yy, color='r', marker='+', markeredgecolor='black', label='the data')
  50.  
  51. leg = ax1.legend()
  52.  
  53. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement