简体   繁体   中英

How to Exclude specific dates from X-axis in Plotly Express

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 在此处输入图像描述

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:

在此处输入图像描述

Plot 2:

在此处输入图像描述

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
loolow = 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 < loolow]))

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

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

fig.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM