<p>If you move the <code>plt.figure()</code> call outside the function all the lines should appear on the same plot. Also, you can move the <code>xlabel</code> and <code>ylabel</code> calls outside the function as well since you only need to label the axes once. Finally, overwriting the title of the plot for every file didn't make any sense to me, so I moved that part to the legend.</p>
<pre><code>import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
from astropy.wcs import WCS
%matplotlib widget

file_path = [
    '~/Desktop/r.HARPN.2018-01-18T14:25:36.145_CCF_A.fits',
    '~/Desktop/r.HARPN.2018-01-18T14:20:10.645_CCF_A.fits',
    '~/Desktop/r.HARPN.2018-01-18T14:31:00.875_CCF_A.fits']

def plot_fits_file(file_path):
    hdul = fits.open(file_path)
    data = hdul[1].data
    h1 = hdul[1].header
    flux = data[1]
    w = WCS(h1, naxis=1, relax=False, fix=False)
    lam = w.wcs_pix2world(np.arange(len(flux)), 0)[0]
    
    plt.plot(lam, flux, label=file_path)

plt.figure(figsize=(5, 5))
plt.ylim(0, )
plt.xlabel('RV')
plt.ylabel('Normalized CCF')

for file_path in spec_list:
    plot_fits_file(file_path)

plt.legend()
plt.show()
</code></pre>

CC BY-SA 4.0
2023-05-12T18:27:37.237Z
2023-05-12T21:23:20.357Z
2023-05-12T21:23:20.357Z
Roy Smart
<p>At the moment, I get three separate graphs.  Ideally I would like to overlay all graphs, so that I only have one plot with multiple FIT files.</p>
<p>I have a folder on my desktop with the HARPS-N data, the extension is</p>
<p>~/Desktop/harpn_sun_release_package_ccf_2018/2018-01-18/r.HARPN.2018-01-18T14:25:36.145_CCF_A.fits</p>
<p>This is my code so far.. with just three of the fits</p>
<pre><code>import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
from astropy.wcs import WCS
%matplotlib widget

file_path = [
    '~/Desktop/r.HARPN.2018-01-18T14:25:36.145_CCF_A.fits',
    '~/Desktop/r.HARPN.2018-01-18T14:20:10.645_CCF_A.fits',
    '~/Desktop/r.HARPN.2018-01-18T14:31:00.875_CCF_A.fits']

def plot_fits_file(file_path):
    hdul = fits.open(file_path)
    data = hdul[1].data
    h1 = hdul[1].header
    flux = data[1]
    w = WCS(h1, naxis=1, relax=False, fix=False)
    lam = w.wcs_pix2world(np.arange(len(flux)), 0)[0]
    
    plt.figure(figsize=(5, 5))
    plt.plot(lam, flux)
    plt.ylim(0, )
    plt.xlabel('RV')
    plt.ylabel('Normalized CCF')
    plt.title(file_path)
    plt.show()

    
for file_path in spec_list:
    plot_fits_file(file_path)
</code></pre>

CC BY-SA 4.0
2023-05-12T07:12:27.423Z
Your title is word salad. Please review your question and write it as though the reader knows nothing about what you are trying to do.
ProfRob
CC BY-SA 4.0
2023-05-12T08:55:37.157Z
Question edited
AlbPet
CC BY-SA 4.0
2023-05-12T10:06:15.900Z
This looks more like a coding issue and may be more appropriate on https://stackoverflow.com
GrapefruitIsAwesome
CC BY-SA 4.0
2023-05-13T01:47:09.733Z
I'm examining CCF's v radial velocity on the HARPS-N site.
AlbPet
<python><fits>
Plot several FITS files in one graph
AlbPet
# Plot several FITS files in one graph

(Asked by: username_0 on 2023-05-12)

At the moment, I get three separate graphs.  Ideally I would like to overlay all graphs, so that I only have one plot with multiple FIT files.
I have a folder on my desktop with the HARPS-N data, the extension is
~/Desktop/harpn_sun_release_package_ccf_2018/2018-01-18/r.HARPN.2018-01-18T14:25:36.145_CCF_A.fits
This is my code so far.. with just three of the fits
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
from astropy.wcs import WCS
%matplotlib widget

file_path = [
    '~/Desktop/r.HARPN.2018-01-18T14:25:36.145_CCF_A.fits',
    '~/Desktop/r.HARPN.2018-01-18T14:20:10.645_CCF_A.fits',
    '~/Desktop/r.HARPN.2018-01-18T14:31:00.875_CCF_A.fits']

def plot_fits_file(file_path):
    hdul = fits.open(file_path)
    data = hdul[1].data
    h1 = hdul[1].header
    flux = data[1]
    w = WCS(h1, naxis=1, relax=False, fix=False)
    lam = w.wcs_pix2world(np.arange(len(flux)), 0)[0]
    
    plt.figure(figsize=(5, 5))
    plt.plot(lam, flux)
    plt.ylim(0, )
    plt.xlabel('RV')
    plt.ylabel('Normalized CCF')
    plt.title(file_path)
    plt.show()

    
for file_path in spec_list:
    plot_fits_file(file_path)

**Tags:** python, fits

> **Comments:**
> **username_1**: Your title is word salad. Please review your question and write it as though the reader knows nothing about what you are trying to do.  
> **username_0**: Question edited  
> **username_2**: This looks more like a coding issue and may be more appropriate on https://stackoverflow.com  
> **username_0**: I'm examining CCF's v radial velocity on the HARPS-N site.  

---

## 1 answers

### Answer by username_3 on 2023-05-12. Score: 1

If you move the plt.figure() call outside the function all the lines should appear on the same plot. Also, you can move the xlabel and ylabel calls outside the function as well since you only need to label the axes once. Finally, overwriting the title of the plot for every file didn't make any sense to me, so I moved that part to the legend.
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
from astropy.wcs import WCS
%matplotlib widget

file_path = [
    '~/Desktop/r.HARPN.2018-01-18T14:25:36.145_CCF_A.fits',
    '~/Desktop/r.HARPN.2018-01-18T14:20:10.645_CCF_A.fits',
    '~/Desktop/r.HARPN.2018-01-18T14:31:00.875_CCF_A.fits']

def plot_fits_file(file_path):
    hdul = fits.open(file_path)
    data = hdul[1].data
    h1 = hdul[1].header
    flux = data[1]
    w = WCS(h1, naxis=1, relax=False, fix=False)
    lam = w.wcs_pix2world(np.arange(len(flux)), 0)[0]
    
    plt.plot(lam, flux, label=file_path)

plt.figure(figsize=(5, 5))
plt.ylim(0, )
plt.xlabel('RV')
plt.ylabel('Normalized CCF')

for file_path in spec_list:
    plot_fits_file(file_path)

plt.legend()
plt.show()


---

