A Python tool for analyzing UV-Vis spectroscopy data by converting wavelengths to wavenumbers, fitting multiple Gaussian peaks, and visualizing the results.
- Automatic unit conversion: Converts wavelength (nm) to wavenumber (cm⁻¹) for fitting
- Multiple Gaussian fitting: Fit any number of Gaussian peaks to your spectrum
- Flexible initial conditions: Specify initial peak positions or use automatic distribution
- Range restriction: Analyze only a specific wavelength range of your spectrum
- Dual visualization: View results in either wavelength (nm) or wavenumber (cm⁻¹) units
- Fit quality metrics: Get R² and RMSE values for fit assessment
- Export capabilities: Save high-resolution plots of your results
-
Clone or download this repository to your local machine
-
Install Python dependencies:
pip install -r requirements.txt
Or install manually:
pip install numpy pandas matplotlib scipy
The tool expects a CSV file with two columns (no headers):
- Column 1: Wavelength in nanometers (nm)
- Column 2: Absorbance values
Example:
1100.011719,0.007490207
1099.009399,-0.019240458
1098.006226,-0.004369807
...
python uvvis_gaussian_fit.py <csv_file> <n_gaussians> [options]csv_file: Path to your CSV data filen_gaussians: Number of Gaussian peaks to fit (integer)
--initial_peaks PEAK1 PEAK2 ...: Specify initial peak positions in wavelength (nm)--initial_peaks_cm PEAK1 PEAK2 ...: Specify initial peak positions in wavenumber (cm⁻¹)--range_nm MIN MAX: Restrict analysis to wavelength range (nm)--display_unit {nm,cm}: Choose display unit for final plot (default: nm)--save FILENAME: Save plot to file (PNG, PDF, etc.)
python uvvis_gaussian_fit.py spectrum.csv 3Fits 3 Gaussians with automatically distributed initial peak positions.
python uvvis_gaussian_fit.py spectrum.csv 2 --initial_peaks 400 500Fits 2 Gaussians with initial centers at 400 and 500 nm.
python uvvis_gaussian_fit.py spectrum.csv 2 --initial_peaks_cm 25000 20000Fits 2 Gaussians with initial centers at 25,000 and 20,000 cm⁻¹.
python uvvis_gaussian_fit.py spectrum.csv 3 --range_nm 300 700Only analyzes data between 300-700 nm, fitting 3 Gaussians.
python uvvis_gaussian_fit.py spectrum.csv 2 --display_unit cmShows the final plot with wavenumber (cm⁻¹) on the x-axis.
python uvvis_gaussian_fit.py spectrum.csv 3 --save my_spectrum_fit.pngSaves the result plot as a high-resolution PNG file.
python uvvis_gaussian_fit.py spectrum.csv 3 \
--initial_peaks 360 450 625 \
--range_nm 350 650 \
--display_unit nm \
--save spectrum_analysis.pdfThe tool provides detailed output including:
- R²: Coefficient of determination (closer to 1.0 = better fit)
- RMSE: Root mean square error (smaller = better fit)
For each fitted Gaussian:
- Center: Peak position in both cm⁻¹ and nm
- Amplitude: Peak height (absorbance units)
- Width: Peak width in both cm⁻¹ and nm
- Original data points
- Sum of all Gaussians (fitted curve)
- Individual Gaussian components
- Fit quality metrics in plot title
- Start with fewer Gaussians (2-3) and increase if needed
- Too many Gaussians may lead to overfitting
- Use physical/chemical knowledge to guide the choice
- Examine your spectrum visually to identify approximate peak positions
- Easy method: Use
--initial_peakswith wavelengths in nm (e.g., 400 500 600) - Advanced method: Use
--initial_peaks_cmwith wavenumbers in cm⁻¹ - Conversion: wavenumber = 10,000,000 / wavelength_nm (e.g., 500 nm → 20,000 cm⁻¹)
- Exclude noisy regions at spectrum edges
- Focus on the region of interest for your analysis
- Ensure sufficient data points for reliable fitting
- Try different initial peak positions
- Adjust the number of Gaussians
- Check data quality and remove outliers if necessary
- Consider if Gaussian peaks are appropriate for your system
-
Examine your data:
# Quick visual inspection python -c "import pandas as pd; import matplotlib.pyplot as plt; data=pd.read_csv('spectrum.csv', header=None); plt.plot(data[0], data[1]); plt.show()"
-
Start with basic fit:
python uvvis_gaussian_fit.py spectrum.csv 2
-
Refine based on results:
python uvvis_gaussian_fit.py spectrum.csv 3 --initial_peaks 25000 20000 15000
-
Final analysis with range restriction:
python uvvis_gaussian_fit.py spectrum.csv 3 \ --initial_peaks 25000 20000 15000 \ --range_nm 400 800 \ --save final_analysis.png
- Wavelength to Wavenumber: ν̃ = 10⁷/λ (cm⁻¹ = 10⁷ nm·cm⁻¹ / nm)
- Wavenumber to Wavelength: λ = 10⁷/ν̃ (nm = 10⁷ nm·cm⁻¹ / cm⁻¹)
Each Gaussian peak is defined as:
G(ν̃) = A × exp(-(ν̃ - ν̃₀)²/(2σ²))
Where:
- A = amplitude (peak height)
- ν̃₀ = center position (cm⁻¹)
- σ = width parameter (cm⁻¹)
The total spectrum is the sum of individual Gaussians:
S(ν̃) = Σ Aᵢ × exp(-(ν̃ - ν̃₀ᵢ)²/(2σᵢ²))
uvvis_convertfit/
├── uvvis_gaussian_fit.py # Main analysis script
├── requirements.txt # Python dependencies
├── examples.py # Usage examples
├── README.md # This documentation
└── spectrum.csv # Example data file
If you encounter issues or have suggestions for improvements:
- Check that your CSV file format matches the expected structure
- Verify all dependencies are installed correctly
- Try simpler cases first (fewer Gaussians, no range restrictions)
This tool is provided as-is for scientific and educational use.