Spider Plot with Standard Deviation as shaded region

10 views (last 30 days)
I want to plot a spider plot where each spoke represents the average value with solid line. In addition, I want to show standard deviation as shaded region around the average plot (Average+SD and Average-SD). I have attached a figure to show the desired outcome.
Thank you in advance.
  2 Comments
Ayesha Tooba Khan
Ayesha Tooba Khan on 4 Apr 2025
Edited: Ayesha Tooba Khan on 6 Apr 2025
Yes, I am using this code only. But, if I am keeping 'fill' as 'on' it is filling the etire region starting from the origin. I only want to have SD region as shaded one.
Thank you for the response.

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 4 Apr 2025
Edited: Mathieu NOE on 4 Apr 2025
hello again
I just created a quick and dirty slightly modified function (sorry for messing it ! ) , so I could suggest this - if this is what you want
for the demo I assumed that each D vector has a correspond std vector called stdD which here is 10% of its nominal D value (of course you can put your own data in here)
custom function is attached (my apologizes to it's creator !) (NB : I simply deactivated certain checks which would otherwise throw error messages)
my result :
% Initialize data points
D1 = [5 3 9 1 2];
D2 = [5 8 7 2 9];
D3 = [8 2 1 4 6];
P = [D1; D2; D3];
% Multiple shaded regions
stdD1 = 0.1*D1;
stdD2 = 0.1*D2;
stdD3 = 0.1*D3;
axes_shaded_limits = {...
[D1-stdD1;D1+stdD1],... % [min axes limits; max axes limits]
[D2-stdD2;D2+stdD2],...
[D3-stdD3;D3+stdD3]};
% Spider plot
spider_plot_mod1(P,...
'AxesShaded', 'on',...
'AxesShadedLimits', axes_shaded_limits,...
'AxesShadedColor', {'b', 'r','y'},...
'AxesShadedTransparency', 0.1);
  8 Comments
Ayesha Tooba Khan
Ayesha Tooba Khan on 8 Apr 2025
Yes, I got stuck there but got that resolved.
just a little change: according to my problem, the axes_shaded_limit would be
axes_shaded_limits = {[LowerBoundUp;UpperBoundUp],...
[LowerBoundDown;UpperBoundDown]};
as the average will have shaded region of lower and upper bounds (for Up and Down condition which corresponds to spider plots on a single tile, in my case).
Just wanted to clarify, if anyone wants to use the same in future.
I sincerely appreciate your help. Thank you so much.

Sign in to comment.

More Answers (1)

Thorsten
Thorsten on 4 Apr 2025
Edited: Thorsten on 4 Apr 2025
You can do it like this:
I am not aware of a function, so you have to do it on your own.
Note that this functions uses values maxval = 7 and offset = 4 that are tailored to your data to have an axis running from -4 to 3.
Your friend is pol2cart to convert all the polar stuff to Cartesian coordinates and then use plot, patch or line commands.
function spiderplot(val, sd, col)
if nargin < 1
val = [-1 -1 1 -1 -1 -1 0];
end
if nargin < 2
Nspokes = size(val, 2);
sd = ones(1, Nspokes) + 0.5*rand(1, Nspokes);
end
if nargin < 3
col = 'r'; % or [234 170 58]/266; or 'g' or any other color
end
labels = {'Degree', 'Density', 'Betweeness Centrality', 'Closeness', 'Eigenvector', 'Transivity', 'Entropy'};
offset = 4; % map the smallest value to 0
maxval = 7;
val = val + offset;
Nspokes = size(val, 2);
theta = linspace(0, 2*pi, Nspokes + 1);
%% plot the axes (background, circles and spokes)
% add a grayish background circle
col_bg = [229, 236, 246]/255;
Npoints = 100;
thetaN = linspace(0, 2*pi, Npoints);
[xc, yc] = pol2cart(thetaN, maxval);
patch(xc, yc, col_bg, 'EdgeColor', 'none')
hold on
% add the spokes
for i = 1:Nspokes
[xs, ys] = pol2cart(theta(i), maxval);
line([0, xs], [0, ys], 'Color', 'w')
end
% add the circles
for i = 1:maxval
[xc, yc] = pol2cart(thetaN, i);
plot(xc, yc, 'w-')
end
% label the axis
for i = 0:maxval
str = int2str(i - offset);
% change dash to minus:
str = strrep(str, '-', char(hex2dec('2212')));
text(i, 0, str, 'FontSize', 14,...
'HorizontalAlignment', 'center', 'VerticalAlignment', 'top')
end
axis off
%% plot the data
% append first element to plot a closed curve
val = [val, val(:, 1)];
sd = [sd, sd(:, 1)];
% plot data
[x, y] = pol2cart(theta, val); % convert to Cartesian
plot(x, y, '-', 'Color', col)
hold on
% plot sd lines
[x1, y1] = pol2cart(theta, val - sd);
[x2, y2] = pol2cart(theta, val + sd);
plot(x1, y1, '--', 'Color', col)
plot(x2, y2, '--', 'Color', col)
% shade the area between the +/- SD lines
h = patch([x1 fliplr(x2)], [y1 fliplr(y2)], col,...
'EdgeColor', 'none', 'FaceAlpha', 0.2);
  1 Comment
Ayesha Tooba Khan
Ayesha Tooba Khan on 5 Apr 2025
Thank you so much for considering even the minor details of the example figure.

Sign in to comment.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!