Plotly: How to add trendline and parallel lines to time series data?

丶灬走出姿态 提交于 2021-01-28 21:54:11

问题


My goal is to add 5 fit lines to the exchange index, which is a time series data.

Below is what I want to achieve:

There should be a plotted (&log2 transformed) index, a best fit line (in yellow), and 4 other parallel lines where it covers 95%, 75%, 25% and 5% of the index respectively. The x-axis is omitted in the picture, but it should be dates. So my question is how to add these 5 lines using Plotly?

With my current code, I was able to plot the index without the 5 parallel lines but I could not pass the 'trendline' argument to it.

The code I am using is like this:

figure = {'data': [{'x': log_hsi['Date'], 'y': log_hsi['Adj Close']}],
      'layout': {'xaxis': {'autorange': True},
                 'yaxis': {'range': [8, 11], 'autorange': False},
                 'title': 'Log transformed HSI'}}
iplot(figure)

The dataframe I am using is like this: (there are too many entries so I deleted some of it)

{'Date': {3654: Timestamp('2001-01-02 00:00:00'),
  3655: Timestamp('2001-01-03 00:00:00'),
  3656: Timestamp('2001-01-04 00:00:00'),
  3657: Timestamp('2001-01-05 00:00:00'),
  3658: Timestamp('2001-01-08 00:00:00'),
  3659: Timestamp('2001-01-09 00:00:00'),
  3660: Timestamp('2001-01-10 00:00:00'),
  3661: Timestamp('2001-01-11 00:00:00'),
  3662: Timestamp('2001-01-12 00:00:00'),
  3663: Timestamp('2001-01-15 00:00:00'),
  3664: Timestamp('2001-01-16 00:00:00'),
  3665: Timestamp('2001-01-17 00:00:00'),
  3666: Timestamp('2001-01-18 00:00:00'),
  3667: Timestamp('2001-01-19 00:00:00'),
  3668: Timestamp('2001-01-22 00:00:00'),
  3669: Timestamp('2001-01-23 00:00:00'),
  3673: Timestamp('2001-01-29 00:00:00'),
  3674: Timestamp('2001-01-30 00:00:00'),
  3675: Timestamp('2001-01-31 00:00:00'),
  3676: Timestamp('2001-02-01 00:00:00'),
  3677: Timestamp('2001-02-02 00:00:00'),
  3678: Timestamp('2001-02-05 00:00:00'),
  3679: Timestamp('2001-02-06 00:00:00'),
  3680: Timestamp('2001-02-07 00:00:00'),
  3681: Timestamp('2001-02-08 00:00:00'),
  3682: Timestamp('2001-02-09 00:00:00'),
  3683: Timestamp('2001-02-12 00:00:00'),
  3684: Timestamp('2001-02-13 00:00:00'),
  3685: Timestamp('2001-02-14 00:00:00'),
  3686: Timestamp('2001-02-15 00:00:00'),
  3687: Timestamp('2001-02-16 00:00:00'),
  3688: Timestamp('2001-02-19 00:00:00'),
  3689: Timestamp('2001-02-20 00:00:00'),
  3690: Timestamp('2001-02-21 00:00:00'),
  3691: Timestamp('2001-02-22 00:00:00'),
  3692: Timestamp('2001-02-23 00:00:00'),
  3693: Timestamp('2001-02-26 00:00:00'),
  3694: Timestamp('2001-02-27 00:00:00'),
  3695: Timestamp('2001-02-28 00:00:00'),
  3696: Timestamp('2001-03-01 00:00:00'),
  3697: Timestamp('2001-03-02 00:00:00'),
  3698: Timestamp('2001-03-05 00:00:00'),
  3699: Timestamp('2001-03-06 00:00:00'),
  3700: Timestamp('2001-03-07 00:00:00'),
  3701: Timestamp('2001-03-08 00:00:00'),
  3702: Timestamp('2001-03-09 00:00:00'),
  3703: Timestamp('2001-03-12 00:00:00'),
  3704: Timestamp('2001-03-13 00:00:00'),
  3705: Timestamp('2001-03-14 00:00:00'),
  3706: Timestamp('2001-03-15 00:00:00'),
  3707: Timestamp('2001-03-16 00:00:00'),
  3708: Timestamp('2001-03-19 00:00:00'),
  3709: Timestamp('2001-03-20 00:00:00'),
  3710: Timestamp('2001-03-21 00:00:00'),
  3711: Timestamp('2001-03-22 00:00:00'),
  3712: Timestamp('2001-03-23 00:00:00'),
  3713: Timestamp('2001-03-26 00:00:00'),
  3714: Timestamp('2001-03-27 00:00:00'),
  3715: Timestamp('2001-03-28 00:00:00'),
  3716: Timestamp('2001-03-29 00:00:00'),
  3717: Timestamp('2001-03-30 00:00:00'),
  3718: Timestamp('2001-04-02 00:00:00'),
  3719: Timestamp('2001-04-03 00:00:00'),
  3720: Timestamp('2001-04-04 00:00:00'),
  3722: Timestamp('2001-04-06 00:00:00'),
  3723: Timestamp('2001-04-09 00:00:00'),
  3724: Timestamp('2001-04-10 00:00:00'),
  3725: Timestamp('2001-04-11 00:00:00'),
  3726: Timestamp('2001-04-12 00:00:00'),
  3729: Timestamp('2001-04-17 00:00:00'),
  3730: Timestamp('2001-04-18 00:00:00'),
  3731: Timestamp('2001-04-19 00:00:00'),
  3732: Timestamp('2001-04-20 00:00:00'),
  3733: Timestamp('2001-04-23 00:00:00'),
  3734: Timestamp('2001-04-24 00:00:00'),
  3735: Timestamp('2001-04-25 00:00:00'),
  3736: Timestamp('2001-04-26 00:00:00'),
  3737: Timestamp('2001-04-27 00:00:00'),
  3740: Timestamp('2001-05-02 00:00:00'),
  3741: Timestamp('2001-05-03 00:00:00'),
  3742: Timestamp('2001-05-04 00:00:00'),
  3743: Timestamp('2001-05-07 00:00:00'),
  3744: Timestamp('2001-05-08 00:00:00'),
  3745: Timestamp('2001-05-09 00:00:00'),
  3746: Timestamp('2001-05-10 00:00:00'),
  3747: Timestamp('2001-05-11 00:00:00'),
  3748: Timestamp('2001-05-14 00:00:00'),
  3749: Timestamp('2001-05-15 00:00:00'),
  3750: Timestamp('2001-05-16 00:00:00'),
  3751: Timestamp('2001-05-17 00:00:00'),
  3752: Timestamp('2001-05-18 00:00:00'),
  3753: Timestamp('2001-05-21 00:00:00'),
  3754: Timestamp('2001-05-22 00:00:00'),
  3755: Timestamp('2001-05-23 00:00:00'),
  3756: Timestamp('2001-05-24 00:00:00'),
  3757: Timestamp('2001-05-25 00:00:00'),
  3758: Timestamp('2001-05-28 00:00:00'),
  3759: Timestamp('2001-05-29 00:00:00'),
  3760: Timestamp('2001-05-30 00:00:00'),
  3761: Timestamp('2001-05-31 00:00:00'),
  3762: Timestamp('2001-06-01 00:00:00'),
  3763: Timestamp('2001-06-04 00:00:00'),
  3764: Timestamp('2001-06-05 00:00:00'),
  3765: Timestamp('2001-06-06 00:00:00'),
  3766: Timestamp('2001-06-07 00:00:00'),
  3767: Timestamp('2001-06-08 00:00:00'),
  3768: Timestamp('2001-06-11 00:00:00'),
  3769: Timestamp('2001-06-12 00:00:00'),
  3770: Timestamp('2001-06-13 00:00:00'),
  3771: Timestamp('2001-06-14 00:00:00'),
  3772: Timestamp('2001-06-15 00:00:00'),
  3773: Timestamp('2001-06-18 00:00:00'),
  3774: Timestamp('2001-06-19 00:00:00'),
  3775: Timestamp('2001-06-20 00:00:00'),
  3776: Timestamp('2001-06-21 00:00:00'),
  3777: Timestamp('2001-06-22 00:00:00'),
  3779: Timestamp('2001-06-26 00:00:00'),
  3780: Timestamp('2001-06-27 00:00:00'),
  3781: Timestamp('2001-06-28 00:00:00'),
  3782: Timestamp('2001-06-29 00:00:00'),
  3784: Timestamp('2001-07-03 00:00:00'),
  3785: Timestamp('2001-07-04 00:00:00'),
  3786: Timestamp('2001-07-05 00:00:00'),
  3788: Timestamp('2001-07-09 00:00:00'),
  3789: Timestamp('2001-07-10 00:00:00'),
  3790: Timestamp('2001-07-11 00:00:00'),
  3791: Timestamp('2001-07-12 00:00:00'),
  3792: Timestamp('2001-07-13 00:00:00'),
  3793: Timestamp('2001-07-16 00:00:00'),
  3794: Timestamp('2001-07-17 00:00:00'),
  3795: Timestamp('2001-07-18 00:00:00'),
  3796: Timestamp('2001-07-19 00:00:00'),
  3797: Timestamp('2001-07-20 00:00:00'),
  3798: Timestamp('2001-07-23 00:00:00'),
  3799: Timestamp('2001-07-24 00:00:00'),
  3801: Timestamp('2001-07-26 00:00:00'),
  3802: Timestamp('2001-07-27 00:00:00'),
  3803: Timestamp('2001-07-30 00:00:00'),
  3804: Timestamp('2001-07-31 00:00:00'),
  3805: Timestamp('2001-08-01 00:00:00'),
  3806: Timestamp('2001-08-02 00:00:00'),
  3807: Timestamp('2001-08-03 00:00:00'),
  3808: Timestamp('2001-08-06 00:00:00'),
  3809: Timestamp('2001-08-07 00:00:00'),
  3810: Timestamp('2001-08-08 00:00:00'),
  3811: Timestamp('2001-08-09 00:00:00'),
  3812: Timestamp('2001-08-10 00:00:00'),
  3813: Timestamp('2001-08-13 00:00:00'),
  3814: Timestamp('2001-08-14 00:00:00'),
  3815: Timestamp('2001-08-15 00:00:00'),
  3816: Timestamp('2001-08-16 00:00:00'),
  3817: Timestamp('2001-08-17 00:00:00'),
  3818: Timestamp('2001-08-20 00:00:00'),
  3819: Timestamp('2001-08-21 00:00:00'),
  3820: Timestamp('2001-08-22 00:00:00'),
  3821: Timestamp('2001-08-23 00:00:00'),
  3822: Timestamp('2001-08-24 00:00:00'),
  3823: Timestamp('2001-08-27 00:00:00'),
  3824: Timestamp('2001-08-28 00:00:00'),
  3825: Timestamp('2001-08-29 00:00:00'),
  3826: Timestamp('2001-08-30 00:00:00'),
  3827: Timestamp('2001-08-31 00:00:00'),
  3828: Timestamp('2001-09-03 00:00:00'),
  3829: Timestamp('2001-09-04 00:00:00'),
  3830: Timestamp('2001-09-05 00:00:00'),
  3831: Timestamp('2001-09-06 00:00:00'),
  3832: Timestamp('2001-09-07 00:00:00'),
  3833: Timestamp('2001-09-10 00:00:00'),
  3834: Timestamp('2001-09-11 00:00:00'),
  3835: Timestamp('2001-09-12 00:00:00'),
  3836: Timestamp('2001-09-13 00:00:00'),
  3837: Timestamp('2001-09-14 00:00:00'),
  3838: Timestamp('2001-09-17 00:00:00'),
  3839: Timestamp('2001-09-18 00:00:00'),
  3840: Timestamp('2001-09-19 00:00:00'),
  3841: Timestamp('2001-09-20 00:00:00'),
  3842: Timestamp('2001-09-21 00:00:00'),
  3843: Timestamp('2001-09-24 00:00:00'),
  3844: Timestamp('2001-09-25 00:00:00'),
  3845: Timestamp('2001-09-26 00:00:00'),
  3846: Timestamp('2001-09-27 00:00:00'),
  3847: Timestamp('2001-09-28 00:00:00'),
  3850: Timestamp('2001-10-03 00:00:00'),
  3851: Timestamp('2001-10-04 00:00:00'),
  3852: Timestamp('2001-10-05 00:00:00'),
  3853: Timestamp('2001-10-08 00:00:00'),
  3854: Timestamp('2001-10-09 00:00:00'),
  3855: Timestamp('2001-10-10 00:00:00'),
  3856: Timestamp('2001-10-11 00:00:00'),
  3857: Timestamp('2001-10-12 00:00:00'),
  3858: Timestamp('2001-10-15 00:00:00'),
  3859: Timestamp('2001-10-16 00:00:00'),
  3860: Timestamp('2001-10-17 00:00:00'),
  3861: Timestamp('2001-10-18 00:00:00'),
  3862: Timestamp('2001-10-19 00:00:00'),
  3863: Timestamp('2001-10-22 00:00:00'),
  3864: Timestamp('2001-10-23 00:00:00'),
  3865: Timestamp('2001-10-24 00:00:00'),
  3866: Timestamp('2001-10-25 00:00:00'),
  3867: Timestamp('2001-10-26 00:00:00')},
 'Adj Close': {3654: 9.607097033393721,
  3655: 9.588062859608462,
  3656: 9.631352678487083,
  3657: 9.645209600589826,
  3658: 9.644492075073419,
  3659: 9.648633356634898,
  3660: 9.644444120530794,
  3661: 9.621838546551764,
  3662: 9.635308711062654,
  3663: 9.635200165752282,
  3664: 9.639727089251528,
  3665: 9.633087316449839,
  3666: 9.650448423527887,
  3667: 9.676182215807014,
  3668: 9.68652917919793,
  3669: 9.683103313378735,
  3673: 9.686563390227043,
  3674: 9.67363846383603,
  3675: 9.68672047877178,
  3676: 9.690541222016048,
  3677: 9.684789731738592,
  3678: 9.669715205415946,
  3679: 9.674906760809609,
  3680: 9.68343108919617,
  3681: 9.674665433066641,
  3682: 9.672392488633012,
  3683: 9.66097706393906,
  3684: 9.670465350566538,
  3685: 9.671581971635309,
  3686: 9.665000014358183,
  3687: 9.656967229361419,
  3688: 9.648011904440962,
  3689: 9.650358930801001,
  3690: 9.638969104269906,
  3691: 9.622359929208285,
  3692: 9.634336683123275,
  3693: 9.631036873033986,
  3694: 9.604726363972071,
  3695: 9.601562537018221,
  3696: 9.572240809113206,
  3697: 9.54441184965187,
  3698: 9.55642764746148,
  3699: 9.569485748232589,
  3700: 9.559401629866123,
  3701: 9.561627340459154,
  3702: 9.560599249541072,
  3703: 9.53073547029408,
  3704: 9.509928555036067,
  3705: 9.497835415239095,
  3706: 9.510753799843064,
  3707: 9.512076228672715,
  3708: 9.50730600080871,
  3709: 9.489778079251513,
  3710: 9.484514655891614,
  3711: 9.4431846870835,
  3712: 9.440130613185564,
  3713: 9.468888922318502,
  3714: 9.449979157067848,
  3715: 9.461208824068935,
  3716: 9.44761478259421,
  3717: 9.454120684795962,
  3718: 9.451504556326523,
  3719: 9.440198905408943,
  3720: 9.397957048264434,
  3722: 9.424371357855359,
  3723: 9.409363315012358,
  3724: 9.410311088855932,
  3725: 9.449863418978582,
  3726: 9.471894287199396,
  3729: 9.441963882191201,
  3730: 9.470610121786176,
  3731: 9.514064346922632,
  3732: 9.506595333206596,
  3733: 9.496383602259439,
  3734: 9.493608493664295,
  3735: 9.491718853856005,
  3736: 9.495001161345217,
  3737: 9.50196765476623,
  3740: 9.533455240115916,
  3741: 9.52647429791706,
  3742: 9.502337389199505,
  3743: 9.51788244309308,
  3744: 9.513463335668517,
  3745: 9.516731801380672,
  3746: 9.518177936298352,
  3747: 9.520513392596355,
  3748: 9.492444661629552,
  3749: 9.491759612070625,
  3750: 9.498218689795582,
  3751: 9.520605769424748,
  3752: 9.507416656913596,
  3753: 9.526702428413103,
  3754: 9.538056542679456,
  3755: 9.535253169902173,
  3756: 9.533191663925663,
  3757: 9.529084259831544,
  3758: 9.527998900677794,
  3759: 9.519999936754434,
  3760: 9.504511088794784,
  3761: 9.486031602464285,
  3762: 9.483521300451965,
  3763: 9.488539389609842,
  3764: 9.506873417520655,
  3765: 9.516059526271494,
  3766: 9.52540142267562,
  3767: 9.533067841143405,
  3768: 9.523360475569014,
  3769: 9.512419287352929,
  3770: 9.512170110321078,
  3771: 9.491669027751996,
  3772: 9.480558330676322,
  3773: 9.468756875278643,
  3774: 9.48293369128291,
  3775: 9.466431924131614,
  3776: 9.487020913528825,
  3777: 9.486001951740908,
  3779: 9.469774943465724,
  3780: 9.473028427171643,
  3781: 9.459371553309266,
  3782: 9.475970855997938,
  3784: 9.486816137667164,
  3785: 9.488542421142602,
  3786: 9.472664671722018,
  3788: 9.448623120188204,
  3789: 9.450451192873874,
  3790: 9.435713467289014,
  3791: 9.446218508764293,
  3792: 9.442466660552066,
  3793: 9.443397047352386,
  3794: 9.433103851072097,
  3795: 9.427642127580112,
  3796: 9.41571256910222,
  3797: 9.417491092037041,
  3798: 9.412174497254961,
  3799: 9.4103462690634,
  3801: 9.39597479458201,
  3802: 9.407728679911855,
  3803: 9.399857656975392,
  3804: 9.418710567070383,
  3805: 9.431781694039891,
  3806: 9.430789907045172,
  3807: 9.414837561626188,
  3808: 9.404986466190781,
  3809: 9.39326095182,
  3810: 9.389156606132271,
  3811: 9.368776387849374,
  3812: 9.372953110523751,
  3813: 9.366855970805329,
  3814: 9.391912461823267,
  3815: 9.404395312850555,
  3816: 9.378600227328686,
  3817: 9.37201776092802,
  3818: 9.34650456280641,
  3819: 9.344901824694107,
  3820: 9.32264802844274,
  3821: 9.33656588127212,
  3822: 9.315627867418097,
  3823: 9.326764237890817,
  3824: 9.332604930413563,
  3825: 9.327448527151956,
  3826: 9.333940224481115,
  3827: 9.313842403932533,
  3828: 9.29676020844021,
  3829: 9.318015638210596,
  3830: 9.300468022736998,
  3831: 9.27465889826041,
  3832: 9.248040717937537,
  3833: 9.246317398619535,
  3834: 9.25122895807117,
  3835: 9.158375285355174,
  3836: 9.166305927329747,
  3837: 9.175277821947487,
  3838: 9.13984812080253,
  3839: 9.1386188229253,
  3840: 9.165149513582218,
  3841: 9.139701196323891,
  3842: 9.097641909876808,
  3843: 9.13610162204065,
  3844: 9.128051597198034,
  3845: 9.145455124069166,
  3846: 9.169600669798987,
  3847: 9.205398199033475,
  3850: 9.200001069931528,
  3851: 9.238576907009563,
  3852: 9.237700631328401,
  3853: 9.207118194132338,
  3854: 9.245604198507314,
  3855: 9.23972830855306,
  3856: 9.26128158783136,
  3857: 9.237384352858927,
  3858: 9.223314822990815,
  3859: 9.225080227987517,
  3860: 9.236087021069979,
  3861: 9.198329565352042,
  3862: 9.192770913389573,
  3863: 9.189886616720194,
  3864: 9.23208619279342,
  3865: 9.23439472833901,
  3866: 9.23439472833901,
  3867: 9.250016773018734},
 'Volume': {3654: 0.0,
  3655: 0.0,
  3656: 0.0,
  3657: 0.0,
  3658: 0.0,
  3659: 0.0,
  3660: 0.0,
  3661: 0.0,
  3662: 0.0,
  3663: 0.0,
  3664: 0.0,
  3665: 0.0,
  3666: 0.0,
  3667: 0.0,
  3668: 0.0,
  3669: 0.0,
  3673: 0.0,
  3674: 0.0,
  3675: 0.0,
  3676: 0.0,
  3677: 0.0,
  3678: 0.0,
  3679: 0.0,
  3680: 0.0,
  3681: 0.0,
  3682: 0.0,
  3683: 0.0,
  3684: 0.0,
  3685: 0.0,
  3686: 0.0,
  3687: 0.0,
  3688: 0.0,
  3689: 0.0,
  3690: 0.0,
  3691: 0.0,
  3692: 0.0,
  3693: 0.0,
  3694: 0.0,
  3695: 0.0,
  3696: 0.0,
  3697: 0.0,
  3698: 0.0,
  3699: 0.0,
  3700: 0.0,
  3701: 0.0,
  3702: 0.0,
  3703: 0.0,
  3704: 0.0,
  3705: 0.0,
  3706: 0.0,
  3707: 0.0,
  3708: 0.0,
  3709: 0.0,
  3710: 0.0,
  3711: 0.0,
  3712: 0.0,
  3713: 0.0,
  3714: 0.0,
  3715: 0.0,
  3716: 0.0,
  3717: 0.0,
  3718: 0.0,
  3719: 0.0,
  3720: 0.0,
  3722: 0.0,
  3723: 0.0,
  3724: 0.0,
  3725: 0.0,
  3726: 0.0,
  3729: 0.0,
  3730: 0.0,
  3731: 0.0,
  3732: 0.0,
  3733: 0.0,
  3734: 0.0,
  3735: 0.0,
  3736: 0.0,
  3737: 0.0,
  3740: 0.0,
  3741: 0.0,
  3742: 0.0,
  3743: 0.0,
  3744: 0.0,
  3745: 0.0,
  3746: 0.0,
  3747: 0.0,
  3748: 0.0,
  3749: 0.0,
  3750: 0.0,
  3751: 0.0,
  3752: 0.0,
  3753: 0.0,
  3754: 0.0,
  3755: 0.0,
  3756: 0.0,
  3757: 0.0,
  3758: 0.0,
  3759: 0.0,
  3760: 0.0,
  3761: 0.0,
  3762: 0.0,
  3763: 0.0,
  3764: 0.0,
  3765: 0.0,
  3766: 0.0,
  3767: 0.0,
  3768: 0.0,
  3769: 0.0,
  3770: 0.0,
  3771: 0.0,
  3772: 0.0,
  3773: 0.0,
  3774: 0.0,
  3775: 0.0,
  3776: 0.0,
  3777: 0.0,
  3779: 0.0,
  3780: 0.0,
  3781: 0.0,
  3782: 0.0,
  3784: 0.0,
  3785: 0.0,
  3786: 0.0,
  3788: 257038800.0,
  3789: 134407800.0,
  3790: 195057600.0,
  3791: 174767800.0,
  3792: 211230200.0,
  3793: 113928800.0,
  3794: 139890800.0,
  3795: 134535000.0,
  3796: 204987000.0,
  3797: 147662000.0,
  3798: 166057200.0,
  3799: 139913800.0,
  3801: 221039000.0,
  3802: 124388600.0,
  3803: 153086200.0,
  3804: 227109800.0,
  3805: 243126000.0,
  3806: 194471600.0,
  3807: 168728800.0,
  3808: 141753200.0,
  3809: 208445200.0,
  3810: 178200800.0,
  3811: 231948800.0,
  3812: 148634200.0,
  3813: 137231600.0,
  3814: 172713800.0,
  3815: 191067400.0,
  3816: 422805600.0,
  3817: 330698600.0,
  3818: 256960200.0,
  3819: 225189800.0,
  3820: 272482800.0,
  3821: 215469200.0,
  3822: 241046000.0,
  3823: 145020400.0,
  3824: 179275400.0,
  3825: 188285800.0,
  3826: 246490800.0,
  3827: 265702000.0,
  3828: 185143200.0,
  3829: 303746000.0,
  3830: 206642600.0,
  3831: 239079600.0,
  3832: 399700800.0,
  3833: 367156400.0,
  3834: 0.0,
  3835: 0.0,
  3836: 0.0,
  3837: 0.0,
  3838: 0.0,
  3839: 0.0,
  3840: 333256200.0,
  3841: 284966400.0,
  3842: 519940400.0,
  3843: 442181500.0,
  3844: 367545800.0,
  3845: 390860600.0,
  3846: 296667600.0,
  3847: 320775600.0,
  3850: 333197400.0,
  3851: 358779000.0,
  3852: 576213400.0,
  3853: 511535600.0,
  3854: 409534200.0,
  3855: 370696800.0,
  3856: 398527200.0,
  3857: 388528000.0,
  3858: 275161200.0,
  3859: 192816600.0,
  3860: 414838800.0,
  3861: 365696000.0,
  3862: 297211400.0,
  3863: 236566000.0,
  3864: 344018800.0,
  3865: 287418600.0,
  3866: 0.0,
  3867: 346798600.0}}

