Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
832 views
in Technique[技术] by (71.8m points)

plot - MATLAB fill area between lines

I'm trying to do something similar to what's outlined in this post: MATLAB, Filling in the area between two sets of data, lines in one figure but running into a roadblock. I'm trying to shade the area of a graph that represents the mean +/- standard deviation. The variable definitions are a bit complicated but it boils down to this code, and when plotted without shading, I get the screenshot below:

x = linspace(0, 100, 101)';    
mean = torqueRnormMean(:,1);
meanPlusSTD = torqueRnormMean(:,1) + torqueRnormStd(:,1);
meanMinusSTD = torqueRnormMean(:,1) - torqueRnormStd(:,1);
plot(x, mean, 'k', 'LineWidth', 2)
plot(x, meanPlusSTD, 'k--')
plot(x, meanMinusSTD, 'k--')

mean and std

But when I try to implement shading just on the lower half of the graph (between mean and meanMinusSTD) by adding the code below, I get a plot that looks like this:

fill( [x fliplr(x)],  [mean fliplr(meanMinusSTD)], 'y', 'LineStyle','--');

shaded

It's obviously not shading the correct area of the graph, and new near-horizontal lines are being created close to 0 that are messing with the shading.

Any thoughts? I'm stumped.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You may be getting a problem with using mean as a variable, since it's also a reserved MATLAB command. Try clearing the variable space and then using a unique variable name.

As for the second problem, you want

fill( [x fliplr(x)],  [meanUniqueName fliplr(meanMinusSTD)], 'y', 'LineStyle','--');

You also don't need to do this in two steps, but can do it all at once. A code snippet from a script I'm currently working on does the exact same thing and contains the lines:

    avar = allan(DATA, tau);
    xFill = [avar.tau1 fliplr(avar.tau1)];
    yFill = [avar.sig2+avar.sig2err fliplr(avar.sig2-avar.sig2err)];

    figure(2);
    fill(xFill,yFill,'y','LineStyle','--')
    line(avar.tau1,avar.sig2);

So I fill the area between the two error lines, and then draw the data line on top.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...