How to plot discrete data in a stackedplot

조회 수: 12 (최근 30일)
jonas
jonas 2021년 3월 26일
편집: dpb 2021년 3월 29일
I'd like to plot timetable data consisting of discrete data (e.g. boolean, enumerations) in a stackedplot (common time x-axis).
In order to plot discrete data there are functions like stairs which supress (?) linear interpolation and plot like interpolation method "zoh".
Is it possible to change the interpolation of stackedplot (to "zoh") or a similar plot with time cursor (lets call it stackedstairs) ? Or do I have to use multiple subplots with stairs and link the x-axis?

채택된 답변

dpb
dpb 2021년 3월 29일
편집: dpb 2021년 3월 29일
There's a 'PlotType' property in the stackedplot object that has the option for 'stairs' -- none of the previous gyrations are needed, simply
hSP=stackedplot(tbl,vars); % stacked plot for starters
set(hSP.LineProperties,{'PlotType'},{'stairs'})
Have to use set since there is an array of axes and the dot notation can't deal with arrays.

추가 답변 (1개)

dpb
dpb 2021년 3월 26일
편집: dpb 2021년 3월 26일
You can get creative and use stackedplot to create the axes for you, and then go "handle-diving" to find and modify the content of the underlying axes...
I used one of the examples for stackedplot as a start point --
tbl = readtable('patients.xls'); % some sample data
vars={'Height','Weight','Systolic','Diastolic'}; % pick the variables
hSP=stackedplot(tbl,vars); % stacked plot for starters
hAx=flip(hSP.NodeChildren(numel(vars)+1:end)); % get the axes handles
for i=1:numel(hAx) % for each axes (same as numel(vars))
hold(hAx(i),'on') % set hold on for each axes to keep other properties unchanged
hL=hAx(i).Children; % get existing line handle
delete(hL); % delete that line
hSL(i)=stairs(hAx(i),tbl.(vars{i})); % replace with stairs line
end
Above yields
Introducing a function handle for the plot function into stackedplot seems worthwhile enhancmenet request.
NOTA BENE: The stackedplot object hides the axes handles in the undocumented and hidden NodeChildren property. Another case IMO of TMW getting far too clever in their penchant to create opaque objects.
Inspection showed the Axes are the last elements in the array and are stored in obverse order of their creation; this is a pretty common (universal?) order in such case; one can tell which is which by inspection of the .Position property for the bottom location of the axes. Remember the position 4-vector is [left bottom width height]
  댓글 수: 11
jonas
jonas 2021년 3월 29일
I created a minimal example to reproduce this error. Run as a script, not in command window.
clear;figure(1);clf;
tbl = readtable('patients.xls');
vars={'Height','Weight','Systolic','Diastolic'};
hSP=stackedplot(tbl,vars);
hAx=flip(hSP.NodeChildren(numel(vars)+1:end));
for i=1:numel(hAx)
hold(hAx(i),'on')
hL(i)=hAx(i).Children; %save hL to array
% delete(hL); %don't delete here
hSL(i)=stairs(hAx(i),tbl.(vars{i}));
end
pause(1); %comment/uncomment this line for race condition(?)
delete(hL); %delete the whole array
The pause here seems to be the key. In interactive mode you always have a little pause.
The code above does produce the correct figure but you lose the common data cursor!
So to summarize this up: instead of deleting, I just set the linestyle to none, as mentioned above.
Thanks for your help!
dpb
dpb 2021년 3월 29일
편집: dpb 2021년 3월 29일
" I just set the linestyle to none after the loop."
You can do that first instead of last to make them not show. Of course, that leaves the old data in place as well, but "any port in a storm!" when it comes to workarounds.
However, see the alternate Answer for the REAL workaround -- but your above reference got me looking more deeply at the stackedplot object and turns out TMW had it built-in all along -- and it is even documented in an example, I just didn't look at all the examples.

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

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by