【发布时间】:2022-11-01 12:41:48
【问题描述】:
我想创建一个情节图,我可以通过button 更改情节表达color 参数的值。我为此使用plotly.express.scatter。
例如,显示的初始图是px.scatter(df, "sepal_length", "sepal_width", color="species")。在下拉菜单中从“物种”更改为“花瓣长度”将更新绘图,以便改为color="petal_length"。如果有所不同,“species”使用默认的离散颜色序列,而“petal_length”使用默认的连续色标。
到目前为止,我的代码制作了初始绘图和下拉按钮,但选择按钮没有效果。我不明白如何通过这个 Plotly.update 接口传递 plotly express color 参数。
import plotly.express as px
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv")
fig = px.scatter(df, "sepal_length", "sepal_width", color="species")
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(
args=["color", "species"],
label="species",
method="update"
),
dict(
args=["color", "petal_length"],
label="petal length",
method="update"
),
]),
showactive=True,
x=0.05,
xanchor="left",
y=1.06,
yanchor="top"
),
]
)
fig.update_layout(
annotations=[
dict(text="color", x=0.015, xref="paper", y=1.05, yref="paper",
align="left", showarrow=False),
])
fig.show()
【问题讨论】:
标签: python pandas plot plotly plotly-express