微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在plotly python中自动测距y轴

如何解决在plotly python中自动测距y轴

我想以 plotly 绘制图形。使用 rangelider 设置缩放范围时,y 轴是固定的,并使数据平坦。我找到了一个专家做的页面,但我做不到

有没有人可以做到这一点

https://community.plotly.com/t/y-axis-autoscaling-with-x-range-sliders/10245/11

在这页面上,第二张图是一个动画gif,这正是我要做

解决方法

这是相同的技术,但使用破折号来捕获事件并触发回调。其他参考: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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?