stackedplot and linear fit
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
zakary Surprenant
el 24 de Nov. de 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 comentarios
Rik
el 24 de Nov. de 2020
To create a linear fit, you will need to extract the x and y positions. What did you try?
Respuesta aceptada
Adam Danz
el 24 de Nov. de 2020
Editada: Adam Danz
el 24 de Nov. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Line Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

