python - python - 如何在python的Plotly图表中获取滑块的位置?
问题描述
我正在使用 plotly 库在 python 中制作图表。该图表有一个滑块,我想检索滑块的位置,以便使用它来显示其他内容(不能在绘图中完成)。有没有办法巧妙地返回滑块的位置?
在此先感谢您的帮助!
解决方案
- 您可以使用破折号中的回调来获取滑块值
- 已使用https://plotly.com/python/sliders/#sliders-in-plotly-express作为带有滑块的绘图示例
- 不能进行直接回调,因此也可以使用传入状态的间隔
- 使用活动步骤和活动值更新div
import plotly.express as px
from jupyter_dash import JupyterDash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
df = px.data.gapminder()
fig = px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
size="pop", color="continent", hover_name="country",
log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90])
fig["layout"].pop("updatemenus") # optional, drop animation buttons
# Build App
app = JupyterDash(__name__)
app.layout = html.Div(
[dcc.Graph(id="graph", figure=fig),
dcc.Interval(id="sliderInterval", interval=400, n_intervals=0),
html.Div(id="whichframe", children=[]),
],
)
# core update of figure on change of dash slider
@app.callback(
Output("whichframe", "children"),
Input("sliderInterval", "n_intervals"),
State("graph", "figure")
)
def setFrame(n_intervals, fig):
slider = fig['layout']['sliders'][0]
return f"active:{slider['active']} value:{slider['steps'][slider['active']]['args'][0][0]}"
# Run app and display result inline in the notebook
app.run_server(mode="inline")
推荐阅读
- opengl - 上传索引缓冲区而不更改 VAO 状态
- python - 尝试将动态值传递给 Rundeck 服务器
- javascript - 我应该为函数使用什么 Jest 匹配器?
- linux - 如何使用 shell 脚本创建和插入数据库?
- python-3.x - 如何删除一定长度的字符串?
- python - 稀疏矩阵与向量相乘中的列顺序
- ios - 收到通知时的默认声音和振动
- amazon-web-services - AWS:检查联合登录用户的权限
- python - Plotly:如何设置不同的标记不透明度,但为所有标记保持相同的轮廓颜色?
- ios - 如何修复:当我在搜索栏中单击“取消”时,TableView 中的 func selectRow 不起作用