DeepakB
DeepakB

Reputation: 21

Plotly Express: How to exclude specific dates from the X-axis after building a figure?

I have a simple dataframe containing dates and a few headers. I need to remove specific dates from the plot.

fig1 = px.line(df, x=Date, y="Header1")
fig1.show()

I want to remove values from the chart itself (not from dataframe), like (removing 15/01/2022 & 22/02/2022).

date vs value plot enter image description here

Upvotes: 1

Views: 1527

Answers (1)

vestland
vestland

Reputation: 61224

I would most likely rather do this with the dataset used to build your figure, instead of in the figure itself. But this suggestion should do exactly what you're asking for. How you find the outliers will be entirely up to you. Given some thresholds toolow, tohigh, the snippet below will turn Plot 1 into Plot 2

fig.for_each_trace(lambda t: highOutliers.extend([t.x[i] for i, val in enumerate(t.y) if val > toohigh]))
fig.for_each_trace(lambda t: lowOutliers.extend([t.x[i] for i, val in enumerate(t.y) if val < loolow]))

fig.update_xaxes(
    rangebreaks=[dict(values=highOutliers+lowOutliers)]
)

fig.update_traces(connectgaps=True)

Plot 1:

enter image description here

Plot 2:

enter image description here

Complete code:

from numpy import random
import datetime
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import numpy as np

# Some sample data
y = np.random.normal(50, 5, 15)
datelist = pd.to_datetime(pd.date_range(datetime.datetime(2020, 1, 1).strftime('%Y-%m-%d'),periods=len(y)).tolist())
df = pd.DataFrame({'date':datelist, 'y':y})

# Introduce some outliers
df.loc[5,'y'] = 120
df.loc[10,'y'] = 2

# build figure
fig = px.line(df, x = 'date', y = 'y')

# containers and thresholds for outliers
highOutliers = []
lowOutliers = []
toohigh = 100
toolow = 20

# find outliers
fig.for_each_trace(lambda t: highOutliers.extend([t.x[i] for i, val in enumerate(t.y) if val > toohigh]))
fig.for_each_trace(lambda t: lowOutliers.extend([t.x[i] for i, val in enumerate(t.y) if val < toolow]))

# define outliers as rangebreaks
fig.update_xaxes(
    rangebreaks=[dict(values=highOutliers+lowOutliers)]
)

# connect gaps in the line
fig.update_traces(connectgaps=True)

fig.show()

Upvotes: 1

Related Questions