繁体   English   中英

如何在 Python 中添加按钮以更改 Plotly 图形的数据参数值

[英]How to add button to change value of data parameter of Plotly graph in Python

下面这两张图是我想做的,但是按钮不起作用。

https://imgur.com/qlrLBqu

https://imgur.com/8AGG340

import plotly.express as px
df = px.data.tips()

fig = px.scatter(df, x="total_bill", y="tip", color="smoker")

## How to fix this part?
fig.update_layout(
    updatemenus=[
        dict(
            type = "buttons",
            direction = "left",
            buttons=list([
                dict(
                    args=["color", "sex"],
                    label="sex",
                    method="update"
                ),
                dict(
                    args=["color", "smoker"],
                    label="smoker",
                    method="update"
                )])),])

fig.show()
##

我应该怎么做才能修复这部分,或者我需要使用另一个其他库来完成这项工作?

好的,我从其他地方得到了这样的答案。

import plotly.express as px
import plotly.graph_objects as go
import plotly
df = px.data.tips()

fig = px.scatter(df, x="total_bill", y="tip", color="sex")
fig.add_trace(px.scatter(df, x="total_bill", y="tip", color="smoker").data[0])
fig.add_trace(px.scatter(df, x="total_bill", y="tip", color="smoker").data[1])


updatemenus=[dict(type = "buttons", direction = "left",
             buttons=list([
             dict(args=[{'visible': [True  , True  , False , False ]} ,],
                  label = "sex"   , method="update"),

             dict(args=[{'visible': [False , False , True  , True  ]} ,],
                  label = "smoker", method="update")
             ])),]

fig.update_layout(updatemenus = updatemenus,
                  legend_title_text='')

fig.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM