MATLAB Answers

stackedplot and linear fit

5 views (last 30 days)
zakary Surprenant
zakary Surprenant 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? 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
Rik on 24 Nov 2020
To create a linear fit, you will need to extract the x and y positions. What did you try?
zakary Surprenant
zakary Surprenant on 24 Nov 2020
So i would have to extract all my data from 60 graphs and then replot them with a graph for each? I was looking into stackedplot because it graphs everything with the same x, as my x-axis is the same through all my graphs.

Sign in to comment.

Accepted Answer

Adam Danz
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

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!