Question

What I expect below code to output is 4 different bars in a bar plot each with different colors, and with tick labels 'a', 'b', 'c', and 'd' respectively. Colors are fine, but only the first bar has the label 'a', the other three have no labels. How can I achieve tick labeling bars in the bar plot while highlighting them with different colors? I am using version 2010b. Thanks!

deneme = [1 2 3 4];
figure;
for i=1:length(deneme)
    if i==1
        colorcode = 'b';
    elseif i==2
        colorcode = 'g';
    elseif i==3
        colorcode = 'r';
    else
        colorcode = 'k';
    end
    bar(i, deneme(i), colorcode);
    hold on;
end
set(gca,'XTickLabel',{'a'; 'b'; 'c'; 'd'})
Was it helpful?

Solution

Just add this line before your last line:

set(gca,'Xtick',1:4)

or combine both lines into

set(gca,'Xtick',1:4,'XTickLabel',{'a'; 'b'; 'c'; 'd'})

Somehow, in your code as it stands there is only one tick, so all labels but the first have no tick to land in. Creating those ticks with set(gca,'xtick',...) solves the issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top