Tae-Sung Shin
Tae-Sung Shin

Reputation: 20620

X-labels in line plot of matrlx

In Matlab, when I want to plot each row of a n x m matrix A as a line, I do

plot(A');

One problem for me is the x-labels which are indices from 1 to number of variables.

I want to change those labels to more meaningful values from, say, a vector B.

So I tried following statement

plot(repmat(B,1,size(A,1)),A');

but the chart looks totally different. I know I can use 'XTickLabel' but it does not work with line plot of matrix, meaning no effect of 'XTickLabel'. Any idea how I can put labels correctly?

Upvotes: 0

Views: 127

Answers (2)

macduff
macduff

Reputation: 4685

You could try this as well

x = 0:0.1:1;
A = [ x.*x ; exp(-x) ]
plot( x, A' )

XLabels

Upvotes: 1

Roney Michael
Roney Michael

Reputation: 3994

You could use something along the lines of:

>> 
A = [
1 2 3 4
5 6 7 8
9 8 7 6
5 4 3 2
    ];
>> 
B = [
15 30 45 60
    ];
>> plot(A')
>> set(gca, 'XTick', 1:numel(B))
>> set(gca, 'XTickLabel', cellstr(num2str(B'))')

This would give you:

enter image description here

Upvotes: 2

Related Questions