How to include median and quartiles in a violin plot with the functin violinplot in MATLAB 2024b?
41 views (last 30 days)
Show older comments
Hi,
How to include median and quartiles in a violin plot with the functin violinplot in MATLAB 2024b? The function violinplot only gives the violin shape but does not include any information like medain or quartiles.
Also, I have data limitted to values between 0 and 1 but the violin plot shows values above 1 and below zero. Is it normal? Thanks.
0 Comments
Accepted Answer
Rohan Kadambi
on 5 Feb 2025
Edited: Rohan Kadambi
on 5 Feb 2025
The built in violinplot funciton does not provide that functionality. You can manually add the median as a point per Cris's answer or if you prefer a solid line (like in a boxchart) you can use the DensityValues field of the plot handle extract a desired width. You can use similar logic if you wish to draw on quartiles as well.
% Draw from the normal distribution 3 times
ydata = randn(100,3);
% Compute Median
median_list = median(ydata);
% Prepare Figure
fh = figure;
ax = axes(fh, NextPlot="add");
% Draw the Violin Plot
v = violinplot(ax, ydata);
% Plot a black x for each violinplot
p = plot(median(ydata),'kx');
% Draw a line across the violinplot at each median
for i = 1:numel(median_list)
% Estimate the width at the median
density_median = interp1(v(i).EvaluationPoints,...
v(i).DensityValues,...
median_list(i));
% Compute the width of the violin plot at the median
width_median = v(i).DensityWidth.*density_median./max(v(i).DensityValues);
% Center the line on the current x_value with the correct width
x_values = double(v(i).XData(1)) + [-0.5 0.5]*width_median;
y_values = [median_list(i) median_list(i)];
% Draw the line with the correct color. If you're using manual coloring
% you can instead use the Color, FaceColor, EdgeColor properties as
% appropriate
plot(x_values, y_values, SeriesIndex=v(i).SeriesIndex);
end
title("Violinplot with Median");
A violinplot doesn't actually plot your data directly, instead, it estimates the distribution of your data using a kernel density estimator. MATLAB computes the kernel density estimate with the built-in kde function. By default, this function uses a normal distribution kernel with an unbounded support whichs is not appropriate for data bounded by [0,1]. If you want a plot that explicitly respects this bounding by default, I suggest you consider using a boxchart or swarmchart. Howerver, you can still use a violinplot if you explicitly modify the support.
% Draw data from the uniform random distribution which is bounded on [0 1]
ydata = rand(100,1);
% Prepare Figure
fh = figure;
ax = axes(fh, NextPlot="add");
% Violin Plot with restricted support
[f,xf] = kde(ydata, Support=[0 1]);
v = violinplot(ax, EvaluationPoints=xf, DensityValues=f);
% Default Violin Plot
violinplot(ax, 2*ones(size(ydata)), ydata);
% BoxChart
boxchart(ax, 3*ones(size(ydata)), ydata);
% SwarmChart
swarmchart(ax, 4*ones(size(ydata)), ydata);
% Plot Cleanup
title("Comparison of Plot Types")
legend(["violinplot (restricted support)"
"violinplot (default)"
"boxchart (default)"
"swarmchart (default)"],...
Location="northeast")
ylim([-0.5 2])
ax.XAxis.TickValues = [];
More Answers (1)
Cris LaPierre
on 5 Feb 2025
That functionality is not currently available in violinplot. However, you might consider using this File Exchange implementation of violin plots: https://www.mathworks.com/matlabcentral/fileexchange/45134-violin-plot
Otherwise, you can add them manually.
ydata = randn(100,3);
violinplot(ydata)
hold on
p(1)=plot(mean(ydata),'ro');
p(2)=plot(median(ydata),'kx');
hold off
ylabel("Age (years)")
legend(p,["Mean","Median"])
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!