0

I have two types of plots.

  1. In fist case I need to label individual bar graphs in the grouped bar plot. Here is the example
a=4.54,88.63,27.27,77.27,54.54;31.81,61.36,38.63,68.18,54.54;54.54,61.36,59.09,54.54,50;68.18,27.27,56.81,34.09,50;90.90,11.36,68.18,15.90,40.90];
b=0.40,0.55,0.70,0.85,1;1.39,1.54,1.69,1.84,1.99;2.340,2.49,2.64,2.79,2.94;3.36,3.51,3.66,3.81,3.96;4.29,4.44,4.59,4.74,4.89];
figure,
hold on
for i=1:5
  
bar(b(i,:),a(:,i))
end
figure,
hold on
for i=1:3
    plot(b(i,:),a(:,i))
end

For the bar plot, I like to lable on the horizontal as shown in figure, the numbers are stored in an other matrix, say b 2) Similarly, I also want to XTickLables using values in b for the line plot. enter image description here

Kanjoo
  • 111
  • 1
  • 8
  • 2
    Possible duplicate 1: https://stackoverflow.com/questions/16091164/getting-a-second-row-of-xlabels-in-matlab-graph. Possible duplicate 2: https://stackoverflow.com/questions/43715593/add-data-label-to-a-grouped-bar-chart-in-matlab – Wolfie Jul 20 '22 at 14:32

1 Answers1

0

you have first to define xticks and XTickLables

for example you can write:

a=[....];
b=[....];
c=[0.5,1,1.5,2,3];
D = {'0.5','1','1.5','2','3'};

figure,
hold on
for i=1:5
          
bar(b(i,:),a(:,i));

xticks(i) = c(i); %% first you define the ticks

XTickLables{i} = d(i); %% second you define the labels
         
end

for the exact same plot like the one you showed, I think you need two nested for loops.

more on that here: https://de.mathworks.com/help/matlab/ref/xticklabels.html

Adnan j
  • 109
  • 10
  • Hello, I cant use XTickLables{i} here secondly, it does not work in the loop like that. Can you please post a proper example. I am trying to do this however it is still not working – Kanjoo Jul 21 '22 at 20:29
  • well, you can also use ax = gca; ax.XTickLable(i) =d(i); or assign it later after the loop ax.XTickLable= ['array element1', 'array element2', ..... ] – Adnan j Jul 22 '22 at 10:53