在 Plotly 中单独显示所有颜色的线性拟合
Show Linear Fits for all colors individually in Plotly
我问的与 . I want to display the trendline for each color individually instead of an overall trendline. Showing individual trendlines is the default as seen in the documentation 完全相反,所以我不确定为什么它不起作用。
代码:
import pandas as pd
import plotly.express as px
# pip install statsmodels
df = pd.read_csv('values.csv')
fig = px.scatter(
df,
x='YEAR',
y='SD',
color='MONTH',
trendline_scope='trace', # same as default
trendline='ols',
)
fig.show()
结果:
我希望像这样的多条趋势线:
数据:
YEAR,MONTH,SD
1980,1,7.3
1980,2,1.75
1980,3,0.9
1980,4,1.09
1980,5,0.11
1980,6,0.03
1980,7,0.03
1980,8,0.01
1980,9,0.0
1980,10,0.01
1980,11,0.91
1980,12,5.67
1981,1,12.82
1981,2,7.54
1981,3,1.82
1981,4,0.21
1981,5,0.05
1981,6,0.02
1981,7,0.02
1981,8,0.01
1981,9,0.0
1981,10,0.09
1981,11,0.57
1981,12,16.23
1982,1,8.43
1982,2,2.75
1982,3,1.92
1982,4,0.54
1982,5,0.1
1982,6,0.02
1982,7,0.01
1982,8,0.0
1982,9,0.0
1982,10,0.0
1982,11,0.09
1982,12,0.66
1983,1,0.94
1983,2,7.67
1983,3,0.78
1983,4,0.22
1983,5,0.04
1983,6,0.02
1983,7,0.01
1983,8,0.0
1983,9,0.0
1983,10,0.0
1983,11,0.13
1983,12,1.5
1984,1,4.33
1984,2,5.88
1984,3,3.81
1984,4,1.12
1984,5,0.04
1984,6,0.02
1984,7,0.01
1984,8,0.0
1984,9,0.0
1984,10,0.01
1984,11,0.06
1984,12,0.36
1985,1,14.18
1985,2,3.82
1985,3,1.82
1985,4,0.2
1985,5,0.08
1985,6,0.01
1985,7,0.01
1985,8,0.0
1985,9,0.0
1985,10,0.0
1985,11,3.34
1985,12,1.11
1986,1,5.09
1986,2,9.18
1986,3,3.14
1986,4,0.69
1986,5,0.03
1986,6,0.02
1986,7,0.0
1986,8,0.0
1986,9,0.0
1986,10,0.0
1986,11,0.0
1986,12,3.02
1987,1,13.14
1987,2,7.3
1987,3,5.3
1987,4,0.46
1987,5,0.06
1987,6,0.03
1987,7,0.01
1987,8,0.0
1987,9,0.0
1987,10,0.0
1987,11,0.16
1987,12,0.18
1988,1,0.45
1988,2,3.77
1988,3,9.79
1988,4,0.79
1988,5,0.04
1988,6,0.03
1988,7,0.02
1988,8,0.0
1988,9,0.0
1988,10,0.0
1988,11,1.08
1988,12,1.38
1989,1,0.42
1989,2,0.47
1989,3,0.27
1989,4,0.06
1989,5,0.02
1989,6,0.02
1989,7,0.01
1989,8,0.0
1989,9,0.0
1989,10,0.0
1989,11,0.4
1989,12,0.36
1990,1,0.43
1990,2,0.74
1990,3,0.34
1990,4,0.06
1990,5,0.03
1990,6,0.02
1990,7,0.01
1990,8,0.0
1990,9,0.0
1990,10,0.0
1990,11,0.28
1990,12,4.02
1991,1,0.53
1991,2,6.64
1991,3,0.11
1991,4,0.12
1991,5,0.04
1991,6,0.02
1991,7,0.0
1991,8,0.0
1991,9,0.0
1991,10,0.01
1991,11,0.21
1991,12,0.8
1992,1,1.02
1992,2,1.65
1992,3,0.87
1992,4,0.29
1992,5,0.04
1992,6,0.02
1992,7,0.01
1992,8,0.0
1992,9,0.0
1992,10,0.04
1992,11,0.13
1992,12,0.4
1993,1,0.73
1993,2,3.35
1993,3,2.28
1993,4,0.08
1993,5,0.03
1993,6,0.02
1993,7,0.01
1993,8,0.0
1993,9,0.0
1993,10,0.01
1993,11,1.32
1993,12,1.7
1994,1,0.67
1994,2,1.9
1994,3,0.31
1994,4,0.24
1994,5,0.03
1994,6,0.02
1994,7,0.0
1994,8,0.0
1994,9,0.0
1994,10,0.0
1994,11,0.01
1994,12,0.36
1995,1,5.46
1995,2,0.55
1995,3,1.34
1995,4,0.28
1995,5,0.04
1995,6,0.03
1995,7,0.01
1995,8,0.0
1995,9,0.0
1995,10,0.0
1995,11,0.79
1995,12,1.41
1996,1,1.23
1996,2,5.43
1996,3,2.51
1996,4,0.38
1996,5,0.02
1996,6,0.01
1996,7,0.01
1996,8,0.0
1996,9,0.0
1996,10,0.0
1996,11,0.95
1996,12,2.98
1997,1,5.4
1997,2,0.83
1997,3,0.22
1997,4,0.14
1997,5,0.04
1997,6,0.03
1997,7,0.02
1997,8,0.01
1997,9,0.0
1997,10,0.01
1997,11,0.01
1997,12,1.08
1998,1,1.18
1998,2,1.22
1998,3,0.42
1998,4,0.1
1998,5,0.02
1998,6,0.02
1998,7,0.01
1998,8,0.0
1998,9,0.0
1998,10,0.01
1998,11,1.35
1998,12,3.78
1999,1,1.63
1999,2,7.77
1999,3,1.35
1999,4,0.3
1999,5,0.05
1999,6,0.04
1999,7,0.03
1999,8,0.01
1999,9,0.0
1999,10,0.0
1999,11,1.47
1999,12,2.35
2000,1,2.83
2000,2,1.28
2000,3,0.74
2000,4,0.22
2000,5,0.03
2000,6,0.02
2000,7,0.01
2000,8,0.01
2000,9,0.0
2000,10,0.0
2000,11,0.01
2000,12,0.91
2001,1,1.12
2001,2,1.89
2001,3,1.09
2001,4,0.23
2001,5,0.02
2001,6,0.02
2001,7,0.01
2001,8,0.0
2001,9,0.01
2001,10,0.0
2001,11,0.31
2001,12,4.5
2002,1,7.62
2002,2,0.69
2002,3,0.29
2002,4,0.05
2002,5,0.03
2002,6,0.02
2002,7,0.01
2002,8,0.0
2002,9,0.0
2002,10,0.0
2002,11,0.05
2002,12,0.25
2003,1,2.51
2003,2,7.27
2003,3,0.65
2003,4,0.12
2003,5,0.02
2003,6,0.01
2003,7,0.0
2003,8,0.0
2003,9,0.0
2003,10,0.04
2003,11,0.02
2003,12,0.65
2004,1,4.32
2004,2,2.52
2004,3,1.79
2004,4,0.1
2004,5,0.03
2004,6,0.03
2004,7,0.01
2004,8,0.0
2004,9,0.0
2004,10,0.0
2004,11,0.54
2004,12,1.12
2005,1,2.6
2005,2,8.91
2005,3,6.79
2005,4,0.09
2005,5,0.03
2005,6,0.02
2005,7,0.01
2005,8,0.0
2005,9,0.0
2005,10,0.0
2005,11,1.38
2005,12,3.78
2006,1,5.01
2006,2,7.22
2006,3,9.61
2006,4,0.52
2006,5,0.03
2006,6,0.03
2006,7,0.0
2006,8,0.0
2006,9,0.0
2006,10,0.0
2006,11,0.05
2006,12,0.1
2007,1,1.06
2007,2,0.52
2007,3,0.54
2007,4,0.03
2007,5,0.01
2007,6,0.01
2007,7,0.0
2007,8,0.0
2007,9,0.0
2007,10,0.03
2007,11,1.25
2007,12,0.69
2008,1,0.6
2008,2,0.31
2008,3,1.2
2008,4,0.2
2008,5,0.04
2008,6,0.02
2008,7,0.0
2008,8,0.0
2008,9,0.0
2008,10,0.03
2008,11,0.98
2008,12,1.65
2009,1,3.26
2009,2,5.76
2009,3,2.66
2009,4,0.38
2009,5,0.05
2009,6,0.03
2009,7,0.02
2009,8,0.0
2009,9,0.02
2009,10,0.07
2009,11,0.01
2009,12,1.71
2010,1,10.45
2010,2,15.67
2010,3,2.54
2010,4,0.07
2010,5,0.04
2010,6,0.03
2010,7,0.01
2010,8,0.0
2010,9,0.0
2010,10,0.03
2010,11,0.89
2010,12,17.19
2011,1,6.01
2011,2,1.12
2011,3,0.16
2011,4,0.02
2011,5,0.02
2011,6,0.01
2011,7,0.0
2011,8,0.0
2011,9,0.0
2011,10,0.0
2011,11,0.0
2011,12,0.94
2012,1,1.87
2012,2,4.2
2012,3,0.73
2012,4,0.12
2012,5,0.05
2012,6,0.03
2012,7,0.01
2012,8,0.0
2012,9,0.0
2012,10,0.2
2012,11,0.12
2012,12,4.55
2013,1,3.61
2013,2,5.52
2013,3,3.83
2013,4,0.6
2013,5,0.04
2013,6,0.04
2013,7,0.02
2013,8,0.0
2013,9,0.0
2013,10,0.01
2013,11,0.14
2013,12,0.48
2014,1,0.92
2014,2,0.45
2014,3,0.1
2014,4,0.03
2014,5,0.03
2014,6,0.02
2014,7,0.01
2014,8,0.0
2014,9,0.0
2014,10,0.01
2014,11,0.01
2014,12,1.09
2015,1,1.97
2015,2,4.13
2015,3,0.62
2015,4,0.2
2015,5,0.05
2015,6,0.03
2015,7,0.02
2015,8,0.0
2015,9,0.03
2015,10,0.01
2015,11,0.25
2015,12,0.01
2016,1,2.58
2016,2,0.53
2016,3,0.65
2016,4,0.11
2016,5,0.06
2016,6,0.03
2016,7,0.02
2016,8,0.0
2016,9,0.0
2016,10,0.0
2016,11,0.15
2016,12,0.04
2017,1,6.36
2017,2,1.75
2017,3,0.16
2017,4,0.15
2017,5,0.04
2017,6,0.02
2017,7,0.0
2017,8,0.0
2017,9,0.0
2017,10,0.0
2017,11,0.19
2017,12,2.0
2018,1,1.19
2018,2,1.76
2018,3,1.14
2018,4,0.12
2018,5,0.03
2018,6,0.02
2018,7,0.0
2018,8,0.0
2018,9,0.0
2018,10,0.01
2018,11,0.05
2018,12,0.34
2019,1,3.98
2019,2,3.88
2019,3,0.54
2019,4,0.09
2019,5,0.08
2019,6,0.05
2019,7,0.02
2019,8,0.0
2019,9,0.0
2019,10,0.0
2019,11,0.03
2019,12,0.14
2020,1,0.17
2020,2,0.46
2020,3,0.16
2020,4,0.03
2020,5,0.03
2020,6,0.02
2020,7,0.0
2020,8,0.0
2020,9,0.0
2020,10,0.0
2020,11,0.0
2020,12,0.48
这是一个简单的例子,将 MONTH 列设为字符串,以便为每个月创建跟踪。
fig = px.scatter(
df.assign(MONTH=df["MONTH"].astype(str)),
x='YEAR',
y='SD',
color='MONTH',
trendline_scope='trace', # same as default
trendline='ols',
)
fig.show()
我问的与
代码:
import pandas as pd
import plotly.express as px
# pip install statsmodels
df = pd.read_csv('values.csv')
fig = px.scatter(
df,
x='YEAR',
y='SD',
color='MONTH',
trendline_scope='trace', # same as default
trendline='ols',
)
fig.show()
结果:
我希望像这样的多条趋势线:
数据:
YEAR,MONTH,SD
1980,1,7.3
1980,2,1.75
1980,3,0.9
1980,4,1.09
1980,5,0.11
1980,6,0.03
1980,7,0.03
1980,8,0.01
1980,9,0.0
1980,10,0.01
1980,11,0.91
1980,12,5.67
1981,1,12.82
1981,2,7.54
1981,3,1.82
1981,4,0.21
1981,5,0.05
1981,6,0.02
1981,7,0.02
1981,8,0.01
1981,9,0.0
1981,10,0.09
1981,11,0.57
1981,12,16.23
1982,1,8.43
1982,2,2.75
1982,3,1.92
1982,4,0.54
1982,5,0.1
1982,6,0.02
1982,7,0.01
1982,8,0.0
1982,9,0.0
1982,10,0.0
1982,11,0.09
1982,12,0.66
1983,1,0.94
1983,2,7.67
1983,3,0.78
1983,4,0.22
1983,5,0.04
1983,6,0.02
1983,7,0.01
1983,8,0.0
1983,9,0.0
1983,10,0.0
1983,11,0.13
1983,12,1.5
1984,1,4.33
1984,2,5.88
1984,3,3.81
1984,4,1.12
1984,5,0.04
1984,6,0.02
1984,7,0.01
1984,8,0.0
1984,9,0.0
1984,10,0.01
1984,11,0.06
1984,12,0.36
1985,1,14.18
1985,2,3.82
1985,3,1.82
1985,4,0.2
1985,5,0.08
1985,6,0.01
1985,7,0.01
1985,8,0.0
1985,9,0.0
1985,10,0.0
1985,11,3.34
1985,12,1.11
1986,1,5.09
1986,2,9.18
1986,3,3.14
1986,4,0.69
1986,5,0.03
1986,6,0.02
1986,7,0.0
1986,8,0.0
1986,9,0.0
1986,10,0.0
1986,11,0.0
1986,12,3.02
1987,1,13.14
1987,2,7.3
1987,3,5.3
1987,4,0.46
1987,5,0.06
1987,6,0.03
1987,7,0.01
1987,8,0.0
1987,9,0.0
1987,10,0.0
1987,11,0.16
1987,12,0.18
1988,1,0.45
1988,2,3.77
1988,3,9.79
1988,4,0.79
1988,5,0.04
1988,6,0.03
1988,7,0.02
1988,8,0.0
1988,9,0.0
1988,10,0.0
1988,11,1.08
1988,12,1.38
1989,1,0.42
1989,2,0.47
1989,3,0.27
1989,4,0.06
1989,5,0.02
1989,6,0.02
1989,7,0.01
1989,8,0.0
1989,9,0.0
1989,10,0.0
1989,11,0.4
1989,12,0.36
1990,1,0.43
1990,2,0.74
1990,3,0.34
1990,4,0.06
1990,5,0.03
1990,6,0.02
1990,7,0.01
1990,8,0.0
1990,9,0.0
1990,10,0.0
1990,11,0.28
1990,12,4.02
1991,1,0.53
1991,2,6.64
1991,3,0.11
1991,4,0.12
1991,5,0.04
1991,6,0.02
1991,7,0.0
1991,8,0.0
1991,9,0.0
1991,10,0.01
1991,11,0.21
1991,12,0.8
1992,1,1.02
1992,2,1.65
1992,3,0.87
1992,4,0.29
1992,5,0.04
1992,6,0.02
1992,7,0.01
1992,8,0.0
1992,9,0.0
1992,10,0.04
1992,11,0.13
1992,12,0.4
1993,1,0.73
1993,2,3.35
1993,3,2.28
1993,4,0.08
1993,5,0.03
1993,6,0.02
1993,7,0.01
1993,8,0.0
1993,9,0.0
1993,10,0.01
1993,11,1.32
1993,12,1.7
1994,1,0.67
1994,2,1.9
1994,3,0.31
1994,4,0.24
1994,5,0.03
1994,6,0.02
1994,7,0.0
1994,8,0.0
1994,9,0.0
1994,10,0.0
1994,11,0.01
1994,12,0.36
1995,1,5.46
1995,2,0.55
1995,3,1.34
1995,4,0.28
1995,5,0.04
1995,6,0.03
1995,7,0.01
1995,8,0.0
1995,9,0.0
1995,10,0.0
1995,11,0.79
1995,12,1.41
1996,1,1.23
1996,2,5.43
1996,3,2.51
1996,4,0.38
1996,5,0.02
1996,6,0.01
1996,7,0.01
1996,8,0.0
1996,9,0.0
1996,10,0.0
1996,11,0.95
1996,12,2.98
1997,1,5.4
1997,2,0.83
1997,3,0.22
1997,4,0.14
1997,5,0.04
1997,6,0.03
1997,7,0.02
1997,8,0.01
1997,9,0.0
1997,10,0.01
1997,11,0.01
1997,12,1.08
1998,1,1.18
1998,2,1.22
1998,3,0.42
1998,4,0.1
1998,5,0.02
1998,6,0.02
1998,7,0.01
1998,8,0.0
1998,9,0.0
1998,10,0.01
1998,11,1.35
1998,12,3.78
1999,1,1.63
1999,2,7.77
1999,3,1.35
1999,4,0.3
1999,5,0.05
1999,6,0.04
1999,7,0.03
1999,8,0.01
1999,9,0.0
1999,10,0.0
1999,11,1.47
1999,12,2.35
2000,1,2.83
2000,2,1.28
2000,3,0.74
2000,4,0.22
2000,5,0.03
2000,6,0.02
2000,7,0.01
2000,8,0.01
2000,9,0.0
2000,10,0.0
2000,11,0.01
2000,12,0.91
2001,1,1.12
2001,2,1.89
2001,3,1.09
2001,4,0.23
2001,5,0.02
2001,6,0.02
2001,7,0.01
2001,8,0.0
2001,9,0.01
2001,10,0.0
2001,11,0.31
2001,12,4.5
2002,1,7.62
2002,2,0.69
2002,3,0.29
2002,4,0.05
2002,5,0.03
2002,6,0.02
2002,7,0.01
2002,8,0.0
2002,9,0.0
2002,10,0.0
2002,11,0.05
2002,12,0.25
2003,1,2.51
2003,2,7.27
2003,3,0.65
2003,4,0.12
2003,5,0.02
2003,6,0.01
2003,7,0.0
2003,8,0.0
2003,9,0.0
2003,10,0.04
2003,11,0.02
2003,12,0.65
2004,1,4.32
2004,2,2.52
2004,3,1.79
2004,4,0.1
2004,5,0.03
2004,6,0.03
2004,7,0.01
2004,8,0.0
2004,9,0.0
2004,10,0.0
2004,11,0.54
2004,12,1.12
2005,1,2.6
2005,2,8.91
2005,3,6.79
2005,4,0.09
2005,5,0.03
2005,6,0.02
2005,7,0.01
2005,8,0.0
2005,9,0.0
2005,10,0.0
2005,11,1.38
2005,12,3.78
2006,1,5.01
2006,2,7.22
2006,3,9.61
2006,4,0.52
2006,5,0.03
2006,6,0.03
2006,7,0.0
2006,8,0.0
2006,9,0.0
2006,10,0.0
2006,11,0.05
2006,12,0.1
2007,1,1.06
2007,2,0.52
2007,3,0.54
2007,4,0.03
2007,5,0.01
2007,6,0.01
2007,7,0.0
2007,8,0.0
2007,9,0.0
2007,10,0.03
2007,11,1.25
2007,12,0.69
2008,1,0.6
2008,2,0.31
2008,3,1.2
2008,4,0.2
2008,5,0.04
2008,6,0.02
2008,7,0.0
2008,8,0.0
2008,9,0.0
2008,10,0.03
2008,11,0.98
2008,12,1.65
2009,1,3.26
2009,2,5.76
2009,3,2.66
2009,4,0.38
2009,5,0.05
2009,6,0.03
2009,7,0.02
2009,8,0.0
2009,9,0.02
2009,10,0.07
2009,11,0.01
2009,12,1.71
2010,1,10.45
2010,2,15.67
2010,3,2.54
2010,4,0.07
2010,5,0.04
2010,6,0.03
2010,7,0.01
2010,8,0.0
2010,9,0.0
2010,10,0.03
2010,11,0.89
2010,12,17.19
2011,1,6.01
2011,2,1.12
2011,3,0.16
2011,4,0.02
2011,5,0.02
2011,6,0.01
2011,7,0.0
2011,8,0.0
2011,9,0.0
2011,10,0.0
2011,11,0.0
2011,12,0.94
2012,1,1.87
2012,2,4.2
2012,3,0.73
2012,4,0.12
2012,5,0.05
2012,6,0.03
2012,7,0.01
2012,8,0.0
2012,9,0.0
2012,10,0.2
2012,11,0.12
2012,12,4.55
2013,1,3.61
2013,2,5.52
2013,3,3.83
2013,4,0.6
2013,5,0.04
2013,6,0.04
2013,7,0.02
2013,8,0.0
2013,9,0.0
2013,10,0.01
2013,11,0.14
2013,12,0.48
2014,1,0.92
2014,2,0.45
2014,3,0.1
2014,4,0.03
2014,5,0.03
2014,6,0.02
2014,7,0.01
2014,8,0.0
2014,9,0.0
2014,10,0.01
2014,11,0.01
2014,12,1.09
2015,1,1.97
2015,2,4.13
2015,3,0.62
2015,4,0.2
2015,5,0.05
2015,6,0.03
2015,7,0.02
2015,8,0.0
2015,9,0.03
2015,10,0.01
2015,11,0.25
2015,12,0.01
2016,1,2.58
2016,2,0.53
2016,3,0.65
2016,4,0.11
2016,5,0.06
2016,6,0.03
2016,7,0.02
2016,8,0.0
2016,9,0.0
2016,10,0.0
2016,11,0.15
2016,12,0.04
2017,1,6.36
2017,2,1.75
2017,3,0.16
2017,4,0.15
2017,5,0.04
2017,6,0.02
2017,7,0.0
2017,8,0.0
2017,9,0.0
2017,10,0.0
2017,11,0.19
2017,12,2.0
2018,1,1.19
2018,2,1.76
2018,3,1.14
2018,4,0.12
2018,5,0.03
2018,6,0.02
2018,7,0.0
2018,8,0.0
2018,9,0.0
2018,10,0.01
2018,11,0.05
2018,12,0.34
2019,1,3.98
2019,2,3.88
2019,3,0.54
2019,4,0.09
2019,5,0.08
2019,6,0.05
2019,7,0.02
2019,8,0.0
2019,9,0.0
2019,10,0.0
2019,11,0.03
2019,12,0.14
2020,1,0.17
2020,2,0.46
2020,3,0.16
2020,4,0.03
2020,5,0.03
2020,6,0.02
2020,7,0.0
2020,8,0.0
2020,9,0.0
2020,10,0.0
2020,11,0.0
2020,12,0.48
这是一个简单的例子,将 MONTH 列设为字符串,以便为每个月创建跟踪。
fig = px.scatter(
df.assign(MONTH=df["MONTH"].astype(str)),
x='YEAR',
y='SD',
color='MONTH',
trendline_scope='trace', # same as default
trendline='ols',
)
fig.show()