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
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})
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)
Related
I have two cell arrays and each cell has a matrix x = (10X5). Each row in x is an array (between -1 and 1) with a mean "m" and std "s". Now i want to represent this matrix in a rectangular grid using MATLAB such that each box has a mean (orange color) and std deviation (filled with red colour in either side of mean) as shown in the example. So basically there should be 10X2 rectangular grids (corresponding to 10 rows and two cells). Can someone please help me with this? I looked up online but could not find anything.
You can use boxplot to create the initial structure of the plot, and then alter them to represent what you want. Each matrix x is converted to one grid plot, and the plots are placed side by side with subplot.
Here is a short code to do what you want:
A = {rand(10,5)*2-1,rand(10,5)*2-1}; % your cell array
for n = 1:numel(A)
subplot(1,2,n)
x = A{n};
means = mean(x,2);
stds = std(x,[],2);
% create boxplot for all variables:
bx = boxplot(x.','Orientation','horizontal');
% remove what's unnecessary:
delete(bx([1:4 7],:))
% set the median to mean:
set(bx(6,:),{'XData'},...
mat2cell([means means],ones(size(x,1),1),2))
set(bx(6,:),{'Color','LineWidth'},{[1 0.7 0],3})
% set the interQ range to std:
std_bounds = repmat(means,1,5)+bsxfun(#times,stds,[-1 1 1 -1 -1]);
set(bx(5,:),{'XData'},mat2cell(std_bounds,ones(size(x,1),1),5))
set(bx(5,:),'Color',[0.8 0 0])
for k = 1:size(std_bounds,1)
patch(std_bounds(k,:),get(bx(5,k),'YData'),[0.8 0 0],...
'FaceAlpha',0.7,...
'EdgeColor','none')
end
xlim([-1 1])
ax = gca;
ax.Children = ax.Children([end 1:end-1]);
% create the grid:
set(ax,{'YGrid','GridColor','GridAlpha','XTick','XAxisLocation','YTick'},...
{'on','k',1,[-1 0 1],'top',(1:size(x,1))+0.5})
% set the zero line:
line(ax,zeros(size(x,1)+2,1),(0:size(x,1)+1).','LineStyle','--','Color','k')
if n>1
set(ax,'YTickLabel',[])
end
end
It creates this:
I do have generated two 2-D dimensional matrices from my core process. Now, I want to represent them via the bar graphs.
I could manage to get the 3d bar graph for independant matrix as shown in figures attached.
My data matrices are
"xData" - size is : (52 x 46 )
"yData" - size is : (52 x 46)
They'll always have the same size.
Now, I want to represent them together in 'Grouped Style' As show in here. I got the 3D dimensional matrix by combining them (xData and yData) together i.e. generated 52 x 46 x 2 matrix and then tried to plot with bar3 command; but, I got an error and couldn't plot.
Do you guys have any idea on how to do it ?
I manage to do that using a loop and this SO answer:
% generate random data
sz = [5 4];
xData = rand(sz);
yData = rand(sz);
% plot zeros in the same size to prepare plot
Z = zeros(sz);
h0 = bar3(Z);
set(h0,'EdgeColor','none','FaceColor','none');
axis tight
% plot each data column in a loop
hold on
for kk = 1:size(xData,2)
h1 = bar3([xData(:,kk),yData(:,kk)],'grouped');
% move current bars to their x position
cellfun(#(x) set(h1,'XData', x + (kk - 1)), get(h1,'XData'));
end
I want to plot a confusion matrix in MATLAB. Here's my code;
data = rand(3, 3)
imagesc(data)
colormap(gray)
colorbar
When I run this, a confusion matrix with a color bar is shown. But usually, I have seen confusion matrix in MATLAB will give counts as well as probabilities. How can I get them? How can I change the class labels which will be shown as 1,2,3, etc.?
I want a matrix like this:
If you do not have the neural network toolbox, you can use plotConfMat. It gets you the following result.
I have also included an independent example below without the need for the function:
% sample data
confmat = magic(3);
labels = {'Dog', 'Cat', 'Horse'};
numlabels = size(confmat, 1); % number of labels
% calculate the percentage accuracies
confpercent = 100*confmat./repmat(sum(confmat, 1),numlabels,1);
% plotting the colors
imagesc(confpercent);
title('Confusion Matrix');
ylabel('Output Class'); xlabel('Target Class');
% set the colormap
colormap(flipud(gray));
% Create strings from the matrix values and remove spaces
textStrings = num2str([confpercent(:), confmat(:)], '%.1f%%\n%d\n');
textStrings = strtrim(cellstr(textStrings));
% Create x and y coordinates for the strings and plot them
[x,y] = meshgrid(1:numlabels);
hStrings = text(x(:),y(:),textStrings(:), ...
'HorizontalAlignment','center');
% Get the middle value of the color range
midValue = mean(get(gca,'CLim'));
% Choose white or black for the text color of the strings so
% they can be easily seen over the background color
textColors = repmat(confpercent(:) > midValue,1,3);
set(hStrings,{'Color'},num2cell(textColors,2));
% Setting the axis labels
set(gca,'XTick',1:numlabels,...
'XTickLabel',labels,...
'YTick',1:numlabels,...
'YTickLabel',labels,...
'TickLength',[0 0]);
If you have the neural network toolbox you can use the function plotconfusion. You can create a copy and edit it to customise it further, for example to print custom labels.
I populate a grid data=zeros(n,n); with 0's and 1's (could also be thought of as an adjacency grid, if you'd like). I just want to plot the grid with colors according to whether the value at that point is 0 or 1. For example,
scatter(1:n,1:n,data);
It gives me the error:
Error using scatter (line 77)
C must be a single color, a vector the same length as X, or an M-by-3 matrix.
Any suggestions?
you are telling matlab to plot only n points ((1,1), (2,2), ..., (n,n)) where you want actually the cartesian product (1:nX1:n).
Try
[X,Y] = meshgrid(1:n,1:n);
scatter(X(:), Y(:), 10, data(:));
scatter allows you to plot points with different options (color, size, etc) for each point depending on a 'Z' value, but it creates a lot of graphic objects (one for each point).
In your case, you only have 2 subsets of data (among all your points). The points with value 1 and with value 0. So another option is to extract these 2 subsets then plot each subset with each a set of common properties.
%% // prepare test data
n = 10 ;
data=randi([0 1],n); %// create a 10x10 matrix filled with `0` and `1`
%% // extract the 2 subsets
[x0 , y0] = find( data == 0 ) ;
[x1 , y1] = find( data == 1 ) ;
%% // display
figure ; axes('Nextplot','add')
plotOptions = {'LineStyle','none','MarkerEdgeColor','k','MarkerSize',10} ; %// common options for both plots
plot(x0,y0,'o','MarkerFaceColor','r', plotOptions{:} ) %// circle marker, red fill
plot(x1,y1,'d','MarkerFaceColor','g', plotOptions{:} ) %// diamond marker, green fill
This way you have full control on each subset property (you can control the size, color, shape etc...). And you only have 2 graphic objects to handle (instead of n^2).
I'm working on MATLAB, where I have a vector which I need to split into two classes and then get a histogram of both resulting vectors (which have different sizes). The values represent height records so the interval is about 140-185.
How can I get a normalized histogram of both resulting vectors in different colors. I was able to get both normalized vectors in the same colour (which is indistiguible) and and also a histogram with different colours but not not normalized...
I hope you understand my question and will be able to help me.
Thanks in advance :)
Maybe this is what you need:
matrix = [155+10*randn(2000,1) 165+10*randn(2000,1)];
matrix(1:1100,1) = NaN;
matrix(1101:2000,2) = NaN; %// example data
[y x] = hist(matrix, 15); %// 15 is desired number of bins
y = bsxfun(#rdivide, y, sum(y)) / (x(2)-x(1)); %// normalize to area 1
bar(x,y) %// plots each column of y vs x. Automatically uses different colors