0

I'm trying to calculate some regression lines. The data consists of +2 data points. I'm using plotly express for visualisation and for retrieving trendline slope and intercept values.

Example of what I'm doing:

import plotly.express as px
import datetime


dataBank={'Date': {1480: 1605101400000, 1481: 1605101700000, 1482: 1605102000000, 1483: 1605102300000, 1484: 1605102600000, 1485: 1605102900000, 1486: 1605103200000, 1487: 1605103500000, 1488: 1605103800000, 1489: 1605104100000, 1490: 1605104400000, 1491: 1605104700000, 1492: 1605105000000, 1493: 1605105300000, 1494: 1605105600000, 1495: 1605105900000, 1496: 1605106200000, 1497: 1605106500000, 1498: 1605106800000, 1499: 1605107100000}, 'Data': {1480: 15641.8, 1481: 15644.77, 1482: 15606.79, 1483: 15613.0, 1484: 15610.01, 1485: 15569.64, 1486: 15591.02, 1487: 15600.97, 1488: 15615.15, 1489: 15589.17, 1490: 15591.47, 1491: 15607.16, 1492: 15583.8, 1493: 15617.69, 1494: 15637.94, 1495: 15654.0, 1496: 15643.99, 1497: 15644.0, 1498: 15615.0, 1499: 15607.26}}

val={'Date': {0: 1605104700000, 1: 1605106200000}, 'Value': {0: 15612.59, 1: 15661.55}}

dataBank=pd.DataFrame(dataBank)
val=pd.DataFrame(val)

fig = px.scatter(val, x="Date", y="Value", trendline="ols")

fig.add_scatter(x=dataBank.Date, y=dataBank.Data)
fig.show()
    
fit_results = px.get_trendline_results(fig).px_fit_results.iloc[0]
m=fit_results.params[1]
b=fit_results.params[0]
 
x=datetime.datetime.now()

x=x.timestamp()

y=(m*x) + b
print(y)


However this is how the plot always end up looking:

enter image description here

The line is fitted horizontally between the two points. but this is not what I want. I want a line that crosses the two points that gives me the slope of these points.

What am I missing?

normal_human
  • 165
  • 9
  • Please include a code sample that reproduces your plot as it's shown. Your existing code sample will *not* do that as it will crash already at `val = DataFrame()`. Please take a look [here](https://stackoverflow.com/questions/63163251/pandas-how-to-easily-share-a-sample-dataframe-using-df-to-dict/63163254#63163254) on how to share a sample dataframe. – vestland Nov 11 '20 at 14:46
  • @vestland Thanks for the input, I updated the question, is it better this way? – normal_human Nov 11 '20 at 15:17
  • Probably. But just try and run it on your end you'll know whether it works or not – vestland Nov 11 '20 at 15:25
  • 1
    yes, it works fine – normal_human Nov 11 '20 at 15:26

0 Answers0