每个图/痕迹多行的数字的下拉按钮
我正在尝试在Platly中创建一个下拉按钮,该按钮将允许一次绘制多个向量以获取数据子集(或者,对于同一下拉列表按钮具有多个跟踪)。这些子集将通过上述按钮选择。
这是plotly.express
的玩具示例:
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
df = pd.DataFrame(
dict(
x=[1.0, 2.0, 3.0, 4.0],
y1=[1.0, 2.0, 3.0, 4.0],
y2=[2.0, 4.0, 6.0, 8.0],
z=["a", "a", "b", "b"],
)
)
px.scatter(df, x="x", y=["y1", "y2"], symbol="z")
plt.show()
我想实现的是一个下拉按钮,它将为不同的z值选择一个子集(“ a”或“” b“)。
不幸的是,go.scatter
似乎没有多个y
数组,我最终会弄乱一团糟。
zs = df.z.unique()
dropdown_buttons = []
fig = go.Figure()
for i, val in enumerate(zs):
df_ = df.query(f'z=="{val}"')
fig.add_trace(
go.Scatter(
x=df_["x"],
y=df_[["y1", "y2"]],
name=val,
)
)
dropdown_buttons.append(
{
"label": val,
"method": "update",
"args": [
{"visible": [x == i for x in range(len(zs))]},
{"title": val},
],
}
)
fig.update_layout(
{
"updatemenus": [
{
"type": "dropdown",
"showactive": True,
"active": 0,
"buttons": dropdown_buttons,
}
]
}
)
fig.show()
I'm trying to create a dropdown button in plotly that would allow for plotting multiple vectors at once for a subset of data (or, to have multiple traces for the same dropdown button). These subsets would be chosen via the above-mentioned button.
Here's a toy example in plotly.express
:
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
df = pd.DataFrame(
dict(
x=[1.0, 2.0, 3.0, 4.0],
y1=[1.0, 2.0, 3.0, 4.0],
y2=[2.0, 4.0, 6.0, 8.0],
z=["a", "a", "b", "b"],
)
)
px.scatter(df, x="x", y=["y1", "y2"], symbol="z")
plt.show()
What I'd like to achieve is a dropdown button that would select a subset for distinct z values ("a" or "b").
Unfortunately, go.Scatter
does not seem like to have multiple y
arrays and I end up with a complete mess.
zs = df.z.unique()
dropdown_buttons = []
fig = go.Figure()
for i, val in enumerate(zs):
df_ = df.query(f'z=="{val}"')
fig.add_trace(
go.Scatter(
x=df_["x"],
y=df_[["y1", "y2"]],
name=val,
)
)
dropdown_buttons.append(
{
"label": val,
"method": "update",
"args": [
{"visible": [x == i for x in range(len(zs))]},
{"title": val},
],
}
)
fig.update_layout(
{
"updatemenus": [
{
"type": "dropdown",
"showactive": True,
"active": 0,
"buttons": dropdown_buttons,
}
]
}
)
fig.show()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论