Skip to content

Commit

Permalink
Allow for pre-created axes in plot_light_curve
Browse files Browse the repository at this point in the history
  • Loading branch information
kboone committed Jun 6, 2019
1 parent 428f4a6 commit 3653811
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions avocado/astronomical_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ def predict_gaussian_process(self, bands, times, uncertainties=True,
else:
return predictions

def plot_light_curve(self, show_gp=True, verbose=False, **kwargs):
def plot_light_curve(self, show_gp=True, verbose=False, axis=None,
**kwargs):
"""Plot the object's light curve
Parameters
Expand All @@ -326,6 +327,9 @@ def plot_light_curve(self, show_gp=True, verbose=False, **kwargs):
verbose : bool (optional)
If True, print detailed information about the light curve and GP
fit.
axis : `matplotlib.axes.Axes` (optional)
The matplotlib axis to plot to. If None, a new figure will be
created.
kwargs : kwargs (optional)
Additional arguments. If show_gp is True, these are passed to
`fit_gaussian_process`. Otherwise, these are passed to
Expand Down Expand Up @@ -355,36 +359,38 @@ def plot_light_curve(self, show_gp=True, verbose=False, **kwargs):
self.predict_gaussian_process(self.bands, pred_times,
fitted_gp=gp)

plt.figure()
if axis is None:
figure, axis = plt.subplots()

for band_idx, band in enumerate(self.bands):
mask = observations['band'] == band
band_data = observations[mask]
color = band_plot_colors[band]

plt.errorbar(band_data['time'], band_data['flux'],
band_data['flux_error'], fmt='o', c=color,
markersize=5, label=band)
axis.errorbar(band_data['time'], band_data['flux'],
band_data['flux_error'], fmt='o', c=color,
markersize=5, label=band)

if not show_gp:
continue

pred = predictions[band_idx]
plt.plot(pred_times, pred, c=color)
axis.plot(pred_times, pred, c=color)
err = prediction_uncertainties[band_idx]

if kwargs.get('uncertainties', True):
# If they were calculated, show uncertainties with a shaded
# band.
plt.fill_between(pred_times, pred-err, pred+err, alpha=0.2,
color=color)
axis.fill_between(pred_times, pred-err, pred+err, alpha=0.2,
color=color)

plt.legend()
axis.legend()

plt.xlabel('Time (days)')
plt.ylabel('Flux')
plt.tight_layout()
plt.xlim(min_time, max_time)
axis.set_xlabel('Time (days)')
axis.set_ylabel('Flux')
axis.set_xlim(min_time, max_time)

axis.figure.tight_layout()

def print_metadata(self):
"""Print out the object's metadata in a nice format."""
Expand Down

0 comments on commit 3653811

Please sign in to comment.