Using plotyy with error bars and 3 sets of data...

조회 수: 5 (최근 30일)
Chris E.
Chris E. 2013년 7월 2일
Hello All,
I have an issue with trying to make a 2-D line plot with y-axes on both left and right side for two different number ranges for the y axis. I need error bars as well for the plot. There are 3 sets of data that are made and 2 of them are going to be on the same 1st axis and one of them is going on the 2nd axis. I have really never used the plotyy, let alone trying to make errorbars on the plotyy and doing 3 sets of data on the same plot.
Here is some code that I have been playing with to try to make the the plot as of now:
sliceNum = 10;
figure
ax = gca;
for x = 1:sliceNum+1
s(x) = struct('field1',rand(1,5)*10,'field2',rand(1,5)*10+40,'field3',rand(1,5)*10);
end
needed = s(2:end);
nums = reshape([needed.field2]',5,sliceNum);
cal = [40 0.8 0.9];%need to find source
AreaVal = nums(1,:).*cal(1);
meanVal = nums(2,:).*cal(2);
RMSVal = nums(3,:).*cal(3);
numsStd = reshape([needed.field3]',5,sliceNum);
AreaValStd = numsStd(1,:).*cal(1)*0.05;
meanValStd = numsStd(2,:).*cal(2)*0.05;
RMSValStd = numsStd(3,:).*cal(3)*0.05;
errorbar(1:sliceNum,AreaVal,AreaValStd,'.-','Parent',ax);
hold(ax,'on');
errorbar(1:sliceNum,meanVal,meanValStd,'.-g','Parent',ax);
errorbar(1:sliceNum,RMSVal,RMSValStd,'.-r','Parent',ax);
hold(ax,'off');
xLab='Slice Number';
xlabel(ax,xLab);
ylabel(ax,'count ()');
% title(ax,opts.title);
legend(ax,'Sum','Mean','RMS');
legend(ax,'boxoff');
When this code is run, random data is generated (similar to the real data) and then is plotted, the plot is good, except the green and red lined data are forced to the lower part of the plot due to the blue data. I need the error bars, but have not been able (don't know how really) to put them on in plotyy. Any help or hints or guidance is very appreciated.
Thank you!
Chris
  댓글 수: 2
Chris E.
Chris E. 2013년 7월 3일
Well to answer part of my own question, I found out how to plot with plotyy, but still have no clue how to add the error bars. Here is the new code:
sliceNum = 10;
figure
ax = gca;
for x = 1:sliceNum+1
s(x) = struct('field1',rand(1,5)*10,'field2',rand(1,5)*10+40,'field3',rand(1,5)*10);
end
needed = s(2:end);
nums = reshape([needed.field2]',5,sliceNum);
cal = [40 0.8 0.9];%need to find source
AreaVal = nums(1,:).*cal(1);
meanVal = nums(2,:).*cal(2);
RMSVal = nums(3,:).*cal(3);
numsStd = reshape([needed.field3]',5,sliceNum);
AreaValStd = numsStd(1,:).*cal(1)*0.05;
meanValStd = numsStd(2,:).*cal(2)*0.05;
RMSValStd = numsStd(3,:).*cal(3)*0.05;
[AX,H1,H2] = plotyy(1:sliceNum,AreaVal,1:sliceNum,[meanVal; RMSVal],'plot');
set(get(AX(1),'Ylabel'),'String','Area')
set(get(AX(2),'Ylabel'),'String','Mean and RMS')
set(get(AX(1),'Xlabel'),'String','Slice Number');
set(H2,'LineStyle','--')
legend(AX(1),'Area','Location','best')
legend(AX(2),'Mean', 'RMS','Location','best')
Chris E.
Chris E. 2013년 7월 3일
Well it is not pritty, but here is the code to do that (if anyone knows a better way, please let me know!):
sliceNum = 10;
figure
ax = gca;
for x = 1:sliceNum+1
s(x) = struct('field1',rand(1,5)*10,'field2',rand(1,5)*10+40,'field3',rand(1,5)*10);
end
needed = s(2:end);
nums = reshape([needed.field2]',5,sliceNum);
cal = [40 0.8 0.9];%need to find source
AreaVal = nums(1,:).*cal(1);
meanVal = nums(2,:).*cal(2);
RMSVal = nums(3,:).*cal(3);
numsStd = reshape([needed.field3]',5,sliceNum);
AreaValStd = numsStd(1,:).*cal(1)*0.05;
meanValStd = numsStd(2,:).*cal(2)*0.05;
RMSValStd = numsStd(3,:).*cal(3)*0.05;
[AX,H1,H2] = plotyy(1:sliceNum,AreaVal,1:sliceNum,[meanVal; RMSVal],'plot');
set(get(AX(1),'Ylabel'),'String','Area')
set(get(AX(2),'Ylabel'),'String','Mean and RMS')
set(get(AX(1),'Xlabel'),'String','Slice Number');
legend(AX(1),'Area','Location','best')
legend(AX(2),'Mean', 'RMS','Location','best')
hold(AX(1),'on');
hold(AX(2),'on');
errorbar(AX(1), 1:sliceNum,AreaVal,AreaValStd,'.-');
errorbar(AX(2), 1:sliceNum,meanVal,meanValStd,'.-g');
errorbar(AX(2), 1:sliceNum,RMSVal,RMSValStd,'.-r');
hold(AX(1),'off');
hold(AX(2),'off');

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

채택된 답변

Chris E.
Chris E. 2013년 7월 3일
Well it is not pritty, but here is the code to do that (if anyone knows a better way, please let me know!):
sliceNum = 10;
figure
ax = gca;
for x = 1:sliceNum+1
s(x) = struct('field1',rand(1,5)*10,'field2',rand(1,5)*10+40,'field3',rand(1,5)*10);
end
needed = s(2:end);
nums = reshape([needed.field2]',5,sliceNum);
cal = [40 0.8 0.9];%need to find source
AreaVal = nums(1,:).*cal(1);
meanVal = nums(2,:).*cal(2);
RMSVal = nums(3,:).*cal(3);
numsStd = reshape([needed.field3]',5,sliceNum);
AreaValStd = numsStd(1,:).*cal(1)*0.05;
meanValStd = numsStd(2,:).*cal(2)*0.05;
RMSValStd = numsStd(3,:).*cal(3)*0.05;
[AX,H1,H2] = plotyy(1:sliceNum,AreaVal,1:sliceNum,[meanVal; RMSVal],'plot');
set(get(AX(1),'Ylabel'),'String','Area')
set(get(AX(2),'Ylabel'),'String','Mean and RMS')
set(get(AX(1),'Xlabel'),'String','Slice Number');
legend(AX(1),'Area','Location','best')
legend(AX(2),'Mean', 'RMS','Location','best')
hold(AX(1),'on');
hold(AX(2),'on');
errorbar(AX(1), 1:sliceNum,AreaVal,AreaValStd,'.-');
errorbar(AX(2), 1:sliceNum,meanVal,meanValStd,'.-g');
errorbar(AX(2), 1:sliceNum,RMSVal,RMSValStd,'.-r');
hold(AX(1),'off');
hold(AX(2),'off');

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Two y-axis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by