How to use stacked bar charts to draw multiple confidence intervals

3 comentarios
Hi @ Hongyun,
First, I will define the coefficients, lower bounds, and upper bounds as provided in your question.
% Coefficients
coef = [-0.0186; 0.0057; -0.0067; -0.0007; 0; -0.0295; -0.0517; -0.0651;
-0.0689; -0.0862; -0.0866];
% Lower bounds for confidence intervals
Lower_Bound = [
-0.061944, -0.051528, -0.04632, -0.042792, -0.040104;
-0.04203, -0.03056, -0.024825, -0.02094, -0.01798;
-0.05314, -0.04198, -0.0364, -0.03262, -0.02974;
-0.044302, -0.033824, -0.028585, -0.025036, -0.022332;
0, 0, 0, 0, 0;
-0.07723, -0.06576, -0.060025, -0.05614, -0.05318;
-0.103042, -0.090704, -0.084535, -0.080356, -0.077172;
-0.121602, -0.108024, -0.101235, -0.096636, -0.093132;
-0.132368, -0.117116, -0.10949, -0.104324, -0.100388;
-0.152506, -0.136572, -0.128605, -0.123208, -0.119096;
-0.183092, -0.159904, -0.14831, -0.140456, -0.134472
];% Upper bounds for confidence intervals
Upper_Bound = [
0.024744, 0.014328, 0.00912, 0.005592, 0.002904;
0.05343, 0.04196, 0.036225, 0.03234, 0.02938;
0.03974, 0.02858, 0.023, 0.01922, 0.01634;
0.042902, 0.032424, 0.027185, 0.023636, 0.020932;
0, 0, 0, 0, 0;
0.01823, 0.00676, 0.001025, -0.00286, -0.00582;
-0.000358, -0.012696, -0.018865, -0.023044, -0.026228;
-0.008598, -0.022176, -0.028965, -0.033564, -0.037068;
-0.005432, -0.020684, -0.02831, -0.033476, -0.037412;
-0.019894, -0.035828, -0.043795, -0.049192, -0.053304;
0.009892, -0.013296, -0.02489, -0.032744, -0.038728
];Then calculate the heights of the bars to create the stacked bar chart. The heights for the lower bounds will be the difference between the coefficients and the lower bounds, while the heights for the upper bounds will be the difference between the upper bounds and the coefficients.
% Calculate the heights for the lower and upper bounds
lower_heights = coef - Lower_Bound;
upper_heights = Upper_Bound - coef;
Once you have the heights for the lower and upper bounds, you can create the stacked bar chart using the bar function.
% Create the stacked bar chart
figure;
bar([lower_heights, upper_heights], 'stacked');
% Set the x-axis labels
set(gca, 'XTickLabel', {'1%', '5%', '10%', '15%', '20%'});
xlabel('Confidence Levels');
ylabel('Coefficient Values');
title('Stacked Bar Chart of Coefficients with Confidence Intervals');legend({'Lower Bound', 'Upper Bound'}, 'Location', 'Best');
Finally, you can customize the chart to enhance its readability and presentation. This includes adding grid lines, adjusting colors, and ensuring that the legend is clear.
% Customize the appearance
grid on;
% Custom colors for lower and upper bounds
colormap([0.8 0.2 0.2; 0.2 0.8 0.2]);
Please see attached.


Feel free to adjust the aesthetics and parameters to better fit your specific needs and preferences. If you have any further questions, please let me know.
Respuesta aceptada
Más respuestas (1)
3 comentarios
Categorías
Más información sobre Data Distribution Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!















