[英]How to add a button to a Plotly Express graph to update a specific value?
[英]How to add button to change value of data parameter of Plotly graph in Python
下面這兩張圖是我想做的,但是按鈕不起作用。
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.