如何在 plotly express 散点图中填充趋势线下方的区域?
How to fill in the area below trendline in plotly express scatterplot?
我画了一个带有非线性趋势线和垂直线的散点图:
import plotly.express as px
uk_age = df[df['CountryLive']=='United Kingdom']['Age'].mean()
fig = px.scatter(df, x=x, y=y, trendline="lowess", trendline_options=dict(frac=0.4),
trendline_color_override="red",template='plotly_white', range_y=[0,30])
fig.add_vline(x=uk_age, annotation_text="UK",
annotation_position="top right", line_color="blue")
fig.show()
- 我想填充趋势线下方的区域。
- 我想填充趋势线下方的区域,但只能在趋势线的右侧
vline(如下图,为我的美术技巧道歉)
我试过提取趋势线结果并再次绘制它们,但无法填充它们。事实上,它是一条平滑的趋势线 (frac=0.4),而不是 'normal' 绘图线,这使它成为一项艰巨的任务。有什么想法吗?
我想我可以通过获取趋势线绘制数据并用它绘制面积图来获得所需的输出。不知道是什么原因,设置透明度不行,所以我用颜色给它加了阴影。
import plotly.express as px
import plotly.graph_objects as go
df = px.data.tips()
fig = px.scatter(df,
x="total_bill",
y="tip",
trendline="lowess",
trendline_options=dict(frac=0.1),
trendline_color_override='red',
template='plotly_white'
)
x_pos = 9.94
x = fig.data[1]['x']
y = fig.data[1]['y']
x = [pos for pos in x if pos >= x_pos]
y = y[len(y) - len(x):]
fig.add_trace(go.Scatter(x=x, y=y,
line_color='rgba(147,112,219,0.3)',
fillcolor='pink',
opacity=0.1,
fill='tozeroy'))
fig.add_vline(x=x_pos, annotation_text='UK',annotation_position='top right', line_color='Blue')
fig.show()
我画了一个带有非线性趋势线和垂直线的散点图:
import plotly.express as px
uk_age = df[df['CountryLive']=='United Kingdom']['Age'].mean()
fig = px.scatter(df, x=x, y=y, trendline="lowess", trendline_options=dict(frac=0.4),
trendline_color_override="red",template='plotly_white', range_y=[0,30])
fig.add_vline(x=uk_age, annotation_text="UK",
annotation_position="top right", line_color="blue")
fig.show()
- 我想填充趋势线下方的区域。
- 我想填充趋势线下方的区域,但只能在趋势线的右侧 vline(如下图,为我的美术技巧道歉)
我试过提取趋势线结果并再次绘制它们,但无法填充它们。事实上,它是一条平滑的趋势线 (frac=0.4),而不是 'normal' 绘图线,这使它成为一项艰巨的任务。有什么想法吗?
我想我可以通过获取趋势线绘制数据并用它绘制面积图来获得所需的输出。不知道是什么原因,设置透明度不行,所以我用颜色给它加了阴影。
import plotly.express as px
import plotly.graph_objects as go
df = px.data.tips()
fig = px.scatter(df,
x="total_bill",
y="tip",
trendline="lowess",
trendline_options=dict(frac=0.1),
trendline_color_override='red',
template='plotly_white'
)
x_pos = 9.94
x = fig.data[1]['x']
y = fig.data[1]['y']
x = [pos for pos in x if pos >= x_pos]
y = y[len(y) - len(x):]
fig.add_trace(go.Scatter(x=x, y=y,
line_color='rgba(147,112,219,0.3)',
fillcolor='pink',
opacity=0.1,
fill='tozeroy'))
fig.add_vline(x=x_pos, annotation_text='UK',annotation_position='top right', line_color='Blue')
fig.show()