<jupyter_start><jupyter_text>Matplotlib animations demo using Cepheids  This demonstration shows how to make an animated figure with matplotlib. We'll use photometric variability data for a set of Cepheids from Gaia DR1.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython.display import HTML<jupyter_output><empty_output><jupyter_text>^ The last line there is needed to make the animation render in the notebook.<jupyter_code>from get_data import get_cepheids
table = get_cepheids()
table<jupyter_output><empty_output><jupyter_text>Based on Cepheid parameters from Gaia DR1, calculate the magnitudes at a given time. Documentation on the parameters can be found [here](https://gea.esac.esa.int/archive/documentation/GDR1/datamodel/Ch2/cepheid.html).Note I'm just making up reference phases because we're supplied only with a reference epoch and I'm lazy! This is a cartoon version...<jupyter_code>ref_epochs = table['epoch_g'] + 2455197.5
phis = np.random.rand(len(table)) * 2. * np.pi
A1 = table['peak_to_peak_g']/2.
def delta_mags(epoch):
    delta_t = epoch - ref_epochs
    delta_mags = A1 * np.sin(2. * np.pi * delta_t/table['p1'] + phis) \
        + A1 * table['r21_g'] * np.sin(2. * np.pi * delta_t/table['p1'] 
                                       + phis * table['phi21_g'])
    return delta_mags<jupyter_output><empty_output><jupyter_text>Now we make a function to feed to [matplotlib.animation.FuncAnimation](https://matplotlib.org/api/_as_gen/matplotlib.animation.FuncAnimation.html). It takes an epoch and an axis object, clears the axis, and replots the data using the magnitudes at the given epoch as sizes.<jupyter_code>mags = table['int_average_g']
sizes = np.exp(mags-15.) # kinda arbitrary

def plot_cepheids(epoch, ax):
    dmags = delta_mags(epoch)
    size_factor = 10.**(dmags/2.5) * 30. # scales with luminosity difference
    ax.cla()
    ax.scatter(table['ra'], table['dec'], s=sizes+size_factor)
    ax.set_xlabel('Right Ascension (degrees)', fontsize=16)
    ax.set_ylabel('Declination (degrees)', fontsize=16)
    ax.set_xlim([80,73])
    ax.set_ylim([-67,-65])<jupyter_output><empty_output><jupyter_text>Generate some epochs and test it out:<jupyter_code>epochs = np.linspace(0, 50, 200) + 2455197.5
fig = plt.figure(figsize=(8, 8))
ax = plt.subplot()
anim = animation.FuncAnimation(fig, plot_cepheids, epochs,
                                fargs=(ax,), 
                                interval=50)
plt.close(fig)
HTML(anim.to_html5_video())<jupyter_output><empty_output><jupyter_text>To save this as a gif, we use imagemagick:<jupyter_code>anim.save('cepheids.gif', writer='imagemagick')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1150866838804869, 'fraction_numerical': 0.023607524898561418, 'mean_word_length': 4.445783132530121, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '185143', 'license': 'MIT', 'n_tokens_mistral': 976, 'n_tokens_neox': 929, 'n_words': 229, 'path': 'animated_cepheids.ipynb', 'repo_name': 'megbedell/astro-dataviz', 'size': '2711'}