[英]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.