admin管理员组

文章数量:1277588

Since 2025/2/17 is a holiday, It should be removed from plot.rangebreaks looks exactly for this case, but,it generate a very weird graph.

full code:

#!/usr/bin/env python3
import yfinance as yf
import plotly.graph_objs as go
from plotly.subplots import make_subplots
import pandas as pd
import datetime

STK="AMZN"
end_time = datetime.datetime.strptime("2025-02-19","%Y-%m-%d")
start_time = end_time - datetime.timedelta(days=7)
df = yf.Ticker(STK).history(start=start_time,end=end_time,interval="30m")

fig = make_subplots(rows=1, cols=1,
                        vertical_spacing=0.01,
                        #shared_xaxes=True
                        )

trace = go.Candlestick(
            x=df.index,
            open=df["Open"],
            high=df["High"],
            low=df["Low"],
            close=df["Close"],
        )
fig.add_trace(trace)

fig.update_xaxes(
    rangebreaks=[
       dict(bounds=["sat", "mon"]),
       dict(values=["2025-02-17","2025-12-25"]),
       dict(bounds=[16, 9.5], pattern="hour")
    ]
)
fig.show()

Output:

If comment out the second line in rangebreaks, output graph as below:

The expected output should be cut the range of 2025-02-17

本文标签: pythonplotlyremove holiday from stock plot not work as expectedStack Overflow