user2011560
user2011560

Reputation: 75

Matlab, x-axis on barchart

I have two vectors: `

R = [-1 -1 1 1];
T = [2.0741 2.1521 2.9529 2];

And I'm trying to make a barchart. The bars has different colors depending on the value in R. The y-axis is the value in T and X-axis should be the index number from R ie. numbers from 1 to length(R) - But this is my problem, I' can't find a way to do so.

this is my code:

figure;

for i=1:length(T)
  h = bar(i, T(i));
    if i == 1, hold on, end
    if R(i) == -1
       col = 'r';
    else
    col = 'g';
    end
    set(h, 'FaceColor', col)

end

Upvotes: 0

Views: 72

Answers (1)

RTL
RTL

Reputation: 3587

There is nothing special about this being a bar chart compared to any other matlab axis except the values along the x axis are much simpler... the middle of each bar is an integer stating at 1 and counting up

so we want to put labels (Ticks) at 1,2,3 and 4 with labels of R (in order)

set(gca,'xTick',1:4,'xTickLabel',R)

I wrote this answer earlier today which contains more detail on the commands/ properties used.

Upvotes: 1

Related Questions