Hello,
I’ve created two sliders and I’m trying to control two traces with them, but each of them affect all traces (so i can’t control traces separately). Is there a way to do it?
Here a sample of my code:
import numpy as np
import sys
PATH = '/Users/jacopo/Desktop/Spectra_Data/Atomic_Lines_NIST/Argon_I.csv'
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]
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
new_array=np.arange(0.01, 1, 0.01)
for sigma in np.arange(0.01, 1, 0.01):
fig.add_trace(go.Scatter( visible=False,
line=dict(color="green", width=2),
name="σ = " + str(sigma),
x=x_range,
y=SumGauss(x_range, xcoords, sigma, ycoords)
))
# 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 100th trace visible
fig.data[100].visible = True
#Set y range manually
fig.update_yaxes(range=[np.amin(y_array),np.amax(y_array)+50])# shift on max y value to display spectrum in a better way
#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(lambda_array[0])):
step = dict(#label="time = " + str(i)+ " ms",
method="update",
args=[{"visible": [False] * len(lambda_array[0])}]
#{"title": "Spectra at time: " + str(i) + " ms"}], #layout attribute
)
step["args"][0]["visible"][i] = True #Toggle i'th trace to "visible"
#print(step)
steps.append(step)
newsteps = []
for j in range(len(new_array)):
stip = dict(#label="σ = " + str(j),
method="update",
args=[{"visible": [False] * len(new_array)}]
#{"title": "Spectra at time: " + str(i) + " ms"}], #layout attribute
)
stip["args"][0]["visible"][j] = True #Toggle i'th trace to "visible"
#print(stips)
newsteps.append(stip)
#--------------TWO SLIDER WITH TIME AND SIGMA
sliders = [dict(
active=20,
currentvalue={"prefix": "Time: "},
pad={"t": 50},
y=-0.1,
steps=steps),
dict(
active=4,
currentvalue={"prefix": "Sigma: "},
pad={"t": 50},
y=-0.3,
steps=newsteps)
]
fig.update_layout(showlegend=False,
sliders=sliders,
width=1300,
height=900,
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()