Position x labels in a compound bar plot

3 visualizaciones (últimos 30 días)
Kevin Thomson
Kevin Thomson el 6 de Abr. de 2018
Respondida: Jatin el 14 de Oct. de 2024
I have a plot made up of three bar plots and they each have a value for every x = {1,...,N}, i.e.
bar1(1) = 0.1, bar2(1) = 0.2, bar3(1) = 0.5 ... bar1(N) = 0.4, bar2(N) = 0.7, bar3(N) = 0.6
I managed to plot all three by plotting them separately, holding on, offsetting the x for bar1 by 0, for bar2 by 0.33 and for bar3 by 0.33 and modifying the 'Barwidth' property for each to 0.33. However, the xlabel appears under bar 1. I want to it to appear under bar2, i.e., in the middle of the three bars.
How can I do that?

Respuestas (1)

Jatin
Jatin el 14 de Oct. de 2024
To position the xlabel under bar 2, you can use the 'xticks' function to set the x-label positions. Here's an example code that demonstrates how to do this:
% Sample data
N = 5; % Number of groups
bar1 = [0.1, 0.3, 0.2, 0.4, 0.5];
bar2 = [0.2, 0.4, 0.3, 0.7, 0.6];
bar3 = [0.5, 0.6, 0.4, 0.6, 0.7];
% X values for each bar
x = 1:N;
% Bar width
barWidth = 0.33;
% Plot bars with offsets
figure;
hold on;
b1 = bar(x - barWidth, bar1, barWidth, 'FaceColor', 'r');
b2 = bar(x, bar2, barWidth, 'FaceColor', 'g');
b3 = bar(x + barWidth, bar3, barWidth, 'FaceColor', 'b');
hold off;
% Adjust x-ticks to be in the middle of the grouped bars
xticks(x);
xticklabels({'1', '2', '3', '4', '5'}); % Replace with your actual labels
% Set x-axis limits to ensure all bars are visible
xlim([0.5, N + 0.5]);
% Add labels and title
xlabel('Group');
ylabel('Values');
title('Grouped Bar Plot');
Read more about 'xticks' with the link below:

Categorías

Más información sobre Errorbars en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by