python - 两条迹线,但只有一条由滑块控制
问题描述
我正在尝试使用 plotly 创建一个滑块,它控制一条迹线,但在我的图中有两条迹线和一个形状。我想使用滑块来控制“实验光谱”(及时),同时跟踪和形状是固定的。我不知道如何解决这个问题,因为我一使用滑块,第二条痕迹就消失了。我能怎么做?提前致谢。
这是我的代码示例:
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()
解决方案
推荐阅读
- r - 如何将基因的标题添加到 R 的输出中?
- javascript - 转换 JavaScript 数组中数据之间的关系
- javascript - React/Redux/Firebase createStore Config
- android - Android WebView 问题,请使用功能齐全的网络浏览器(例如 Google Chrome)访问此站点
- angular - 无法隐藏弹出箭头
- node.js - 文本文件中的内容将是文件名,如何获取并与新文件名进行比较(是否匹配)
- c# - 无法从程序集 System.Web 加载类型“System.Web.HttpPostedFileBase”
- c# - Physics.OverlapSphere() 返回的对撞机是否有特定顺序?
- typescript - 在受歧视的工会中没有多余的财产检查
- python - 在 Python 中聚合一个字典中的值以填充另一个字典