簡體   English   中英

結合 while 循環的結果進行繪圖

[英]Combine results from while loop for plotting

我有 3 個數據文件; 波長/像素、計數/像素和背景/像素。 每個數據文件有多個訂單(訂單總數 = 35)。 我已經為每個訂單擬合了一個多項式並繪制了結果。 但是,我需要一個結合所有結果的 plot 而不是 35 個圖。 我嘗試使用.append填充一個空數組,但我無法將其用於 plot 組合結果。 這是我繪制每個訂單的代碼。 有人可以指出我如何結合這些結果的正確方向嗎? 先感謝您。

# =============================================================================
# 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
# =============================================================================

為了顯示 plot 中的所有圖表,我使用.append() (x 和 y)將所有不同的數據 arrays 添加到列表中。 然后,以下代碼將不同的圖形繪制成一個 plot。

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

plt.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM