[英]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.