Advertisement
Guest User

Untitled

a guest
May 5th, 2024
21
0
2 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. # updates the plot
  5. def update_plot_and_trendlines(data_entry, fig):
  6.     axes = fig.subplots(2,3)
  7.    
  8.     # Store plot styling for each mass
  9.     plot_kwargs = {  
  10.         "3 amu":     {"color": "blue"},
  11.         "4 amu":     {"color": "red"},
  12.         "4/3 Ratio": {"color": "purple"},
  13.         "2 amu":     {"edgecolor": "black", "facecolor": "white", "linewidth": 1},
  14.         "40 amu":    {"color": "magenta"},
  15.         "5 amu":     {"color": "gray"}
  16.     }
  17.  
  18.     # loop to plot masses on the top row
  19.     for ax, mass in zip(axes[0], ['3 amu', '4 amu', '4/3 Ratio']):
  20.  
  21.         # dress up the plot
  22.         ax.set_xlabel('Time (sec)')
  23.         if mass == '4/3 Ratio':
  24.             ax.set_ylabel('4/3 Ratio × 1000')
  25.         else:
  26.             ax.set_ylabel('Intensity (A)')
  27.         ax.set_title(mass)
  28.         ax.grid(True, zorder=1)
  29.  
  30.         # define x and y
  31.         x = data_entry.raw_data[mass]['time_sec']
  32.         y = data_entry.raw_data[mass][mass]
  33.  
  34.         # get active indices from data_status[mass]
  35.         active_indices = data_entry.data_status[mass].to_numpy().flatten()
  36.  
  37.         # draw trendline/average from active indices
  38.         if mass == '4/3 Ratio':
  39.             average_y = np.mean(y[active_indices==1])
  40.             trend_x   = [x.min(), x.max()]
  41.             trend_y   = [average_y, average_y]
  42.             ax.plot(trend_x, trend_y, color="black", zorder=2)
  43.         else: # 3 amu and 4 amu
  44.             trend   = np.polyfit(x[active_indices==1], y[active_indices==1], 1)
  45.             trend_x = [x.min(), x.max()]
  46.             trend_y = np.polyval(trend, trend_x)
  47.             ax.plot(trend_x, trend_y, color="black", zorder=2)
  48.          
  49.         ax.scatter(x[active_indices==1], y[active_indices==1], **plot_kwargs[mass], zorder=4)
  50.         ax.scatter(x[active_indices==0], y[active_indices==0], marker='x', color='gray', zorder=4)
  51.  
  52.     # loop to plot masses in the bottom row
  53.     for ax, mass in zip(axes[1], ['2 amu', '40 amu', '5 amu']):
  54.  
  55.         # dress up the plot
  56.         ax.set_xlabel('Time (sec)')
  57.         ax.set_ylabel('Intensity (A)')
  58.         ax.set_title(mass)
  59.         ax.grid(True, zorder=1)
  60.  
  61.         # define x and y
  62.         x = data_entry.raw_data[mass]['time_sec']
  63.         y = data_entry.raw_data[mass][mass]
  64.  
  65.         # get active and inactive indices
  66.         active_indices   = data_entry.data_status[mass].to_numpy().flatten()
  67.  
  68.         # draw trendline/average from active indices
  69.         if mass == '5 amu':
  70.             average_y = np.mean(y[active_indices==1]) if np.any(active_indices) else np.nan
  71.             trend_x   = [x.min(), x.max()]
  72.             trend_y   = [average_y, average_y]
  73.             ax.plot(trend_x, trend_y, color="black", zorder=2)
  74.         else:
  75.             trend   = np.polyfit(x[active_indices==1], y[active_indices==1], 1)
  76.             trend_x = [x.min(), x.max()]
  77.             trend_y = np.polyval(trend, trend_x)
  78.             ax.plot(trend_x, trend_y, color="black", zorder=2)
  79.  
  80.         # draw active and inactive plots
  81.         ax.scatter(x[active_indices==1], y[active_indices==1], **plot_kwargs[mass], zorder=3)
  82.         ax.scatter(x[active_indices==0], y[active_indices==0], marker='x', color='gray', zorder=3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement