简体   繁体   English

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

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

These two pictures below are what I want to do, but the buttons do not work.下面这两张图是我想做的,但是按钮不起作用。

https://imgur.com/qlrLBqu https://imgur.com/qlrLBqu

https://imgur.com/8AGG340 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()
##

What should I do to fix this part, or I need to use another other library to do this work?我应该怎么做才能修复这部分,或者我需要使用另一个其他库来完成这项工作?

OK, I got the answer from other place like this.好的,我从其他地方得到了这样的答案。

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.

相关问题 如何向 Plotly Express 图形添加按钮以更新特定值? - How to add a button to a Plotly Express graph to update a specific value? 如何使用plotly python中的按钮更改数据和图形? - How to change data and graph using buttons in plotly python? 如何使用 Python 中的新数据更新绘图图? - How to update a plotly graph with new data in Python? 如何在 Python 中更改绘图条形图中的图例名称 - How to change the legend names in a plotly bar graph in Python 如何使用 python 中的破折号 plotly 更改图表的 colors - How do I change the colors of the graph using dash plotly in python 如何在 python 动画散点图 plotly 图中添加动画水平线? - How to add an animated horizontal line in a python animated scatter plotly graph? 如何在现有 plotly 图中添加更多数据? - How to add up more data in an existing plotly graph? 如何使用python制作具有正确值计数的绘图条形图? - How to make a plotly bar graph with the correct value count with python? 如何在 Python 中的 Plotly Dash Graph 上获取点击事件坐标数据? - How to obtain Click Event Coordinate Data on Plotly Dash Graph in Python? 情节:如何在情节折线图中的特定点添加标记(python/pandas) - Plotly: How to add markers at specific points in plotly line graph (python / pandas)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM