Plotly:如何针对多个其他时间序列显示一个变量的回归线?
Plotly: How to display a regression line for one variable against multiple other time series?
对于各种股票的时间序列等数据集,如何轻松显示一个变量相对于所有其他变量的回归线并快速定义一些美学元素,例如:
- 根据其他变量绘制哪个变量,
- 图形的主题颜色,
- 痕迹的色阶
- 趋势线类型;线性还是非线性?
数据:
date GOOG AAPL AMZN FB NFLX MSFT
100 2019-12-02 1.216280 1.546914 1.425061 1.075997 1.463641 1.720717
101 2019-12-09 1.222821 1.572286 1.432660 1.038855 1.421496 1.752239
102 2019-12-16 1.224418 1.596800 1.453455 1.104094 1.604362 1.784896
103 2019-12-23 1.226504 1.656000 1.521226 1.113728 1.567170 1.802472
104 2019-12-30 1.213014 1.678000 1.503360 1.098475 1.540883 1.788185
可重现通过:
import pandas as pd
import plotly.express as px
df = px.data.stocks()
精华:
target = 'GOOG'
fig = px.scatter(df, x = target,
y = [c for c in df.columns if c != target],
color_discrete_sequence = px.colors.qualitative.T10,
template = 'plotly_dark', trendline = 'ols',
title = 'Google vs. the world')
详情:
使用最新版本的 plotly.express (px) and px.scatter,这些事情既简单、直接又灵活。下面的代码片段将完全按照问题中的要求执行。
首先,从数据框列中定义一个 target = 'GOOG
。然后,使用 `px.scatter() 你可以:
- 使用
y = [c for c in df.columns if c != target]
针对目标绘制其余列
- Select 通过
template='plotly_dark')
的主题或使用 pio.templates
找到另一个主题。
- Select 通过
color_discrete_sequence = px.colors.qualitative.T10
的痕迹的配色方案或使用 dir(px.colors.qualitative)
找到另一个配色方案
- 通过
trendline = 'ols'
或trendline = 'lowess'
定义趋势估计方法
(下图是用宽格式的数据源制作的。经过一些非常小的修改,px.scatter()
可以轻松处理长格式的数据。)
情节
完整代码:
# imports
import pandas as pd
import plotly.express as px
import plotly.io as pio
# data
df = px.data.stocks()
df = df.drop(['date'], axis = 1)
# your choices
target = 'GOOG'
colors = px.colors.qualitative.T10
# plotly
fig = px.scatter(df,
x = target,
y = [c for c in df.columns if c != target],
template = 'plotly_dark',
color_discrete_sequence = colors,
trendline = 'ols',
title = 'Google vs. the world')
fig.show()
对于各种股票的时间序列等数据集,如何轻松显示一个变量相对于所有其他变量的回归线并快速定义一些美学元素,例如:
- 根据其他变量绘制哪个变量,
- 图形的主题颜色,
- 痕迹的色阶
- 趋势线类型;线性还是非线性?
数据:
date GOOG AAPL AMZN FB NFLX MSFT
100 2019-12-02 1.216280 1.546914 1.425061 1.075997 1.463641 1.720717
101 2019-12-09 1.222821 1.572286 1.432660 1.038855 1.421496 1.752239
102 2019-12-16 1.224418 1.596800 1.453455 1.104094 1.604362 1.784896
103 2019-12-23 1.226504 1.656000 1.521226 1.113728 1.567170 1.802472
104 2019-12-30 1.213014 1.678000 1.503360 1.098475 1.540883 1.788185
可重现通过:
import pandas as pd
import plotly.express as px
df = px.data.stocks()
精华:
target = 'GOOG'
fig = px.scatter(df, x = target,
y = [c for c in df.columns if c != target],
color_discrete_sequence = px.colors.qualitative.T10,
template = 'plotly_dark', trendline = 'ols',
title = 'Google vs. the world')
详情:
使用最新版本的 plotly.express (px) and px.scatter,这些事情既简单、直接又灵活。下面的代码片段将完全按照问题中的要求执行。
首先,从数据框列中定义一个 target = 'GOOG
。然后,使用 `px.scatter() 你可以:
- 使用
y = [c for c in df.columns if c != target]
针对目标绘制其余列
- Select 通过
template='plotly_dark')
的主题或使用pio.templates
找到另一个主题。 - Select 通过
color_discrete_sequence = px.colors.qualitative.T10
的痕迹的配色方案或使用dir(px.colors.qualitative)
找到另一个配色方案
- 通过
trendline = 'ols'
或trendline = 'lowess'
定义趋势估计方法
(下图是用宽格式的数据源制作的。经过一些非常小的修改,px.scatter()
可以轻松处理长格式的数据。)
情节
完整代码:
# imports
import pandas as pd
import plotly.express as px
import plotly.io as pio
# data
df = px.data.stocks()
df = df.drop(['date'], axis = 1)
# your choices
target = 'GOOG'
colors = px.colors.qualitative.T10
# plotly
fig = px.scatter(df,
x = target,
y = [c for c in df.columns if c != target],
template = 'plotly_dark',
color_discrete_sequence = colors,
trendline = 'ols',
title = 'Google vs. the world')
fig.show()