The notebook is available here: https://github.com/starkit/starkit/tree/master/docs/examples/interactive_spectrum.ipynb
Plotting the spectrum interactivelyΒΆ
[1]:
# Disable all warnings & logging statements from displaying in output
import warnings, logging
warnings.simplefilter('ignore')
logging.disable(logging.CRITICAL)
Load the grid you want to use:
[2]:
from starkit.gridkit import load_grid
from bqplot import pyplot as plt
from ipywidgets import interactive, IntSlider, FloatSlider
grid = load_grid('mygrid.h5')
Create the sliders for each grid parameter and a function to plot spectrum using those parameters:
[3]:
# Prepare sliders with their min, max from extent values
teff_extent, logg_extent, mh_extent = grid.get_grid_extent()
teff_slider = IntSlider(min=teff_extent[0], max=teff_extent[1], step=1, description='Teff', continuous_update=False)
logg_slider = FloatSlider(min=logg_extent[0], max=logg_extent[1], step=0.1, description='log g', continuous_update=False)
mh_slider = FloatSlider(min=mh_extent[0], max=mh_extent[1], step=0.1, description='[M/H]', continuous_update=False)
# Function to plot spectrum
def plot_spectrum(teff, logg, mh):
grid.teff = teff
grid.logg = logg
grid.mh = mh
wave, flux = grid()
plt.figure(title='Spectrum')
plt.plot(wave,flux)
plt.xlabel('Wavelength (Ang)')
plt.ylabel('Flux')
plt.show()
Now bind the both, and display the produced interactive plot:
[4]:
# Interactively plot by binding sliders with function arguments
interactive_plot = interactive(plot_spectrum, teff=teff_slider, logg=logg_slider, mh=mh_slider)
# Fix height to prevent output from flickering
output = interactive_plot.children[-1]
output.layout.height = '600px'
interactive_plot
[ ]: