# Comparison of Gaussian and Lorentz profiles

Figure 2.4 from Chapter 2 of *Interstellar and Intergalactic Medium* by Ryden & Pogge, 2021, 
Cambridge University Press.

Example of Gaussian and Lorentz profile with the same width and normalized to the same area.

Profiles are computed in situ.


```python
%matplotlib inline

import math
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, LogLocator, NullFormatter

import warnings
warnings.filterwarnings('ignore',category=UserWarning, append=True)
```

## Standard Plot Format

Setup the standard plotting format and make the plot. Fonts and resolution adopted follow CUP style.


```python
figName = 'Fig2_4'

# graphic aspect ratio = width/height

aspect = 4.0/3.0 # 4:3

# Text width in inches - don't change, this is defined by the print layout

textWidth = 6.0 # inches

# output format and resolution

figFmt = 'png'
dpi = 600

# Graphic dimensions 

plotWidth = dpi*textWidth
plotHeight = plotWidth/aspect
axisFontSize = 10
labelFontSize = 6
lwidth = 0.5
axisPad = 5
wInches = textWidth 
hInches = wInches/aspect

# Plot filename

plotFile = f'{figName}.{figFmt}'

# LaTeX is used throughout for markup of symbols, Times-Roman serif font

plt.rc('text', usetex=True)
plt.rc('font', **{'family':'serif','serif':['Times-Roman'],'weight':'bold','size':'16'})

# Font and line weight defaults for axes

matplotlib.rc('axes',linewidth=lwidth)
matplotlib.rcParams.update({'font.size':axisFontSize})

# axis and label padding

plt.rcParams['xtick.major.pad'] = f'{axisPad}'
plt.rcParams['ytick.major.pad'] = f'{axisPad}'
plt.rcParams['axes.labelpad'] = f'{axisPad}'
```

## Functions for Gaussian and Lorentz Profiles

Gaussian:
\begin{equation}
  G(x,\sigma) = \frac{1}{\sigma\sqrt{2\pi}} \exp\left(-\frac{x^2}{2\sigma^2}\right)
\end{equation}

Lorentz:
\begin{equation}
  L(x,\gamma) = \frac{4\gamma}{16\pi^2x^2 + \gamma^2}
\end{equation}

The two profiles will be plotted normalized to the same area given the same FWHM:
 * Gaussian: $\sigma = FWHM/ 2\sqrt{2\ln 2}$
 * Lorentz: $\gamma = 2\pi FWHM$ 

 
This is done in the code for G(x,FWHM) in the following cell.


```python
# Compute a Gaussian profile

def G(x,fwhm):
    sigma = fwhm/(2.0*math.sqrt(2.0*math.log(2.0)))
    xarg = x/sigma
    G = np.exp(-0.5*xarg*xarg)/np.sqrt(np.pi)*sigma
    return G

# Compute a Lorentz Profile

def L(x,fwhm):
    gamma = 2.0*math.pi*fwhm
    xarg = 4*math.pi*x
    L = 4*gamma/(xarg*xarg + gamma*gamma)
    return L

# Calculation Limits

nuMin = -7.0
nuMax = 7.0
nuStep = 0.01
fwhm = 2.0
yMin = -0.01
yMax = 0.5

# Do the calculation 

numPts = int((nuMax-nuMin)/nuStep)+1
nu = np.linspace(nuMin,nuMax,num=numPts)
gauss = G(nu,fwhm)
lorentz = L(nu,fwhm)
```

### Make the Plot

The Lorentz profiles is a solid line and the Gaussian profile is a dashed line.


```python
fig,ax = plt.subplots()

fig.set_dpi(dpi)
fig.set_size_inches(wInches,hInches,forward=True)

ax.tick_params('both',length=6,width=lwidth,which='major',direction='in',top='on',right='on')
ax.tick_params('both',length=3,width=lwidth,which='minor',direction='in',top='on',right='on')

plt.xlim(nuMin,nuMax)
ax.xaxis.set_major_locator(MultipleLocator(5))
ax.xaxis.set_minor_locator(MultipleLocator(1))
plt.xlabel(r'$\nu-\nu_{ul}$',fontsize=axisFontSize)

plt.ylim(yMin,yMax)
ax.yaxis.set_major_locator(MultipleLocator(0.1))
ax.yaxis.set_minor_locator(MultipleLocator(0.02))
plt.ylabel(r'$\Phi_\nu$',fontsize=axisFontSize)

# plot the curves

plt.plot(nu,lorentz,'-',color='black',lw=1,zorder=10)
plt.plot(nu,gauss,'--',color='black',lw=1,zorder=9)

# Labels

plt.text(0.5,0.45,'Gaussian',fontsize=axisFontSize,color='black')
plt.text(2.6,0.05,'Lorentz',fontsize=axisFontSize,color='black')

# plot and file

plt.plot()
plt.savefig(plotFile,bbox_inches='tight',facecolor='white')
```

d94cafe7e9dc00580dff7a58642840e373313477
ipynb
Jupyter Notebook
Chapter2/Fig2_4_GaussLorentz.ipynb
CambridgeUniversityPress/Interstellar-and-Intergalactic-Medium
6d19cd4a517126e0f4737ba0f338117098224d92
CC0-1.0 CC-BY-4.0
2021-04-20T07:26:10.000Z
2022-02-24T11:02:47.000Z
Chapter2/Fig2_4_GaussLorentz.ipynb
CambridgeUniversityPress/Interstellar-and-Intergalactic-Medium
6d19cd4a517126e0f4737ba0f338117098224d92
CC0-1.0 CC-BY-4.0
Chapter2/Fig2_4_GaussLorentz.ipynb
CambridgeUniversityPress/Interstellar-and-Intergalactic-Medium
6d19cd4a517126e0f4737ba0f338117098224d92
CC0-1.0 CC-BY-4.0
Qwen/Qwen-72B
1. YES
2. YES