A big thanks to all of you who took time to help.


回答1:


This answer focuses directly on how to add those lines (dynamically) to a plotly figure. How you calculate those lines is another matter. If the lines are in fact straight parallell lines, then the only tricky part you need to calculate are the starting points, or constants, of the line. The slope of the lines should be equal to the slope of the linear trend of your Adj Close.

I've put togehter a suggestion that builds a given number of lines by calculating some necessary parameters from your source data using statsmodels.

mod = sm.OLS(df['Adj Close'],sm.add_constant(df.ix)).fit()
const = mod.params[0]
trend = mod.params[1]

And then I've specified a list of adjustments to the starting points (model constant) like this:

extra_lines = [-0.2,-0.1,0, 0.1,0.2] # add or  remove as you please

The reason why df.ix is included as a continuous index because your original index seemed to have some jumps in it.

I then proceed to add adjusted trendlines to a fig using:

for i, m in enumerate(model):
    df[m['Line']]=[(m['const']) + (trend*i) for i,v in enumerate(df.index)]
    fig.add_traces(go.Scatter(x=df.ix, y=df[m['Line']]))

Here's the result based on your sample data:

Complete code:

import plotly.graph_objects as go
import statsmodels.api as sm
import pandas as pd
import numpy as np
import datetime
from pandas import Timestamp

