Adding vertical trendline for mutiple y variables in one scatterplot
5 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a scatterplot with mutiple y variables from different trials. I want to make a trendline for all the trials together, not separate trendlines for each trial. I was reccomended to "draw" a vertical line on the x axis, then all of the trial points that touch that line should be averaged and a single point drawn on that x plane for the accumulative trendline. Then, to make the trendline, I would have to connect all these new dots together from each point of the x plane. Does this make sense? I'm not sure how to do it and would appreciate help!
Thanks!
0 个评论
采纳的回答
Star Strider
2023-7-3
One option is to simply reshape all of the different ‘x’ and ‘y’ data vectors (combined into matrices here for convenience) into a single column using either reshape or the ‘(:)’ operator, and then perform the regression —
x = sort(randn(20, 5));
y = 5*(1:size(x,2)) + randn(size(x)) + 0.5*(1:size(x,1)).';
Line = [x(:) ones(size(x(:)))] * ([x(:) ones(size(x(:)))] \ y(:)); % Calculate Single Linear Regression Line For All Data
figure
hs = scatter(x, y, 'filled', 'DisplayName','Data');
hold on
hp = plot(x(:), Line, 'DisplayName','Linear Regression');
hold off
grid
xlabel('X')
ylabel('Y')
legend([hs hp], 'Location','best')
That is how I would approach it, anyway.
.
10 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Scatter Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!