Hi I’m not so familiar with python so I need basic help.
I have this code, that plots a light curve(graph of flux as a function of time) I need to plot several light curves (different from each other by something called filter=color) on the same graph with different colors and offset. here is my code:
import json
import numpy as np
from astropy.table import Table
import matplotlib.pyplot as plt
##############################################################################
def bin_daily(x, y, e) → object:
# Sort:
s = np.argsort(x)
x = x[s]
y = y[s]
e = e[s]
# Convert to flux:
flux = 10 ** (-0.4 * y)
fluxe = abs(flux * (-0.4) * np.log(10) * e)
if 0 in fluxe:
weight = np.tile(1, len(fluxe))
else:
weight = 1 / fluxe ** 2
# Do the binning using weighted averages:
newdayjump = 0.5
toaverage = [0]
binned_jd = np.array([])
binned_flux = np.array([])
binned_fluxe = np.array([])
for i in range(0, len(x)):
if abs(x[i] - x[toaverage[-1]]) < newdayjump:
toaverage.append(i)
else:
binned_jd = np.append(binned_jd, sum(weight[toaverage] * x[toaverage]) / sum(weight[toaverage]))
binned_flux = np.append(binned_flux, sum(weight[toaverage] * flux[toaverage]) / sum(weight[toaverage]))
binned_fluxe = np.append(binned_fluxe, np.sqrt(1 / sum(weight[toaverage])))
toaverage = [i]
# Convert back to magnitudes:
binned_mag = -2.5 * np.log10(binned_flux)
if 0 in fluxe:
binned_mage = np.tile(0, len(binned_flux))
else:
binned_mage = abs(-2.5 * binned_fluxe / (binned_flux * np.log(10)))
s = np.argsort(binned_jd)
return (binned_jd[s], binned_mag[s], binned_mage[s])
##############################################################################
fig, ax = plt.subplots(1, 1, figsize=(14, 6))
offsets = {‘g’: 1, ‘r’: 0, ‘i’: -1 , ‘b’:2,‘m’:-2}# m for filter V and b for filter B
ZTF Data:
with open(‘ztf.json’) as json_file:
data = json.load(json_file)
filts = [‘g’, ‘r’]
for i, f in enumerate(filts):
mjd = np.array([x[‘mjd’] for x in data[‘candidates’] if x[‘fid’] == i + 1 and ‘isdiffpos’ in x])
mag = np.array([x[‘magpsf’] for x in data[‘candidates’] if x[‘fid’] == i + 1 and ‘isdiffpos’ in x])
magerr = np.array([x[‘sigmapsf’] for x in data[‘candidates’] if x[‘fid’] == i + 1 and ‘isdiffpos’ in x])
mjd, mag, magerr = bin_daily(mjd, mag, magerr)
ax.errorbar(mjd, mag + offsets[f], magerr, color=f, linestyle=‘none’, marker=‘o’,
label=‘ZTF {}{:+d}’.format(f, offsets[f]).replace(’+0’, ‘’))
#adding ZTF found by Irura
with open(‘ztf2.json’) as json_file:
data = json.load(json_file)
filts = [‘g’, ‘r’]
for i, f in enumerate(filts):
mjd = np.array([x[‘mjd’] for x in data[‘candidates’] if x[‘fid’] == i + 1 and ‘isdiffpos’ in x])
mag = np.array([x[‘magpsf’] for x in data[‘candidates’] if x[‘fid’] == i + 1 and ‘isdiffpos’ in x])
magerr = np.array([x[‘sigmapsf’] for x in data[‘candidates’] if x[‘fid’] == i + 1 and ‘isdiffpos’ in x])
mjd, mag, magerr = bin_daily(mjd, mag, magerr)
ax.errorbar(mjd, mag + offsets[f], magerr, color=f, linestyle=‘none’, marker=‘o’,markerfacecolor=‘none’,
label=‘ZTF18achzddr {}{:+d}’.format(f, offsets[f]).replace(’+0’, ‘’))
LCO Data:
lcodata = Table.read(‘lco.txt’, format=‘ascii’)
filts = [‘gp’, ‘rp’, ‘ip’,‘bB’,‘vV’]
for f in filts:
w = lcodata[‘filter’] == f
mjd = np.array(lcodata[‘mjd’][w])
mag = np.array(lcodata[‘mag’][w])
magerr = np.array(lcodata[‘dmag’][w])
mjd, mag, magerr = bin_daily(mjd, mag, magerr)
ax.errorbar(mjd, mag + offsets[f[0].replace(‘v’, ‘m’)], magerr, color=f[0].replace(‘i’, ‘k’).replace(‘v’, ‘m’),
linestyle=‘none’, marker=‘s’,label=‘Las Cumbres {}{:+d}’.format(f[0], offsets[f[0].replace(‘v’, ‘m’)]).replace(’+0’, ‘’))
# Hinkle Data:
hinkdata= Table.read(‘hinkatls.txt’, format=‘ascii’)
filts = [‘op’, ‘cp’]
for f in filts:
** q=hinkdata[‘filter’] == f**
** mjd = np.array(hinkdata[‘mjd’][q])**
** mag = np.array(hinkdata[‘mag’][q])**
** magerr = np.array(hinkdata[‘dmag’][q])**
** mjd, mag, magerr = bin_daily(mjd, mag, magerr)**
** ax.errorbar(mjd, mag + offsets[f[0].replace(‘o’, ‘w’)], magerr, color=f[0].replace(‘o’, ‘w’),**
** linestyle=‘none’, marker=‘s’,**
** label=‘Las Cumbres {}{:+d}’.format(f[0], offsets[f[0].replace(‘o’, ‘w’)]).replace(’+0’, ‘’))**
ax.invert_yaxis()
ax.set_xlabel(‘MJD’)
ax.set_ylabel(‘Apparent Magnitude’)
ax.legend()
plt.show()
fig.savefig(‘lightcurve1.pdf’)
so -It works except for the Hinkle data part it doesn’t work (something wrong with the letter ‘w’ I choose to replace the ‘p’ with and also don’t except the ‘c’. why is that?
thanks