Matlab's bars and legends are not in the same direction

When building a matrix in the form of several horizontal stripes in matlab (using barh), Matlab puts the first column as the bottom panel and the last one at the top. However, the legend is the other way around, so the first element is the top one in the legend. I think it looks very confusing. See Attached Image

    data = [0.8000    0.1000    0.6000    0.4500
    0.3000    0.5000    0.7000    0.3500
    0.4000    0.4500    0.2000    0.5000];
    barh(data);
    legend('Column 1', 'Column 2', 'Column 3', 'Column 4');

How can I change the order of legends?

(Reordering the elements in the legend command only changes the lines, not the colors in the legend, so the order is still upside down and the labels are wrong.)

+4
source share
1 answer

You can do:

data = [0.8000    0.1000    0.6000    0.4500
        0.3000    0.5000    0.7000    0.3500
        0.4000    0.4500    0.2000    0.5000];
h = barh(data);
legend(fliplr(h),'Column 4', 'Column 3', 'Column 2', 'Column 1');

. .

0

All Articles