如何在Python中使用范围滑块自动缩放绘图?

jdgnovmf  于 2022-11-19  发布在  Python
关注(0)|答案(1)|浏览(171)

当我使用范围滑块或在图表本身上选择范围时,我一直试图在plotly散点图(与线条连接的标记-折线图)中添加自动缩放功能。这个问题被问了很多次,但我没有得到任何有效的解决方案。我有一个样本折线图-

import plotly.graph_objs as go

fig = go.Figure()
df.sort_index(ascending=True, inplace=True)

trace = go.Scatter(x=list(df.index), y=list(df.Close))
fig.add_trace(trace)

fig.update_layout(
    dict(
        title="Time series with range slider and selectors",
        xaxis=dict(
            rangeselector=dict(
                buttons=list(
                    [
                        dict(count=1, label="1m", step="month", stepmode="backward"),
                        dict(count=6, label="6m", step="month", stepmode="backward"),
                        dict(count=1, label="YTD", step="year", stepmode="todate"),
                        dict(count=1, label="1y", step="year", stepmode="backward"),
                        dict(step="all"),
                    ]
                )
            ),
            rangeslider=dict(visible=True),
            type="date",
        ),
    )
)
fig

每当我使用范围滑块选择一个范围,并选择图表本身的范围,我想有自动调整Y轴,这将调整其范围,以当前可见的数据集范围。我该如何做呢?
谢谢你!

EDIT:我添加了示例数据,如果有用,我将获取这些数据.

import yfinance as yf
vix_tickers = ['AUDJPY=X']

df = yf.download(vix_tickers,
                 auto_adjust=True, #only download adjusted data
                 progress=False,
            )
df = df[["Close"]]
df

看起来像这样-

k5ifujac

k5ifujac1#

我用了这个fig.update_layout(yaxis=dict(autorange=True, fixedrange=False))来自动缩放y轴,但我看到它只对图表本身范围选择起作用。
下面是完整的代码:

import plotly.graph_objs as go

fig = go.Figure()
df.sort_index(ascending=True, inplace=True)

trace = go.Scatter(x=list(df.index), y=list(df.Close))
fig.add_trace(trace)

fig.update_layout(
    dict(
        title="Time series with range slider and selectors",
        xaxis=dict(
            rangeselector=dict(
                buttons=list(
                    [
                        dict(count=1, label="1m", step="month", stepmode="backward"),
                        dict(count=6, label="6m", step="month", stepmode="backward"),
                        dict(count=1, label="YTD", step="year", stepmode="todate"),
                        dict(count=1, label="1y", step="year", stepmode="backward"),
                        dict(step="all"),
                    ]
                )
            ),
            rangeslider=dict(visible=True),
            type="date",
        ),
    )
)
fig.update_layout(yaxis=dict(autorange=True, fixedrange=False))

fig.show()

结果如下:

相关问题