df = pd.DataFrame({'Date': {3762: Timestamp('2001-06-01 00:00:00'),
  3763: Timestamp('2001-06-04 00:00:00'),
  3764: Timestamp('2001-06-05 00:00:00'),
  3765: Timestamp('2001-06-06 00:00:00'),
  3766: Timestamp('2001-06-07 00:00:00'),
  3767: Timestamp('2001-06-08 00:00:00'),
  3768: Timestamp('2001-06-11 00:00:00'),
  3769: Timestamp('2001-06-12 00:00:00'),
  3770: Timestamp('2001-06-13 00:00:00'),
  3771: Timestamp('2001-06-14 00:00:00'),
  3772: Timestamp('2001-06-15 00:00:00'),
  3773: Timestamp('2001-06-18 00:00:00'),
  3774: Timestamp('2001-06-19 00:00:00'),
  3775: Timestamp('2001-06-20 00:00:00'),
  3776: Timestamp('2001-06-21 00:00:00'),
  3777: Timestamp('2001-06-22 00:00:00'),
  3779: Timestamp('2001-06-26 00:00:00'),
  3780: Timestamp('2001-06-27 00:00:00'),
  3781: Timestamp('2001-06-28 00:00:00'),
  3782: Timestamp('2001-06-29 00:00:00'),
  3784: Timestamp('2001-07-03 00:00:00'),
  3785: Timestamp('2001-07-04 00:00:00'),
  3786: Timestamp('2001-07-05 00:00:00'),
  3788: Timestamp('2001-07-09 00:00:00'),
  3789: Timestamp('2001-07-10 00:00:00'),
  3790: Timestamp('2001-07-11 00:00:00'),
  3791: Timestamp('2001-07-12 00:00:00'),
  3792: Timestamp('2001-07-13 00:00:00'),
  3793: Timestamp('2001-07-16 00:00:00'),
  3794: Timestamp('2001-07-17 00:00:00'),
  3795: Timestamp('2001-07-18 00:00:00'),
  3796: Timestamp('2001-07-19 00:00:00'),
  3797: Timestamp('2001-07-20 00:00:00'),
  3798: Timestamp('2001-07-23 00:00:00'),
  3799: Timestamp('2001-07-24 00:00:00'),
  3801: Timestamp('2001-07-26 00:00:00'),
  3802: Timestamp('2001-07-27 00:00:00'),
  3803: Timestamp('2001-07-30 00:00:00'),
  3804: Timestamp('2001-07-31 00:00:00'),
  3805: Timestamp('2001-08-01 00:00:00'),
  3806: Timestamp('2001-08-02 00:00:00'),
  3807: Timestamp('2001-08-03 00:00:00'),
  3808: Timestamp('2001-08-06 00:00:00'),
  3809: Timestamp('2001-08-07 00:00:00'),
  3810: Timestamp('2001-08-08 00:00:00'),
  3811: Timestamp('2001-08-09 00:00:00'),
  3812: Timestamp('2001-08-10 00:00:00'),
  3813: Timestamp('2001-08-13 00:00:00'),
  3814: Timestamp('2001-08-14 00:00:00'),
  3815: Timestamp('2001-08-15 00:00:00'),
  3816: Timestamp('2001-08-16 00:00:00'),
  3817: Timestamp('2001-08-17 00:00:00'),
  3818: Timestamp('2001-08-20 00:00:00'),
  3819: Timestamp('2001-08-21 00:00:00'),
  3820: Timestamp('2001-08-22 00:00:00'),
  3821: Timestamp('2001-08-23 00:00:00'),
  3822: Timestamp('2001-08-24 00:00:00'),
  3823: Timestamp('2001-08-27 00:00:00'),
  3824: Timestamp('2001-08-28 00:00:00'),
  3825: Timestamp('2001-08-29 00:00:00'),
  3826: Timestamp('2001-08-30 00:00:00'),
  3827: Timestamp('2001-08-31 00:00:00'),
  3828: Timestamp('2001-09-03 00:00:00'),
  3829: Timestamp('2001-09-04 00:00:00'),
  3830: Timestamp('2001-09-05 00:00:00'),
  3831: Timestamp('2001-09-06 00:00:00'),
  3832: Timestamp('2001-09-07 00:00:00'),
  3833: Timestamp('2001-09-10 00:00:00'),
  3834: Timestamp('2001-09-11 00:00:00'),
  3835: Timestamp('2001-09-12 00:00:00'),
  3836: Timestamp('2001-09-13 00:00:00'),
  3837: Timestamp('2001-09-14 00:00:00'),
  3838: Timestamp('2001-09-17 00:00:00'),
  3839: Timestamp('2001-09-18 00:00:00'),
  3840: Timestamp('2001-09-19 00:00:00'),
  3841: Timestamp('2001-09-20 00:00:00'),
  3842: Timestamp('2001-09-21 00:00:00'),
  3843: Timestamp('2001-09-24 00:00:00'),
  3844: Timestamp('2001-09-25 00:00:00'),
  3845: Timestamp('2001-09-26 00:00:00'),
  3846: Timestamp('2001-09-27 00:00:00'),
  3847: Timestamp('2001-09-28 00:00:00'),
  3850: Timestamp('2001-10-03 00:00:00'),
  3851: Timestamp('2001-10-04 00:00:00'),
  3852: Timestamp('2001-10-05 00:00:00'),
  3853: Timestamp('2001-10-08 00:00:00'),
  3854: Timestamp('2001-10-09 00:00:00'),
  3855: Timestamp('2001-10-10 00:00:00'),
  3856: Timestamp('2001-10-11 00:00:00'),
  3857: Timestamp('2001-10-12 00:00:00'),
  3858: Timestamp('2001-10-15 00:00:00'),
  3859: Timestamp('2001-10-16 00:00:00'),
  3860: Timestamp('2001-10-17 00:00:00'),
  3861: Timestamp('2001-10-18 00:00:00'),
  3862: Timestamp('2001-10-19 00:00:00'),
  3863: Timestamp('2001-10-22 00:00:00'),
  3864: Timestamp('2001-10-23 00:00:00'),
  3865: Timestamp('2001-10-24 00:00:00'),
  3866: Timestamp('2001-10-25 00:00:00'),
  3867: Timestamp('2001-10-26 00:00:00')},
 'Adj Close': {3762: 9.483521300451965,
  3763: 9.488539389609842,
  3764: 9.506873417520655,
  3765: 9.516059526271494,
  3766: 9.52540142267562,
  3767: 9.533067841143405,
  3768: 9.523360475569014,
  3769: 9.512419287352929,
  3770: 9.512170110321078,
  3771: 9.491669027751996,
  3772: 9.480558330676322,
  3773: 9.468756875278643,
  3774: 9.48293369128291,
  3775: 9.466431924131614,
  3776: 9.487020913528825,
  3777: 9.486001951740908,
  3779: 9.469774943465724,
  3780: 9.473028427171643,
  3781: 9.459371553309266,
  3782: 9.475970855997938,
  3784: 9.486816137667164,
  3785: 9.488542421142602,
  3786: 9.472664671722018,
  3788: 9.448623120188204,
  3789: 9.450451192873874,
  3790: 9.435713467289014,
  3791: 9.446218508764293,
  3792: 9.442466660552066,
  3793: 9.443397047352386,
  3794: 9.433103851072097,
  3795: 9.427642127580112,
  3796: 9.41571256910222,
  3797: 9.417491092037041,
  3798: 9.412174497254961,
  3799: 9.4103462690634,
  3801: 9.39597479458201,
  3802: 9.407728679911855,
  3803: 9.399857656975392,
  3804: 9.418710567070383,
  3805: 9.431781694039891,
  3806: 9.430789907045172,
  3807: 9.414837561626188,
  3808: 9.404986466190781,
  3809: 9.39326095182,
  3810: 9.389156606132271,
  3811: 9.368776387849374,
  3812: 9.372953110523751,
  3813: 9.366855970805329,
  3814: 9.391912461823267,
  3815: 9.404395312850555,
  3816: 9.378600227328686,
  3817: 9.37201776092802,
  3818: 9.34650456280641,
  3819: 9.344901824694107,
  3820: 9.32264802844274,
  3821: 9.33656588127212,
  3822: 9.315627867418097,
  3823: 9.326764237890817,
  3824: 9.332604930413563,
  3825: 9.327448527151956,
  3826: 9.333940224481115,
  3827: 9.313842403932533,
  3828: 9.29676020844021,
  3829: 9.318015638210596,
  3830: 9.300468022736998,
  3831: 9.27465889826041,
  3832: 9.248040717937537,
  3833: 9.246317398619535,
  3834: 9.25122895807117,
  3835: 9.158375285355174,
  3836: 9.166305927329747,
  3837: 9.175277821947487,
  3838: 9.13984812080253,
  3839: 9.1386188229253,
  3840: 9.165149513582218,
  3841: 9.139701196323891,
  3842: 9.097641909876808,
  3843: 9.13610162204065,
  3844: 9.128051597198034,
  3845: 9.145455124069166,
  3846: 9.169600669798987,
  3847: 9.205398199033475,
  3850: 9.200001069931528,
  3851: 9.238576907009563,
  3852: 9.237700631328401,
  3853: 9.207118194132338,
  3854: 9.245604198507314,
  3855: 9.23972830855306,
  3856: 9.26128158783136,
  3857: 9.237384352858927,
  3858: 9.223314822990815,
  3859: 9.225080227987517,
  3860: 9.236087021069979,
  3861: 9.198329565352042,
  3862: 9.192770913389573,
  3863: 9.189886616720194,
  3864: 9.23208619279342,
  3865: 9.23439472833901,
  3866: 9.23439472833901,
  3867: 9.250016773018734},
 'Volume': {3762: 0.0,
  3763: 0.0,
  3764: 0.0,
  3765: 0.0,
  3766: 0.0,
  3767: 0.0,
  3768: 0.0,
  3769: 0.0,
  3770: 0.0,
  3771: 0.0,
  3772: 0.0,
  3773: 0.0,
  3774: 0.0,
  3775: 0.0,
  3776: 0.0,
  3777: 0.0,
  3779: 0.0,
  3780: 0.0,
  3781: 0.0,
  3782: 0.0,
  3784: 0.0,
  3785: 0.0,
  3786: 0.0,
  3788: 257038800.0,
  3789: 134407800.0,
  3790: 195057600.0,
  3791: 174767800.0,
  3792: 211230200.0,
  3793: 113928800.0,
  3794: 139890800.0,
  3795: 134535000.0,
  3796: 204987000.0,
  3797: 147662000.0,
  3798: 166057200.0,
  3799: 139913800.0,
  3801: 221039000.0,
  3802: 124388600.0,
  3803: 153086200.0,
  3804: 227109800.0,
  3805: 243126000.0,
  3806: 194471600.0,
  3807: 168728800.0,
  3808: 141753200.0,
  3809: 208445200.0,
  3810: 178200800.0,
  3811: 231948800.0,
  3812: 148634200.0,
  3813: 137231600.0,
  3814: 172713800.0,
  3815: 191067400.0,
  3816: 422805600.0,
  3817: 330698600.0,
  3818: 256960200.0,
  3819: 225189800.0,
  3820: 272482800.0,
  3821: 215469200.0,
  3822: 241046000.0,
  3823: 145020400.0,
  3824: 179275400.0,
  3825: 188285800.0,
  3826: 246490800.0,
  3827: 265702000.0,
  3828: 185143200.0,
  3829: 303746000.0,
  3830: 206642600.0,
  3831: 239079600.0,
  3832: 399700800.0,
  3833: 367156400.0,
  3834: 0.0,
  3835: 0.0,
  3836: 0.0,
  3837: 0.0,
  3838: 0.0,
  3839: 0.0,
  3840: 333256200.0,
  3841: 284966400.0,
  3842: 519940400.0,
  3843: 442181500.0,
  3844: 367545800.0,
  3845: 390860600.0,
  3846: 296667600.0,
  3847: 320775600.0,
  3850: 333197400.0,
  3851: 358779000.0,
  3852: 576213400.0,
  3853: 511535600.0,
  3854: 409534200.0,
  3855: 370696800.0,
  3856: 398527200.0,
  3857: 388528000.0,
  3858: 275161200.0,
  3859: 192816600.0,
  3860: 414838800.0,
  3861: 365696000.0,
  3862: 297211400.0,
  3863: 236566000.0,
  3864: 344018800.0,
  3865: 287418600.0,
  3866: 0.0,
  3867: 346798600.0}})

# line parameters using statsmodels
df['Date'] = pd.to_datetime(df['Date'])
df['ix']=np.arange(0, len(df))
mod = sm.OLS(df['Adj Close'],sm.add_constant(df.ix)).fit()
const = mod.params[0]
trend = mod.params[1]

# dict that stores adjusted constants (starting points)
extra_lines = [-0.2,-0.1,0, 0.1,0.2] # add or  remove as you please
model = [{'Line': 'Line_'+str(i+1), 'value': k, 'const': const+k} for i, k in enumerate(extra_lines)]

# plotly
fig = go.Figure(go.Scatter(x=df.ix, y=df['Adj Close']))
for i, m in enumerate(model):
    df[m['Line']]=[(m['const']) + (trend*i) for i,v in enumerate(df.index)]
    fig.add_traces(go.Scatter(x=df.ix, y=df[m['Line']]))
    
fig.show()


来源:https://stackoverflow.com/questions/64852492/plotly-how-to-add-trendline-and-parallel-lines-to-time-series-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!