Shading the Standard Deviation
185 ビュー (過去 30 日間)
古いコメントを表示
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.
回答 (2 件)
Star Strider
2020 年 3 月 30 日
編集済み: Star Strider
2020 年 3 月 30 日
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 件のコメント
Star Strider
2020 年 3 月 30 日
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
Image Analyst
2020 年 3 月 30 日
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'
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Graphics Performance についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!