如何解决在plotly python中自动测距y轴
我想以 plotly 绘制图形。使用 rangelider 设置缩放范围时,y 轴是固定的,并使数据平坦。我找到了一个专家做的页面,但我做不到
有没有人可以做到这一点
https://community.plotly.com/t/y-axis-autoscaling-with-x-range-sliders/10245/11
解决方法
这是相同的技术,但使用破折号来捕获事件并触发回调。其他参考:graph slider as input
from jupyter_dash import JupyterDash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input,Output,State
import plotly.express as px
import numpy as np
import pandas as pd
# generate some data and a figure...
s = 500
df = pd.DataFrame({"date": pd.date_range("1-jan-2020",periods=s),"val": np.random.uniform(0,1,s)})
df = df.assign(val=df["val"] * (df["date"] - df["date"].min()).dt.days ** 3)
fig = px.line(df,x="date",y="val")
fig = fig.update_layout(xaxis={"rangeslider": {"visible": True},"range":[df["date"].quantile(.75),df["date"].max()]})
# Build App
app = JupyterDash(__name__)
app.layout = html.Div([
dcc.Graph(id='dashFig',figure=fig),],style={"font-family": "Arial","font-size":"0.9em"})
# get callback from rangeslider and update ranges on x & y axis
@app.callback(Output('dashFig','figure'),[Input('dashFig','relayoutData')])
def rangesliderchg(relayoutData):
if relayoutData and "xaxis.range" in relayoutData.keys():
s = df.loc[df["date"].between(relayoutData['xaxis.range'][0],relayoutData['xaxis.range'][1]),"val"]
fig["layout"]["yaxis"]["range"] = [s.min(),s.max()]
fig["layout"]["xaxis"]["range"] = relayoutData['xaxis.range']
return fig
# Run app and display result inline in the notebook
app.run_server(mode='inline')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。