Kev1n91
Kev1n91

Reputation: 3683

Evenly distribute bar groups in a bar graph with a non-periodic X-Label in MATLAB

I have a Matrix I like to put in a bar chart. This works, however the x-axis is not periodic, it follows the following numbers:

1 2 3 4 5 6 7 8 9 10 12 14 16 18 20 22 24 26 28 30 35 40 45 50 55 60 70 80 90

These values are stored in the variable Batchsizes, the Matrix is stored in the variable valuable

I use the following code:

figure;
bar(Batchsizes,valuable);
set(gca,'Xtick',Batchsizes(1:length(Batchsizes)));

The following output is generated:

enter image description here

As you can see, the graph is crowded on the left and wide on the right. I would like to have the bar groups evenly distributed over the x-axis so that the graph is evenly spaced while preserving the old x-labels.

Any help is really appreciated.

Upvotes: 0

Views: 374

Answers (1)

Dev-iL
Dev-iL

Reputation: 24169

Remember the answer to your previous question? This is exactly the case when you do want to use XTickLabel (because now the ticks will be positioned at 1:1:29 but you want labels to have the values Batchsizes(1),Batchsizes(2),...). Here's one way to do it:

Batchsizes = [1:9, 10:2:28 30:5:55 60:10:90];
valuable = randi(35,numel(Batchsizes),3);
figure; bar(1:numel(Batchsizes),valuable);
set(gca,'XTick',1:numel(Batchsizes),...
        'XTickLabel',cellstr(num2str(Batchsizes.')));

The result is:

enter image description here

Upvotes: 1

Related Questions