Shading the Standard Deviation

191 visualizzazioni (ultimi 30 giorni)
Logan Thurston
Logan Thurston il 30 Mar 2020
Commentato: Star Strider il 30 Mar 2020
I have this graph, with the plotted mean and +-Std Dev. I need to shade the area between the upper and lower deviation. I have tried fill and area but they create very distorted images.
Any guidance would be appreciated.
  3 Commenti
Image Analyst
Image Analyst il 30 Mar 2020
Modificato: Image Analyst il 30 Mar 2020
Logan, read this link. And see my and Star's answers below.
Shading between curves is in the FAQ. (I just added it.)
Logan Thurston
Logan Thurston il 30 Mar 2020
Heres the data, and my code.

Accedi per commentare.

Risposte (2)

Star Strider
Star Strider il 30 Mar 2020
Modificato: Star Strider il 30 Mar 2020
Try this (obviously with your data, not my simulated vectors):
x = 1:50; % Create: ‘x’ Data
y = randi([100 150], size(x)); % Create: ‘y’ Data
sd = 10*rand(size(x)); % Create: Standard Deviation Vector
figure
plot(x, y)
hold on
patch([x fliplr(x)], [y-sd fliplr(y+sd)], [0.6 0.7 0.8])
hold off
That will work for both of your data sets. Use a separate patch call for each one.
If your data ar column vectors ratther than the row vectors in my simulation, vertically concatenate them using the semicolon (;) and use flipud instead of fliplr.
Example —
patch([x(:); flipud(x(:))], [y(:)-sd(:); flipud(y(:)+sd(:))], [0.6 0.7 0.8])
(The (:) subscript convention forces them to be column vectors.)
EDIT — (30 Mar 2020 at 17:17)
The ‘t’ vector must be forced to be a column vector for the code to work, since all the rest are column vectors:
figure(1)
plot(t(:),(NSDR1),'r');
hold on;
plot(t(:),(PSDR1),'r');
patch([t(:); flipud(t(:))],[NSDR1; flipud(PSDR1)], 'r', 'FaceAlpha',0.2, 'EdgeColor','none');
hold off
  3 Commenti
Logan Thurston
Logan Thurston il 30 Mar 2020
Modificato: Logan Thurston il 30 Mar 2020
I have converted t into a colum vector by doing t=t'; but i still get the same error. All my values in the workpsace are 8000x1 column vectors.
Edit- I also tried using the (:) convention prior to transposing with t' but no dice.
Star Strider
Star Strider il 30 Mar 2020
The code I posted (the last part derived from your code) ran without error.
Please use the code I posted.
If you also want the ‘Block1’ mean value to appear, add that plot call:
figure(1)
plot(t(:),(NSDR1),'r');
hold on;
plot(t(:),(PSDR1),'r');
patch([t(:); flipud(t(:))],[NSDR1; flipud(PSDR1)], 'r', 'FaceAlpha',0.2, 'EdgeColor','none');
plot(t(:), Block1, 'r')
hold off
That code produces this plot:
The complete code:
Assignment = load('Assignment(2).txt');
Fs=2048;
%2- Isolate Trails 1-5, 21-25
MVC1=Assignment(:,1:5);
MVC2=Assignment(:,21:25);
t=[1:length(MVC1)]*(1/Fs);
Block1=mean(MVC1,2);
Block2=mean(MVC2,2);
SD1=std(MVC1,0,2);
SD2=std(MVC2,0,2);
NSDR1=(Block1-SD1);
PSDR1=(Block1+SD1);
figure(1)
plot(t(:),(NSDR1),'r');
hold on;
plot(t(:),(PSDR1),'r');
patch([t(:); flipud(t(:))],[NSDR1; flipud(PSDR1)], 'r', 'FaceAlpha',0.2, 'EdgeColor','none');
plot(t(:), Block1, 'r')
hold off

Accedi per commentare.


Image Analyst
Image Analyst il 30 Mar 2020
Here's a general purpose demo that you can adapt as needed.
% Shades region between two curves with light green.
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 15;
numPoints = 600;
x = 1 : numPoints;
% Make and plot y1.
period1 = 300;
y1 = cos(2 * pi * x / period1);
% plot(x, y1, 'r-', 'LineWidth', 3);
% Make and plot y2.
period2 = 100;
y2 = cos(2 * pi * x / period2);
hold on;
% plot(x, y2, 'b-', 'LineWidth', 3);
grid on;
% Shade the area between in light green using the patch() function.
lightGreen = [0.85, 1, 0.85];
% Create the boundaries of the upper points and the lower points.
% Assume y1 and y2 have the same number of elements located at the same x values.
upperBoundary = max(y1, y2);
lowerBoundary = min(y1, y2);
% Now do the actual display of the shaded region.
patch([x fliplr(x)], [upperBoundary fliplr(lowerBoundary)], lightGreen);
% Plot y1 and y2 AFTER the patch so the patch does not cover any of those curves.
hold on;
plot(x, y1, 'r-', 'LineWidth', 2);
plot(x, y2, 'b-', 'LineWidth', 2);
legend('Patch', 'y1', 'y2');
xlabel('X', 'FontSize', 24);
ylabel('Y', 'FontSize', 24);
% Maximize the figure window
g = gcf; % Doesn't work on gcf directly for some reason.
g.WindowState = 'maximized'

Categorie

Scopri di più su Graphics Performance in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by