-
-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trendline for scatter plots #471
Comments
Hi there! |
Is it because you are also setting the x axis? Btw, happy to hear you are having fun with it! |
Thanks for the quick reply. An example, I'm using one entity (heat produced by heatpump) to set Y and another (outdoor temperature to set X. When I define a trendline as set above I'm getting a line from point to point in the scatter, but not a trendline fitted trough the dataset. |
See my comment above, also make sure your yaml is well defined (you have issues with the filters there. Try the yaml editor helper linked at the top of the readme to validate your yaml |
Unfortunately I don't understand how I could use 'map_y' and 'map_x' to get to a correct trendline in the scatter plot. My full code looks like this:
|
Oh, sorry, not map_y/x, the fn filter: filters:
- fn: |
({vars}) => ({xs: vars.temperature, ys: vars.heat})
- trendline:
type: linear
show_formula: true
show_r2: true I think this may work |
Uhmmmm on a second thought, I think the problem is just that your x axis isn't sorted. Try this: - fn: |
({vars}) => {
const records = vars.temperature.map((x,i)=>({
x,
y:vars.heat[i]
})).sort((a,b)=>a.x-b.x);
return {
xs: records.map(({x})=>x),
ys: records.map(({y})=>y)
};
} Followed by the Trendline filter |
Oke, just tried it. Getting this now: |
try again, corrected a missing parenthesis. |
That fixes the code but still gives me a scatter with a line from dot to dot. |
show me what you've got |
Yes, I've got this now.
|
type: custom:plotly-graph
$fn: |
$ex {
// ignore this block
vars.temperature = [2,4,12,2,4,5,67,3,3,2];
vars.heat = [4000,2000,2000,3000,1000,4000,6000,7000,5000,3000,1];
}
entities:
- entity: ""
mode: lines
name: sorted data
fn: |
$ex {
const records = vars.temperature
.map((x, i) => ({
x,
y: vars.heat[i],
}))
.sort((a, b) => a.x - b.x);
vars.sorted = {
xs: records.map(({ x }) => x),
ys: records.map(({ y }) => y),
};
}
x: $ex vars.sorted.xs
y: $ex vars.sorted.ys
- entity: ""
name: trendline
mode: lines
filters:
- load_var: sorted
- trendline:
type: linear
show_formula: true
show_r2: true
- store_var: trendline
x: $ex vars.trendline.xs.map(x=>+x)
y: $ex vars.trendline.ys
raw_plotly_config: true
layout:
showlegend: true
margin:
r: 50
l: 75
t: 50
b: 75
legend:
orientation: h
xaxis:
title:
text: Temperature
range:
- -5
- 18
showgrid: false
zeroline: false
yaxis:
title:
text: Heat produced (W)
range:
- 0
- 7500
showgrid: false
zeroline: false
|
I think that is just numerical error, you can mal it again to remove all but the first and last data points of the trendline |
I'm curious why your example code gave such a different trendline without this problem. In this thread I also saw similar issues with the histogram plots having piecewise trendlines: Also the formula of the trendline isn't showing up. |
The formula and error squared (r2) will go into the name of the trace, so to see them you can:
Regarding the piece wise trend line, to be honest I don't know, maybe the lib I use for the linear trendline (https://www.npmjs.com/package/ml-regression-simple-linear) thinks it should use integers or something like that. You could try using the |
First off, what a great card. I'm really having a lot of fun making new visualizations of my data.
Describe the solution you'd like
I want to add a trendline to my scatter plots.
How would it be defined in yaml?
Scribble
Examples of how this could look like in Plotly can be found here: https://plotly.com/python/linear-fits/
The text was updated successfully, but these errors were encountered: