我试图从折线图中排除非工作时间和周末,但使用以下数据不起作用:
数据:
绘制折线图的代码:
fig = px.line(df, x=df.index, y='close')
fig.update_xaxes(rangebreaks=[dict(bounds=[16, 9.5], pattern="hour"),
dict(bounds=['sat', 'mon'])])
fig.show()
折线图的输出:
如果我使用其他类型的数据,这将有效,就像此链接的示例一样: https://plotly.com/python/time-series/
import plotly.express as px
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
fig = px.scatter(df, x='Date', y='AAPL.High', range_x=['2015-12-01', '2016-01-15'],
title="Hide Weekend and Holiday Gaps with rangebreaks")
fig.update_xaxes(
rangebreaks=[
dict(bounds=["sat", "mon"]), #hide weekends
dict(values=["2015-12-25", "2016-01-01"]) # hide Christmas and New Year's
]
)
fig.show()
最佳答案
我无法通过plotly express 来做到这一点。 px.bar 有效,px.line 或 px.scatter 无效。所以我深入到 graph_objects,如下所示:
import plotly.graph_objects as go
fig = go.Figure()
fig.update_layout(width=1600, height=1200)
fig.add_trace(go.Scatter(x=df.index, y=df['data']))
fig.update_xaxes(
rangebreaks=[
dict(bounds=[17.5, 9.5], pattern="hour"),
dict(bounds=["sat", "mon"]),
]
)
仍然不明白为什么 px.line 方式不起作用..
关于python - 不显示折线图范围划分以排除 python 中的非工作时间和周末,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72282359/