Shading area between curves using gramm

9 views (last 30 days)
Cara Masterson
Cara Masterson on 27 Apr 2020
Answered: Harshavardhan on 21 Feb 2025
I am trying to generate graphs for reporting. I discovered gramm and it makes much of what I am doing easy to manipulate and looks nice. My one struggle is trying to shade an area between two curves. As an overview, I am trying to graph joint angles of the knee in one plane, left and right, against a band of control data. I have succeessfully generated a graph containig the discrete knee angles from one participant (6 left and 6 right), successfully utilized a custom colormap, altered all aspects of the graph that I desire to (title, axes names, etc) except add this component. This tool can easily generate shaded areas related to statistics of the data lines specifically, such as a confidence interval or standard deviation of the input data, but I would like to utilize my control data as the shaded area, see how far an individual is from the "norm". I have a mean and a standard deviation vector for my contol data, so I can generate an upper bound and a lower bound for the desired shaded area. Ideally I would shade one standard deviation and two standard deviations from control norm as seen below. My first thougth was to manipulate my control data and utilize one of the built in statistical functions where it will shade what I'm looking for (although only one standard deviation), but I have not successfully done that. Any and all help is greatly appreciated.
What I would like to generate (note actual data values are different between graphs, I am only using for style reference):
What I can generate currently:
This code generates the above graph:
KNEE_Flex = cat(1,LKNEE_Strides,RKNEE_Strides);
ColorArray = cat(1,blue,red);
cycle = 0:1:100;
Knee_X_Mean = gramm('x',cycle','y',KNEE_Flex,'color',ColorArray);
Knee_X_Mean.set_color_options('map',NMGcolormap);
Knee_X_Mean.set_names('x','% cycle','y','degree');
Knee_X_Mean.set_title('Knee Flexion (+)');
Knee_X_Mean.geom_line;
Knee_X_Mean.draw();

Answers (1)

Harshavardhan
Harshavardhan on 21 Feb 2025
To incorporate the shading between two curves into your existing code, you can enhance it by defining polygons for the shaded areas and rendering them with the “geom_polygon” method. Here are the additional lines you need to include in your original code to accomplish this:
% Calculate upper and lower bounds for 1 and 2 standard deviations
% Let control_mean and control_std to be the mean and standard deviation data of the control data that you mentioned you have
upper_bound_1std = control_mean + control_std;
lower_bound_1std = control_mean - control_std;
upper_bound_2std = control_mean + 2 * control_std;
lower_bound_2std = control_mean - 2 * control_std;
% Define polygons for the shaded areas
x_polygon = {cat(2, cycle, fliplr(cycle))};
y_polygon_1std = {cat(2, upper_bound_1std, lower_bound_1std)};
y_polygon_2std = {cat(2, upper_bound_2std, lower_bound_2std)};
% Define color for the shaded areas
green = [0 1 0];
% Initialisation is same as before
KNEE_Flex = cat(1,LKNEE_Strides,RKNEE_Strides);
ColorArray = cat(1,blue,red);
cycle = 0:1:100;
Knee_X_Mean = gramm('x', cycle', 'y', KNEE_Flex, 'color', ColorArray);
% Add shaded areas for 1 and 2 standard deviations
Knee_X_Mean.geom_polygon('x', x_polygon, 'y', y_polygon_2std, 'color', green, 'alpha', 0.2);
Knee_X_Mean.geom_polygon('x', x_polygon, 'y', y_polygon_1std, 'color', green, 'alpha', 0.4);
% Set color options and other properties
Knee_X_Mean.set_color_options('map', NMGcolormap);
Knee_X_Mean.set_names('x', '% cycle', 'y', 'degree');
Knee_X_Mean.set_title('Knee Flexion (+)');
Knee_X_Mean.geom_line;
% Draw the plot
Knee_X_Mean.draw();
You can find more information about “gramm” here:
Hope this helps.

Categories

Find more on Vector Fields in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!