Interactive plot

This examples demonstrates how to read out and plot live absorbance spectra interactively.

Note

To run this example we need additional libraries. You can install it them via pip install numpy matplotlib.

Plot live absorbance spectra
import matplotlib.pyplot as plt
import numpy as np
import requests
from matplotlib.animation import FuncAnimation

base_url = "http://10.0.0.2:5555"
mode = "standard_v2"

# increase this for better data quality at the cost of speed
n_scans = 5

# use wavenumbers as x-axis
wn = requests.get(f"{base_url}/api/spectrometer/wavenumbers").json()

# set up plot
plt.ion()
fig, ax = plt.subplots()
(line,) = ax.plot(wn, np.zeros_like(wn))
ax.set_xlabel("Wavenumber (cm⁻¹)")
ax.set_ylabel("Absorbance")
ax.set_title("Live Absorbance Spectrum")
plt.show()


def fetch_and_update(_):
    response = requests.get(
        f"{base_url}/api/spectrometer/spectra/processed/{mode}",
        params={"n_scans": n_scans},
    ).json()

    # use absorbance values as y-axis
    y = np.array(response)
    line.set_ydata(y)
    ax.relim()
    ax.autoscale_view()
    fig.canvas.draw()
    fig.canvas.flush_events()


# fetch_and_update is called every second
anim = FuncAnimation(fig, fetch_and_update, interval=1000)

plt.show(block=True)