python - 在绘图散点图中按下按钮时更新轴

标签 python python-3.x plotly scatter-plot

我有几个数字列表,我想以成对的方式比较它们。我尝试使用 Plotly 制作散点图并添加按钮,我希望能够更改 x 轴或 y 轴上的列表。

使用example in the documentation ,我设法创建了绘图和按钮。但是,当我选择另一个列表时,轴不会更新。

这是我的代码和示例:

import plotly
from plotly import graph_objs as go, offline as po, tools
po.init_notebook_mode()

import numpy as np

l1 = list(np.random.randint(100, size=100))
l2 = list(np.random.rand(100))
l3 = list(np.random.randint(100, size=100))

# Create figure
fig = go.Figure()

# Add scatter
fig.add_trace(go.Scatter(
     x=l1,
     y=l1,
    mode = 'markers'
)
             )

# Add drowdowns
button_layer_1_height = 1.15
fig.update_layout(
    updatemenus=[
        go.layout.Updatemenu(
            buttons=list([
                dict(
                    args=["x", l1],
                    label="L1",
                    method="restyle"
                ),
                dict(
                    args=["x", l2],
                    label="L2",
                    method="restyle"
                ),
                dict(
                    args=["x", l3],
                    label="L3",
                    method="restyle"
                ),
            ]),
            direction="down",
            pad={"r": 10, "t": 10},
            showactive=True,
            x=0.02,
            xanchor="left",
            y=button_layer_1_height,
            yanchor="top"
        ),
        go.layout.Updatemenu(
            buttons=list([
                dict(
                    args=["y", l1],
                    label="L1",
                    method="restyle"
                ),
                dict(
                    args=["y", l2],
                    label="L2",
                    method="restyle"
                ),
                dict(
                    args=["y", l3],
                    label="L3",
                    method="restyle"
                ), 
            ]),
            direction="down",
            pad={"r": 10, "t": 10},
            showactive=True,
            x=0.15,
            xanchor="left",
            y=button_layer_1_height,
            yanchor="top"
        ),
    ]
)

fig.update_layout(
    annotations=[
        go.layout.Annotation(text="x", x=0, xref="paper", y=1.08, yref="paper",
                             align="left", showarrow=False),
        go.layout.Annotation(text="y", x=0.13, xref="paper", y=1.08,
                             yref="paper", showarrow=False),
    ])

fig.show()

最佳答案

args 中,您的列表(例如 L1)需要是列表的元素,而不仅仅是列表本身。因此,只需将所有 args=["x", l1] 更改为 args=["x", [l1]] 就可以了!听起来好得令人难以置信?这是一些证据

第一次运行时的绘图:

enter image description here

为 x 选择 L2 时的绘图:

enter image description here

为 y 选择 L3 时的绘图:

enter image description here

这是您编辑后的代码:

import plotly
from plotly import graph_objs as go, offline as po, tools
po.init_notebook_mode()

import numpy as np

l1 = list(np.random.randint(100, size=100))
l2 = list(np.random.rand(100))
l3 = list(np.random.randint(100, size=100))

# Create figure
fig = go.Figure()

# Add scatter
fig.add_trace(go.Scatter(
     x=l1,
     y=l1,
    mode = 'markers'
)
             )

# Add drowdowns
button_layer_1_height = 1.15
fig.update_layout(
    updatemenus=[
        go.layout.Updatemenu(
            buttons=list([
                dict(
                    args=["x", [l1]],
                    label="L1",
                    method="restyle"
                ),
                dict(
                    args=["x", [l2]],
                    label="L2",
                    method="restyle"
                ),
                dict(
                    args=["x", [l3]],
                    label="L3",
                    method="restyle"
                ),
            ]),
            direction="down",
            pad={"r": 10, "t": 10},
            showactive=True,
            x=0.02,
            xanchor="left",
            y=button_layer_1_height,
            yanchor="top"
        ),
        go.layout.Updatemenu(
            buttons=list([
                dict(
                    args=["y", [l1]],
                    label="L1",
                    method="restyle"
                ),
                dict(
                    args=["y", [l2]],
                    label="L2",
                    method="restyle"
                ),
                dict(
                    args=["y", [l3]],
                    label="L3",
                    method="restyle"
                ), 
            ]),
            direction="down",
            pad={"r": 10, "t": 10},
            showactive=True,
            x=0.15,
            xanchor="left",
            y=button_layer_1_height,
            yanchor="top"
        ),
    ]
)

fig.update_layout(
    annotations=[
        go.layout.Annotation(text="x", x=0, xref="paper", y=1.08, yref="paper",
                             align="left", showarrow=False),
        go.layout.Annotation(text="y", x=0.13, xref="paper", y=1.08,
                             yref="paper", showarrow=False),
    ])

fig.show()

关于python - 在绘图散点图中按下按钮时更新轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58974513/

相关文章:

python - Ipython 笔记本 3 禁用 seaborn 设置

python - 在没有教师强制的情况下使用 LSTM 解码器 - Tensorflow

python - 无法加载资源: the server responded with a status of 405 (METHOD NOT ALLOWED) in django

python - 如何将具有一列的 csv 文件转换为 Python 中的字典?

python - 如何从列表中删除项目并将其存储到Python中的变量中

python - Plotly:如何交替背景网格颜色?

c++ - 我可以在扩展 Python 的同时使用 C++ 功能吗?

python-3.x - 使用 Matplotlib 设置多组次要刻度

python - 使用 Dash,有没有办法在页面的两个部分中重复相同的 id 组件?

python - 相对频率直方图,其中纵轴是频率 Python