Udi*_*zon 4 python plot plotly
我得到了一个折线图,其中有多条线代表不同频率的正弦波。
我想看一下特定的波浪,而其余的都不在图表中。我知道我可以在图例中单击我不想看到的线条,这样它们就会消失。
我想知道是否有一种交互式方式可以一次单击取消选择所有行,而不是单击每一行。
我的代码:
import numpy as np
import plotly.graph_objects as go
step = 1/1000
t = np.arange(0,1,step) # time vector
trig_func = lambda ff : np.sin(2*np.pi*ff*t)
fig = go.Figure()
for freq in [1, 3, 5, 7]:
y = trig_func(freq)
fig.add_trace(go.Scatter(x=t, y=y, name=f"{freq} Hz"))
fig.show()
Run Code Online (Sandbox Code Playgroud)
您可以双击图例中的线
您还可以设置它,以便默认情况下不显示一行,例如visible = "legendonly":
go.Scatter(x = data[x],
y = data[y],
visible = "legendonly")
Run Code Online (Sandbox Code Playgroud)
小智 6
在显示该图之前,添加此部分:
fig.update_layout(dict(updatemenus=[
dict(
type = "buttons",
direction = "left",
buttons=list([
dict(
args=["visible", "legendonly"],
label="Deselect All",
method="restyle"
),
dict(
args=["visible", True],
label="Select All",
method="restyle"
)
]),
pad={"r": 10, "t": 10},
showactive=False,
x=1,
xanchor="right",
y=1.1,
yanchor="top"
),
]
))
Run Code Online (Sandbox Code Playgroud)
这将添加按钮来一次取消选择和选择所有轨迹。它应该看起来像这样:

有关plotly中自定义按钮的更多信息:https ://plotly.com/python/custom-buttons/
| 归档时间: |
|
| 查看次数: |
5795 次 |
| 最近记录: |