Plotly Express:如何在构建图形后从 X-axis 中排除特定日期?
Plotly Express: How to exclude specific dates from the X-axis after building a figure?
我有一个简单的数据框,其中包含日期和一些 headers。我需要从情节中删除特定日期。
fig1 = px.line(df, x=Date, y="Header1")
fig1.show()
我想从图表本身(而不是数据框)中删除值,例如(删除 15/01/2022 和 22/02/2022)。
日期与价值图
我很可能宁愿使用用于构建图形的数据集,而不是图中本身。但是这个建议应该完全符合您的要求。 如何 find the outliers 将完全取决于您。给定一些阈值 toolow, tohigh
,下面的代码片段会将 Plot 1 变成 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)
地块 1:
情节 2:
完整代码:
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()
我有一个简单的数据框,其中包含日期和一些 headers。我需要从情节中删除特定日期。
fig1 = px.line(df, x=Date, y="Header1")
fig1.show()
我想从图表本身(而不是数据框)中删除值,例如(删除 15/01/2022 和 22/02/2022)。
日期与价值图
我很可能宁愿使用用于构建图形的数据集,而不是图中本身。但是这个建议应该完全符合您的要求。 如何 find the outliers 将完全取决于您。给定一些阈值 toolow, tohigh
,下面的代码片段会将 Plot 1 变成 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)
地块 1:
情节 2:
完整代码:
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()