如何在 plotly express scatter 中为多种颜色设置一条趋势线?
How to have just one trendline for multiple colors in plotly express scatter?
我想创建一个只有一条趋势线的散点图。 Plotly Express 为点列表中的每种颜色创建不同的趋势线。
import plotly.express as px
value = [15, 20, 35, 40, 48]
years = [2010, 2011, 2012, 2013, 2014]
colors = ['red', 'red', 'blue', 'blue', 'blue']
fig = px.scatter(
x=years,
y=value,
trendline='ols',
color=colors
)
fig.show()
有没有办法为所有点创建一条趋势线?
剧情:
提前致谢!
目前没有内置功能,不,很遗憾!但这是个好主意,我创建了一个问题来建议将其作为补充:https://github.com/plotly/plotly.py/issues/1846
随着 Plotly 的发布 5.2.1 (2021-08-13)
使用 px.scatter()
允许您指定:
trendline_scope = 'overall'
地块 1 - trendline_scope = 'overall'
如果您不喜欢趋势线的绿色,您可以通过以下方式进行更改:
trendline_color_override = 'black'
情节 2 - trendline_color_override = 'black'
trendline_scope
的另一个选项是trace
,它产生:
情节 3 - trendline_scope = 'trace'
完整代码:
import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip",
color="sex",
trendline="ols",
trendline_scope = 'overall',
# trendline_scope = 'trace'
trendline_color_override = 'black'
)
fig.show()
旧版本的先前答案:
因为你没有特别要求内置的 plotly express 功能,你可以轻松地在 px.Scatter()
的基础上构建并使用 [=23 获得你想要的东西=] 连同 add_traces(go.Scatter())
:
剧情:
代码:
import plotly.express as px
import plotly.graph_objs as go
import statsmodels.api as sm
value = [15, 20, 35, 40, 48]
years = [2010, 2011, 2012, 2013, 2014]
colors = ['red', 'red', 'blue', 'blue', 'blue']
# your original setup
fig = px.scatter(
x=years,
y=value,
color=colors
)
# linear regression
regline = sm.OLS(value,sm.add_constant(years)).fit().fittedvalues
# add linear regression line for whole sample
fig.add_traces(go.Scatter(x=years, y=regline,
mode = 'lines',
marker_color='black',
name='trend all')
)
fig
你可以同时拥有它:
剧情:
代码更改: 只需添加 trendline='ols'
fig = px.scatter(
x=years,
y=value,
trendline='ols',
color=colors
)
我想创建一个只有一条趋势线的散点图。 Plotly Express 为点列表中的每种颜色创建不同的趋势线。
import plotly.express as px
value = [15, 20, 35, 40, 48]
years = [2010, 2011, 2012, 2013, 2014]
colors = ['red', 'red', 'blue', 'blue', 'blue']
fig = px.scatter(
x=years,
y=value,
trendline='ols',
color=colors
)
fig.show()
有没有办法为所有点创建一条趋势线?
剧情:
提前致谢!
目前没有内置功能,不,很遗憾!但这是个好主意,我创建了一个问题来建议将其作为补充:https://github.com/plotly/plotly.py/issues/1846
随着 Plotly 的发布 5.2.1 (2021-08-13)
使用 px.scatter()
允许您指定:
trendline_scope = 'overall'
地块 1 - trendline_scope = 'overall'
如果您不喜欢趋势线的绿色,您可以通过以下方式进行更改:
trendline_color_override = 'black'
情节 2 - trendline_color_override = 'black'
trendline_scope
的另一个选项是trace
,它产生:
情节 3 - trendline_scope = 'trace'
完整代码:
import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip",
color="sex",
trendline="ols",
trendline_scope = 'overall',
# trendline_scope = 'trace'
trendline_color_override = 'black'
)
fig.show()
旧版本的先前答案:
因为你没有特别要求内置的 plotly express 功能,你可以轻松地在 px.Scatter()
的基础上构建并使用 [=23 获得你想要的东西=] 连同 add_traces(go.Scatter())
:
剧情:
代码:
import plotly.express as px
import plotly.graph_objs as go
import statsmodels.api as sm
value = [15, 20, 35, 40, 48]
years = [2010, 2011, 2012, 2013, 2014]
colors = ['red', 'red', 'blue', 'blue', 'blue']
# your original setup
fig = px.scatter(
x=years,
y=value,
color=colors
)
# linear regression
regline = sm.OLS(value,sm.add_constant(years)).fit().fittedvalues
# add linear regression line for whole sample
fig.add_traces(go.Scatter(x=years, y=regline,
mode = 'lines',
marker_color='black',
name='trend all')
)
fig
你可以同时拥有它:
剧情:
代码更改: 只需添加 trendline='ols'
fig = px.scatter(
x=years,
y=value,
trendline='ols',
color=colors
)