使用列值作为按钮绘制下拉菜单

问题描述 投票:0回答:2

我有一个数据集,可在 here 中使用,我想将其转换为带有自定义下拉菜单的绘图。

我的目标是在组合的散点图中绘制四个值:

chrPos
(x 轴)和
depth_total, depth_5 and depth_3
(全部在 y 轴上),具体取决于特征列(应在下拉列表中选择)下菜单)。 例如(其中 Cluster 等于 Feature)

你有任何关于如何实现这一目标的线索吗?

提前致谢

python plot plotly
2个回答
0
投票

您可以使用

fig=go.Figure()
创建一个空图形,然后为每个要显示为条形的特征添加
go.Bar
轨迹:
depth_total, depth_5 and depth_3
。然后您可以将散点添加为最后一条轨迹。

然后您的下拉菜单将通过传递一个布尔值来修改这些条形轨迹中的每一个是否可见(如果您有四个轨迹,例如

[depth_total, depth_5, depth_3, centroid]
并且用户选择
depth_total
,您的下拉菜单将传递
[True, False, False, True]
以显示depth_total轨迹和质心的散点——假设您希望散点始终可见)

import pandas as pd
import plotly.graph_objects as go

df = pd.read_csv('https://raw.githubusercontent.com/YouCook21/AskToTheWebExamples/main/PlotlyDropdown.csv')
df = df.sort_values(by='chrPos')
df['chrPos'] = df['chrPos'].astype(str)

fig = go.Figure()
features = ['depth_total', 'depth_5', 'depth_3']
buttons = []

## make depth_total visible by default
for feature in features:
    if feature == 'depth_total':
        visible=True
    else:
        visible=False
    fig.add_trace(go.Bar(
        x=df['chrPos'],
        y=df[feature],
        name=feature,
        visible=visible
    ))

    ## only show the selected bar, plus the scatter which is the last trace
    show_traces = [f == feature for f in features] + [True]
    buttons.append(
        dict(label=feature,
             method="update",
             args=[{"visible": show_traces}])
    )

## add centroid as scatter
fig.add_trace(go.Scatter(
    x=df['chrPos'],
    y=df['centroid'],
    name="centroid",
    mode='lines',
    line=dict(dash='dash', color='black', width=1)
))

fig.update_layout(
    updatemenus=[
        dict(
            active=0,
            buttons=buttons,
        )
    ])

fig.show()


0
投票
import plotly.express as px
import plotly.graph_objects as go

# groupby the id
grps = list(df.groupby('centroid'))
# create a start and end step because we have three graphs for each id
start=0
end=3

buttons = []
figs = go.Figure()
# iterate over the groupby object
for idx,v in enumerate(grps):
    # use the centroid as the name of each graph
    name = f'{v[0]}'
    # set the visibility of each graph on selection
    visible = [False] * len(grp)*3
    visible[start:end] = [True]*3
    # update the start/end values
    start+=3
    end+=3
    # create your graph for each id
    fig = px.scatter(v[1], x='chrPos', y='depth_total')
    fig.add_bar(x=v[1]['chrPos'], y=v[1]['depth_3'], name='depth_3')
    fig.add_bar(x=v[1]['chrPos'], y=v[1]['depth_5'], name='depth_5')
    
    # set the visibility for the first three traces as the default
    
    if idx == 0:
        fig.data[0].visible = True
        fig.data[1].visible = True
        fig.data[2].visible = True
    else:
        fig.data[0].visible = False
        fig.data[1].visible = False
        fig.data[2].visible = False
    # add all your traces to the empty go.Figure
    figs.add_traces(fig.data)
    # append your dropdowns
    buttons.append(dict(label=name,
                        method="update",
                        args=[{"visible": visible},
                              {"title":f"Cluster n: {name}"}]))


updatemenus = [{'active': 0, "buttons": buttons, 'showactive': True}]
figs.update_layout(title=f'Cluster n: {grps[0][0]}', title_x=0.5, updatemenus=updatemenus)
figs.show()

© www.soinside.com 2019 - 2024. All rights reserved.