How to include median and quartiles in a violin plot with the functin violinplot in MATLAB 2024b?
53 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Rohan Kadambi
2025 年 2 月 5 日 16:51
編集済み: Rohan Kadambi
2025 年 2 月 5 日 17:25
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 = [];
その他の回答 (1 件)
Cris LaPierre
2025 年 2 月 5 日 14:40
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"])
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!