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

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

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

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()
##

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

python data-visualization
1个回答
0
投票

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

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()
© www.soinside.com 2019 - 2024. All rights reserved.