0
$\begingroup$

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)
$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – ProfRob
    May 12, 2023 at 7:12
  • $\begingroup$ Question edited $\endgroup$
    – AlbPet
    May 12, 2023 at 8:55
  • 1
    $\begingroup$ This looks more like a coding issue and may be more appropriate on stackoverflow.com $\endgroup$ May 12, 2023 at 10:06
  • $\begingroup$ I'm examining CCF's v radial velocity on the HARPS-N site. $\endgroup$
    – AlbPet
    May 13, 2023 at 1:47

1 Answer 1

1
$\begingroup$

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()
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .