Python Plotly 图表更新有两个下拉菜单

发布于 2025-01-18 06:44:50 字数 2389 浏览 3 评论 0原文

我正在尝试在Jupyter Lab中构建一个情节散点图,以便能够在数据框架中的各个列之间看到依赖关系。 我想有两个下拉菜单(与X和Y轴相对应),在每个菜单中都有完整的DF列列表。当我在任何菜单中选择一列时,应将适当轴上的数据替换为我选择的列(因此,如果我为X和Y选择了同一列,我希望直线直线)。

以下是我使用示例数据框架的当前实现:

# Creating the DataFrame    
temp = pd.DataFrame(np.random.randint(0, 1000, (100, 10)))
col_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
temp.columns = col_list

# Init figure with the A column on both axes by default
fig = go.Figure()

default_col = 0
fig.add_trace(
    go.Scatter(
        x=temp[col_list[default_col]].values,
        y=temp[col_list[default_col]].values,
        name="Metric correlation",
        mode="markers"
    ),
)
fig.update_xaxes(title_text=col_list[default_col])
fig.update_yaxes(title_text=col_list[default_col])

col_list = temp.columns

# Building options for each of the lists
btns_x = [
    dict(
        label=c,
        method="update",
        args=[
            {"x": temp[c].fillna(0).values,
             'xaxis': {'title': c}
             }],
    ) for c in col_list]


btns_y = [
    dict(
        label=c,
        method="update",
        args=[
            {"y": temp[c].fillna(0).values,
             'yaxis': {'title': c}
             }],
    ) for c in col_list]

# Adding the lists to the figure
fig.update_layout(
    updatemenus=[
        dict(
            buttons=btns_x,
            # method="update",
            direction="down",
            pad={"r": 10, "t": 10},
            showactive=True,
            x=0.1,
            xanchor="left",
            y=1.1,
            yanchor="top"
        ),
        dict(
            buttons=btns_y,
            # method="update",
            direction="down",
            pad={"r": 10, "t": 10},
            showactive=True,
            x=0.1,
            xanchor="right",
            y=1.1,
            yanchor="top"
        ),
    ]
)
fig.update_layout(width=1000, height=1000)
fig.show()

该图最初是正确绘制的:

仍然存在一些问题:

  1. 当我在下拉列表中更改值时,它只能工作一次。在接下来的尝试中,
  2. 如果我首先更改一个下拉列表的值,然后在另一个下拉列表中,所有数据都从图中消失(请参见下面的屏幕截图),
  3. 轴标签未更新

I am trying to build a plotly scatterplot in Jupyter Lab to be able to see dependencies between various columns in a DataFrame.
I want to have two dropdown menus (corresponding to the X and Y axes), in each of which a full list of the DF columns will be available. When I select a column in any of the menus, the data on the appropriate axis should be replaced by the column I selected (so, if I select the same column for X and Y, I would expect a straight line).

Below is my current implementation with a sample DataFrame:

# Creating the DataFrame    
temp = pd.DataFrame(np.random.randint(0, 1000, (100, 10)))
col_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
temp.columns = col_list

# Init figure with the A column on both axes by default
fig = go.Figure()

default_col = 0
fig.add_trace(
    go.Scatter(
        x=temp[col_list[default_col]].values,
        y=temp[col_list[default_col]].values,
        name="Metric correlation",
        mode="markers"
    ),
)
fig.update_xaxes(title_text=col_list[default_col])
fig.update_yaxes(title_text=col_list[default_col])

col_list = temp.columns

# Building options for each of the lists
btns_x = [
    dict(
        label=c,
        method="update",
        args=[
            {"x": temp[c].fillna(0).values,
             'xaxis': {'title': c}
             }],
    ) for c in col_list]


btns_y = [
    dict(
        label=c,
        method="update",
        args=[
            {"y": temp[c].fillna(0).values,
             'yaxis': {'title': c}
             }],
    ) for c in col_list]

# Adding the lists to the figure
fig.update_layout(
    updatemenus=[
        dict(
            buttons=btns_x,
            # method="update",
            direction="down",
            pad={"r": 10, "t": 10},
            showactive=True,
            x=0.1,
            xanchor="left",
            y=1.1,
            yanchor="top"
        ),
        dict(
            buttons=btns_y,
            # method="update",
            direction="down",
            pad={"r": 10, "t": 10},
            showactive=True,
            x=0.1,
            xanchor="right",
            y=1.1,
            yanchor="top"
        ),
    ]
)
fig.update_layout(width=1000, height=1000)
fig.show()

The figure draws correctly initially:
enter image description here

Still, there are a few problems:

  1. When I change values in a dropdown, it only works once. On the next tries nothing happens
  2. If I first change the value on one dropdown and then on the other, all data disappears from the graph (see screenshot below)
  3. The axes labels are not being updated
    enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此刻的回忆 2025-01-25 06:44:50

这几乎是在列表综合上进行系统的。以下完全有效,允许选择任何列并更新适当的轴标题。

import pandas as pd
import numpy as np
import plotly.express as px

temp = pd.DataFrame(np.random.randint(0, 1000, (100, 10)))
col_list = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
temp.columns = col_list

fig = px.scatter(temp, x="A", y="B")

fig.update_layout(
    updatemenus=[
        {
            "buttons": [
                {
                    "label": c,
                    "method": "update",
                    "args": [
                        {axis: [temp[c]]},
                        {f"{axis}axis": {"title": {"text": c}}},
                    ],
                }
                for c in temp.columns
            ],
            "x": 0 if axis == "x" else 0.1,
            "y": 1.2,
        }
        for axis in "xy"
    ]
)

It's just about being systematic around the list comprehensions. Below fully works, allows selection of any column and updates appropriate axis title.

import pandas as pd
import numpy as np
import plotly.express as px

temp = pd.DataFrame(np.random.randint(0, 1000, (100, 10)))
col_list = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
temp.columns = col_list

fig = px.scatter(temp, x="A", y="B")

fig.update_layout(
    updatemenus=[
        {
            "buttons": [
                {
                    "label": c,
                    "method": "update",
                    "args": [
                        {axis: [temp[c]]},
                        {f"{axis}axis": {"title": {"text": c}}},
                    ],
                }
                for c in temp.columns
            ],
            "x": 0 if axis == "x" else 0.1,
            "y": 1.2,
        }
        for axis in "xy"
    ]
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文