1

I'm trying to exclude non-business hours AND weekends from the line chart, but it's not working using the following data:

The data:

The data

The code to plot the line chart:

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()

The Output of the line chart:

enter image description here

If I use another type of data, this works, like the example of this link: 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()

1 Answers1

1

I could not get to do it via plotly express. px.bar worked, px.line or px.scatter did not. So I went lower into graph_objects, like so:

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"]),
    ]
)

Still do not understand why the px.line way does not work though..

Kocas
  • 302
  • 1
  • 12