Plotly 下拉菜单丢失映射

Plotly dropdown menus loosing mapping

我正在绘制一个图表,用户可以在其中 select 每个需求的 X 和 Y 变量。到目前为止,当 select 在一个下拉列表中输入所需的变量时,效果很好。但是,当我切换到另一个下拉菜单时,我失去了所有颜色映射:参见下面的示例:

选择第一个下拉菜单:

选择第二个下拉菜单:

我使用的代码如下:

 for col in colnames:


    button_x_list.append(dict(method='update',
                        label=col,
                        visible=True,
                        args=[{'x':[df[col]]},{"xaxis":{"title":col}},{"marker.color_discrete_map":list(df["status"].map(colors))}],
                        )
                  )

    button_y_list.append(dict(method='update',
                        label=col,
                        visible=True,
                        args=[{'y':[df[col]]},{"yaxis":{"title":col}},{"marker.color_discrete_map":list(df["status"].map(colors))}],
                        )
                 )
                        
                            
    
    
button_x_dict= dict(direction="down",
            showactive=True,
            xanchor="left",
            yanchor="top",
            visible=True,
            buttons=button_x_list,
            pad={"r": 15, "t": 10},
            x=0.27,
            y=1.07,)

button_y_dict= dict(direction="down",
            showactive=True,
            xanchor="left",
            yanchor="top",
            visible=True,
            buttons=button_y_list,
            pad={"r": 15, "t": 10},
            x=0.5,
            y=1.07,)                         
                         

annotation_x = dict(text="X:", showarrow=False, x=0.25, y=1.05, xanchor="left",xref="paper",yref="paper", align="left",yanchor="top")
annotation_y = dict(text="Y:", showarrow=False, x=0.48, y=1.05, xanchor="left", xref="paper",yref="paper", align="left",yanchor="top")

fig = go.Figure()
fig = px.scatter(df, x="var1", y="var2", color=type_check,color_discrete_map=colors, title="Test"})
  • 为了使其成为可重现的示例,我模拟了数据
  • 从根本上说,问题是 plotlty express 为每种颜色生成一个 trace。然后这些与 updatemenus
  • 不一致
  • 更改为 图形对象 较低级别 API 并且您可以以仅使用一个 trace[=25= 的方式控制颜色的生成]
  • 如果我正确理解了您的数据形状,那么在 updatemenusupdate 颜色是多余的。我把它留在原地
import plotly.graph_objects as go
import pandas as pd
import numpy as np

s = 300
colnames = list("3456")
df = pd.DataFrame(
    {
        **{"status": np.random.choice(["A", "B", "C"], s)},
        **{c: np.random.uniform(5 + int(c), 10 + int(c), s) for c in colnames},
    }
)

button_x_list = []
button_y_list = []
colors = {"A": "red", "B": "blue", "C": "yellow"}
for col in colnames:

    button_x_list.append(
        dict(
            method="update",
            label=col,
            visible=True,
            args=[
                {"x": [df[col]]},
                {"xaxis": {"title": col}},
                {"marker":{"color": list(df["status"].map(colors))}},
            ],
        )
    )

    button_y_list.append(
        dict(
            method="update",
            label=col,
            visible=True,
            args=[
                {"y": [df[col]]},
                {"yaxis": {"title": col}},
                {"marker":{"color": list(df["status"].map(colors))}},
            ],
        )
    )


button_x_dict = dict(
    direction="down",
    showactive=True,
    xanchor="left",
    yanchor="top",
    visible=True,
    buttons=button_x_list,
    pad={"r": 15, "t": 10},
    x=0.27,
    y=1.07,
)

button_y_dict = dict(
    direction="down",
    showactive=True,
    xanchor="left",
    yanchor="top",
    visible=True,
    buttons=button_y_list,
    pad={"r": 15, "t": 10},
    x=0.5,
    y=1.07,
)


annotation_x = dict(
    text="X:",
    showarrow=False,
    x=0.25,
    y=1.05,
    xanchor="left",
    xref="paper",
    yref="paper",
    align="left",
    yanchor="top",
)
annotation_y = dict(
    text="Y:",
    showarrow=False,
    x=0.48,
    y=1.05,
    xanchor="left",
    xref="paper",
    yref="paper",
    align="left",
    yanchor="top",
)


fig = go.Figure(go.Scatter(x=df["3"], y=df["3"], mode="markers", marker={"color":df["status"].map(colors)}))
fig.update_layout(
    updatemenus=[button_x_dict, button_y_dict], annotations=[annotation_x, annotation_y],
    title="Test",
    xaxis={"title":"3"}, yaxis={"title":"3"}
)