Advertisement
UF6

Improved Gaussian Fit That Can Take Columns

UF6
Nov 8th, 2023
928
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | Source Code | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from scipy.stats import norm
  5.  
  6. # Replace 'B5 segmentSummary (1).csv' with the actual CSV file path
  7. file_path = 'B5 segmentSummary (1).csv'
  8.  
  9. # Define the range of column positions you want to analyze (columns 7 through 10)
  10. start_column_position = 6  # Corresponds to column 7
  11. end_column_position = 9   # Corresponds to column 10
  12.  
  13. # Read the CSV file into a Pandas DataFrame
  14. df = pd.read_csv(file_path)
  15.  
  16. # Select columns 7 through 10 for analysis
  17. selected_columns = df.iloc[:, start_column_position:end_column_position + 1]
  18.  
  19. # Create a figure with subplots for each Gaussian distribution
  20. plt.figure(figsize=(12, 6))
  21.  
  22. # Define the x-axis range for the plots
  23. x_range = np.linspace(selected_columns.min().min(), selected_columns.max().max(), 1000)
  24.  
  25. # Iterate through the selected columns and analyze them
  26. for i, col in enumerate(selected_columns.columns):
  27.     plt.subplot(2, 2, i + 1)  # Create subplots in a 2x2 grid
  28.     target_column = selected_columns[col]
  29.     mean = target_column.mean()
  30.     std_dev = target_column.std()
  31.    
  32.     # Fit a Gaussian distribution
  33.     fitted_data = norm.pdf(x_range, mean, std_dev)
  34.    
  35.     # Plot the Gaussian distribution
  36.     plt.plot(x_range, fitted_data, 'b-', linewidth=2, label='Fitted Gaussian')
  37.    
  38.     # Plot the original data as a histogram
  39.     plt.hist(target_column, bins=30, density=True, alpha=0.7, color='gray', label='Data Histogram')
  40.    
  41.     # Add labels and title with the target column name
  42.     plt.xlabel('Values')
  43.     plt.ylabel('Frequency')
  44.     plt.title(f'Gaussian Fit for {col}')
  45.    
  46.     # Show the mean shift
  47.     plt.axvline(mean, color='red', linestyle='dashed', linewidth=2, label='Mean Shift')
  48.    
  49.     # Add a legend
  50.     plt.legend()
  51.  
  52. # Adjust layout
  53. plt.tight_layout()
  54.  
  55. # Show the plot
  56. plt.show()
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement