Hello, Iโm trying to create a single slider which controls one trace, but with two traces in my figure. I donโt know how to solve this problem, because the second trace disappear as soon as I use the slider. How can I do?
Thanks in advance.
This is a sample of my code:
import numpy as np
import sys
import os
PATH = '/Users/jacopo/Desktop/Spectra_Data/Atomic_Lines_NIST/Argon_I.csv'
if os.path.isfile(PATH) and os.access(PATH, os.R_OK):
print("File exists and is readable")
else:
print("Either the file is missing or not readable")
data=np.loadtxt(PATH)
xcoords = data[:,0]
ycoords = data[:,1]
xmin = np.amin(xcoords)
xmax = np.amax(xcoords)
ymin = np.amin(ycoords)
ymax = np.amax(ycoords)
name=PATH.split("/")[6].replace(".csv","")
file=sys.argv[1]
wavelegth_array=np.loadtxt(file, skiprows=1, dtype=np.float32)
lambda_array = np.loadtxt(file, skiprows=1, dtype=np.float32)[:,1:]
print ("Rows:",len(lambda_array))
print ("Columns:", len(lambda_array[0]))
x_array = wavelegth_array[:,0]#+1.27 = shift in lambda
y_array = lambda_array[:,]
#--------------Interactive plot with Plotly: Sliders
import plotly.graph_objects as go
def SumGauss(x, mu, sigma, intensity = 1):
sumgaussian = sum([intensity[i] * np.exp(-0.5*((x-mu[i])/sigma)**2) for i in range (len(mu))])
return sumgaussian
np.seterr(divide='ignore', invalid='ignore')
# Spectrum parameters
sigma_a1 = 0.04
x_range = np.linspace(xmin, xmax, 60000)
gauss_a = SumGauss(x_range, xcoords, sigma_a1, ycoords)
# Create figure
fig = go.Figure()
#Add trace for simulated spectra
fig.add_trace(go.Scatter( visible=True,
line=dict(color="yellow", width=2),
x=x_range,
y=gauss_a
))
# Add trace for experimental spectra
for step in range(len(lambda_array[0])):
fig.add_trace(
go.Scatter(
visible=False,
line=dict(color="#00CED1", width=2),
name="time = " + str(step)+ " ms",
x=x_array,
y=y_array[:,int(step)]
))
# Make 20th trace visible
fig.data[20].visible = True
#Set y range manually
fig.update_yaxes(range=[np.amin(y_array),np.amax(y_array)+50])
#Add atomic lines shape
for k in data:
fig.add_shape(type="line", xref='x', yref='y',
x0=k[0], y0=0, x1=k[0], y1=k[1],
line=dict(color="red",width=1))
# Create and add slider
steps = []
for i in range(len(fig.data)):
step = dict(
method="update",
args=[{"visible": [False] * len(fig.data)}]
)
step["args"][0]["visible"][i] = True
steps.append(step)
#--------------ONE SLIDER WITH TIME
sliders = [dict(
active=20,
currentvalue={"prefix": "Time: "},
pad={"t": 50},
steps=steps)]
fig.update_layout(showlegend=False,
sliders=sliders,
title_text="Atomic lines of {} ".format(name)+"in range: {:.2f}-{:.2f}".format(xmin,xmax)+" nm",
title_font_size=30, xaxis_title="Wavelength [nm]",
yaxis_title="Intensity"
)
fig.show()