stackedplot and linear fit

조회 수: 5 (최근 30일)
zakary Surprenant
zakary Surprenant 2020년 11월 24일
편집: Adam Danz 2020년 11월 24일
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
Rik
Rik 2020년 11월 24일
To create a linear fit, you will need to extract the x and y positions. What did you try?
zakary Surprenant
zakary Surprenant 2020년 11월 24일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2020년 11월 24일
편집: Adam Danz 2020년 11월 24일
> 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개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by