stackedplot and linear fit
10 views (last 30 days)
Show older comments
i was just wondering if they was any way i could put a linear fit line and get its data from stackedplot? I am plotting about 60 graphs so using stackedplot makes it really easy but can't find anything in the help section;
This is a part of my graph where TT_Montlymin,max,mean are 432x3 timetable
%This sets them all together (Max,Min,Mean)
tt=synchronize(TT_Monthlymin,TT_MonthlyMax,Monthlymean);
%this plots them all (Max,Min,Mean)
stackedplot(tt)
2 Comments
Rik
on 24 Nov 2020
To create a linear fit, you will need to extract the x and y positions. What did you try?
Accepted Answer
Adam Danz
on 24 Nov 2020
Edited: Adam Danz
on 24 Nov 2020
> i was just wondering if they was any way i could put a linear fit line and get its data from stackedplot?
If you're creating the stackedplot then you alread have the x,y coordinates which you can use to compute the regression lines. To add those lines to the stackedplot you'll need to use undocumented methods since the axis handles are unavailable by default.
Here's a demo
% Create stacked plot
x = linspace(0,20,40);
y = x(:).*[1,2,4,8]+(rand(numel(x),4)-.5)*10;
h = stackedplot(x,y, 'o');
% Get slope and intercepts (slope, intercept)
coefs = arrayfun(@(i){polyfit(x,y(:,i),1)},1:size(y,2));
% Add refline
ax = flipud(findobj(h.NodeChildren, 'Type','Axes'));
arrayfun(@(i)refline(ax(i),coefs{i}(1),coefs{i}(2)),1:numel(ax))
If you do not have access to the raw data and only have the figure, you can get the x,y coordinates and add reference lines using,
% Create stacked plot (for demo only)
x = linspace(0,20,40);
y = x(:).*[1,2,4,8]+(rand(numel(x),4)-.5)*10;
h = stackedplot(x,y, 'o');
drawnow()
% Get figure handle, stackedplot handle, and axes handles
fig = gcf();
s = findobj(fig,'Type','stackedplot');
ax = flipud(findobj(s.NodeChildren, 'Type','Axes'));
% Loop through each axis, get (x,y) coordinates, compute and plot reg line
% This assumes there's only 1 group of data within each axes.
for i = 1:numel(ax)
x = get(ax(i).Children,'XData');
y = get(ax(i).Children,'YData');
coef = polyfit(x,y,1);
refline(ax(i),coef(1),coef(2))
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!