pythonnumpymatplotlibmultidimensional-arrayfits

Combine results from while loop for plotting


I have 3 data files; wavelength/pixels, counts/pixels, and background/pixels. Each data files multiple order (total number of orders = 35). I have fitted a polynomial to each order and plotted the results. However, instead of 35 plots I need one plot that combines all the result. I have tried filling an empty array using .append but I could not use this to plot the combined results. This is my code that plots each order. Could someone point me in the right direction about how to combine that results? Thank you in advance.

# =============================================================================
# Load libraries
# =============================================================================
from astropy.io import fits
import matplotlib.pyplot as plt
import numpy as np
# =============================================================================
# Load data
# =============================================================================
counts_image = ("counts.fits")
wavelength_image = ("wavelength.fits")
background_image = ("backgound.fits")
# =============================================================================
# Open as fits files
# =============================================================================
sp = fits.open(counts_image)
sp_w = fits.open(wavelength_image)
sp_b = fits.open(background_image)
# =============================================================================
# Array data
# =============================================================================
counts_data = np.array(sp[0].data)
wave_data = np.array(sp_w[0].data)
background_data = np.array(sp_b[0].data)
# =============================================================================
# Order-by-order
# =============================================================================
order = 0
while order < 34:
# =============================================================================    
    counts = counts_data[order]
    wave = wave_data[order]
    background = background_data[order]
# =============================================================================
# Fitting
# =============================================================================
    z = Fit(wave, counts)
# =============================================================================
# Wavelength range
# =============================================================================
    wavespread = max(wave) - min(wave)
    wavecenter = wave - min(wave) - wavespread/2.
# =============================================================================  
# Normalize 
# =============================================================================
    norm = counts/max(counts)
# =============================================================================    
# Make a function based on those polynomial coefficients
# =============================================================================
    cfit = np.poly1d(z)
# =============================================================================  
# Plot the original 
#==============================================================================
    plt.figure()
    plt.plot(wavecenter, norm)
# =============================================================================
# Continuum fit
# =============================================================================
    plt.plot(wavecenter, cfit(wavecenter))
    fitted = norm/cfit(wavecenter)
# =============================================================================
    plt.figure()
    plt.plot(wave, fitted)
    plt.ylim([0,1.1])
    plt.xlim([min(wave), max(wave)])
# =============================================================================
# End while loop
# =============================================================================   
    ord += 1
# =============================================================================


Solution

  • In order to show all graphs in one plot, I added all the different data arrays to a list using .append() (x and y). The following code then plotted the different graphs into one plot.

    fig,axarray = plt.subplots()
    for i in range(len(x)):
        axarray.plot(x[i],y[i])
    
    plt.show()