mad
mad

Reputation: 2789

Plot vectors with labels in matlab

I have a Nx62 matrix with N 62-D vectors and a NX1 vector with the labels for the vectors. I am trying to plot these vectors with their labels because I want to see the behavior of these classes when plotted in a 62-dimensional space. The vectors belong to three classes according to the labels of a NX1 vector cited before.

How to to that in matlab? when i do plot(vector,classes) the result is very weird to analyse, how to put labels in the graph?

The code i am using to get the labels, vectors and plotting is the following:

%labels is a vector with labels, vectors is a matrix where each line is a vector
[labels,vectors]=libsvmread('features-im1.txt');

when I plot a three dimensional vector is simple

a=[1,2,3]
plot(a)

and then I get the result

Simple plot

but now i have a set of vectors and a set of labels, and i want to see the distribution of them, i want to plot each of these labels but also want to identify their classes. How to do that in matlab?

EDIT: This code is almost working. The problem is the fact that for each vector and class the plot will assign a color. I just want three colors and three labels, one per class.

[class,vector]=libsvmread('features-im1.txt'); 
%the plot doesn't allow negative and 0 values in the label 
class=class+2; 
labels = {'class -1','class 0','class 1'}; 
h = plot(vector); 
legend(h,labels{class})   

Upvotes: 0

Views: 1109

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112679

If I understand correctly, this does what you want:

N = 5;
classes = [1 2 3 1 2]; % class of each vector. Size N x 1
colors = {'r', 'g', 'b'}; % you can also define them numerically
matrix = rand(N,62); % example data. Size N x 62
labels = {'class 1','class 2','class 3'}; % class names. Size max(classes) x 1
h = plot(matrix.');
h_first = NaN(1,3); % initialization
for k = 1:max(classes)
    ind = find(classes==k);
    set(h(ind), 'color', colors{k}) % setting color to all plots of a given class
    h_first(k) = h(ind(1)); % remember a handle of each color (for legend)
end
legend(h_first,labels)

Upvotes: 1

Related Questions