Guest User

plot_diff

a guest
Jan 4th, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.66 KB | None | 0 0
  1. import numpy as np
  2. import math
  3. import matplotlib.pyplot as plt
  4. import matplotlib.animation as animation
  5. import sys
  6.  
  7. #python3 plot_diff.py name_file_IMD name_file_xraydb
  8. #examples:
  9. #python3 plot_diff.py Pt_substrate.dat Pt.dat
  10. #python3 plot_diff.py Pd_substrate.dat Pd.dat
  11. n = len(sys.argv)
  12. print("Total arguments passed:", n)
  13. if n!=3:
  14.     print("2 args needed: name_file_IMD name_file_xraydb")
  15. else:
  16.     compteur=1
  17.    
  18.     with open(sys.argv[1]) as ffirst:
  19.         for line in ffirst:
  20.             if compteur==7:
  21.                 linestartswithlist = line.strip().split(" ")
  22.                 linestartswith = linestartswithlist[0]
  23.                 #print(linestartswith)
  24.             compteur+=1
  25.             if line[0]=="#":
  26.                 ii = line.strip().split("=")
  27.                 if(ii[0]=="#theta_step"):
  28.                     theta_step_imd = float(ii[1])
  29.                 if(ii[0]=="#theta_min"):
  30.                     theta_min_imd = float(ii[1])      
  31.                            
  32.     #print(float(theta_min_imd),float(theta_step_imd))
  33.    
  34.     with open(sys.argv[2]) as fsecond:
  35.         for line in fsecond:
  36.             if line[0]=="#":
  37.                 ii = line.strip().split("=")
  38.                 if(ii[0]=="#theta_step"):
  39.                     theta_step_xraydb = float(ii[1])
  40.                 if(ii[0]=="#theta_min"):
  41.                     theta_min_xraydb = float(ii[1])      
  42.                          
  43.     #print(float(theta_min_xraydb),float(theta_step_xraydb))  
  44.  
  45.            
  46.     with open(sys.argv[1]) as f:
  47.         lines = (line for line in f if not line.startswith("#") and not line.startswith(linestartswith))
  48.         a=np.loadtxt(lines)
  49.  
  50.     #print(a)
  51.  
  52.  
  53.     sizea=len(a[0])
  54.  
  55.     Xdeux = np.zeros(sizea)
  56.     Ydeux = np.zeros(sizea)
  57.  
  58.     i=0
  59.     incr=theta_min_imd
  60.     for elt in a[0]:  
  61.         #print(incr,elt)  
  62.         Xdeux[i]=incr
  63.         Ydeux[i]=elt
  64.         incr+=theta_step_imd
  65.         i+=1
  66.  
  67.     with open(sys.argv[2]) as ff:
  68.         lines = (line for line in ff if not line.startswith("#"))
  69.         b=np.loadtxt(lines)
  70.  
  71.     sizeb=len(b[0])
  72.  
  73.     Xdeuxb = np.zeros(sizeb)
  74.     Ydeuxb = np.zeros(sizeb)
  75.  
  76.     i=0
  77.     incr=theta_min_xraydb
  78.     for elt in b[0]:  
  79.         #print(incr,elt)  
  80.         Xdeuxb[i]=incr
  81.         Ydeuxb[i]=elt
  82.         incr+=theta_step_xraydb
  83.         i+=1
  84.  
  85.  
  86.     fig, ax = plt.subplots()
  87.  
  88.     line, = ax.plot(Xdeux,Ydeux,"r")
  89.  
  90.     line2, = ax.plot(Xdeuxb,Ydeuxb,"b")
  91.  
  92.     jj=0
  93.     def animate(i):
  94.         #print(i)
  95.         j=0
  96.         incr=theta_min_imd
  97.         for elt in a[i]:  
  98.             #print(incr,elt)  
  99.             Xdeux[j]=incr
  100.             Ydeux[j]=elt
  101.             incr+=theta_step_imd
  102.             j+=1
  103.         line.set_ydata(Ydeux)  # update the data.
  104.        
  105.        
  106.        
  107.        
  108.         jj=0
  109.         incr=theta_min_xraydb
  110.         for elt in b[i]:  
  111.             #print(incr,elt)  
  112.             Xdeuxb[jj]=incr
  113.             Ydeuxb[jj]=elt
  114.             incr+=theta_step_xraydb
  115.             jj+=1    
  116.            
  117.         line2.set_ydata(Ydeuxb)
  118.        
  119.         return line, line2
  120.  
  121.  
  122.     #ani = animation.FuncAnimation(fig, animate, interval=20, blit=True, save_count=50)
  123.     ani = animation.FuncAnimation(fig, animate, frames=200, interval=100, blit=True)
  124.  
  125.     # To save the animation, use e.g.
  126.     #
  127.     # ani.save("movie.mp4")
  128.     #
  129.     # or
  130.     #
  131.     # writer = animation.FFMpegWriter(
  132.     #     fps=15, metadata=dict(artist='Me'), bitrate=1800)
  133.     # ani.save("movie.mp4", writer=writer)
  134.  
  135.     plt.title(linestartswith)
  136.     plt.xlabel("Theta degree")
  137.     plt.ylabel("Reflectivity")
  138.  
  139.     plt.legend([line,line2],["IMD","XrayDB"])
  140.  
  141.     plt.show()
  142.  
Advertisement
Add Comment
Please, Sign In to add comment