How to generate 3d scatter plot with different colours for labels in MATLAB? - matlab

I have used TSNE in MATLAB for dimensionality reduction of a large data. I have been able to generate the Scatter Plot for TSNE in 2 dimensions which shows the labels of the cluster in different colors for each label, but, I am unable to do so in 3D. Referring to https://uk.mathworks.com/help/stats/tsne.html, I used the following syntax:-
Where, merged_data_all is a 21392x1974 table with the last column named FunctionalGroup containing different labels (similar to the Fisheriris species labels in the Mathworks example on tsne). Y2 is the 3 dimensional variable which I have been successfully able to generate of dimensions 21392 x 3 double.
figure
v = double(categorical(merged_data_all.FunctionalGroup));
c = full(sparse(1:numel(v),v,ones(size(v)),numel(v),3));
scatter3(Y2(:,1),Y2(:,2),Y2(:,3),15,c,'filled')
title('3-D Embedding')
view(-50,8)
When I use this code, I get the error "Error using sparse- Index exceeds array bounds". I even tried to use a modified version of the code and doing something like this
scatter3(Y(:,1), Y(:,2),Y(:,3),merged_data_all.FunctionalGroup)
In case of this, I get the error "Error using scatter3- Input arguments must be numeric, datetime or categorical". I am quite confused as to how I can plot a 3d scatter plot, with 14 different colors (for the 14 types of different labels I have in my FunctionalGroup column of merged_data_all). Any help in this regard would be highly appreciated. Thanks

Related

How do I correctly plot the clusters produced from a cluster analysis in matlab?

I want to carry out hierarchical clustering in Matlab and plot the clusters on a scatterplot. I have used the evalclusters function to first investigate what a 'good' number of clusters would be using different criteria values eg Silhouette, CalinskiHarabasz. Here is the code I used for the evaluation (x is my data with 200 observations and 10 variables):
E = evalclusters(x,'linkage','CalinskiHarabasz','KList',[1:10])
%store kmean optimal clusters
optk=E.OptimalK;
%save the outouts to a structure
clust_struc(1).Optimalk=optk;
clust_struc(1).method={'CalinskiHarabasz'}
I then used code similar to what I have found online:
gscatter(x(:,1),x(:,2),E.OptimalY,'rbgckmr','xod*s.p')
%OptimalY is a vector 200 long with the cluster numbers
and this is what I get:
My question may be silly, but I don't understand why I am only using the first two columns of data to produce the scatter plot? I realise that the clusters themselves are being incorporated through the use of the Optimal Y, but should I not be using all of the data in x?
Each row in x is an observation with properties in size(x,2) dimensions. All this dimensions are used for clustering x rows.
However, when plotting the clusters, we cannot plot more than 2-3 dimensions so we try to represent each element with its key properties. I'm not sure that x(:,1),x(:,2) are the best option, but you have to choose 2 for a 2-D plot.
Usually you would have some property of interest that you want to plot. Have a look at the example in MATLAB doc: the fisheriris data has 4 different variables - the length and width measurements from the sepals and petals of three species of iris flowers. It is up to you to decide which you want to plot against each other (in the example they choosed Petal Length and Petal Width).
Here is a comparison between taking Petals measurements and Sepals measurements as the axis for plotting the grouping:

matlab scatter3 plot real and imaginary parts over frequency

I've got to vectors called ttre and ttim which contain real and imaginary data over a frequency (from 1 to 64). The fields are looking like this:
ttim 64x10100 single
ttre 64x10100 single
I can easily make a 2D scatter plot of a certain row by using the command
scatter(ttim(40,:),ttre(40,:))
Now, I would like to display all data in a 3D scatter plot where X=real values, Y=imaginary values and Z=[1...64]
I created an array for Z with the number 1 to 64 and copied it to make it the same size as the other variables, by:
z=(1:64)'
z=repmat(z,1,10100)
result:
z 64x10100 double
When I try to plo a 3D scatter plot now, I get the error "Vectors x,yu,z must be of the same size"...however, as far as I understand, they are of the same size.
>> scatter3(ttim,ttre,z)
Error using scatter3 (line 64)
X, Y and Z must be vectors of the same length.
I hope that someone could point me into the right direction here.
Kind regards
scatter3 needs points to plot, so x,yand z should be 1xN , where N is the amount of points your are plotting. I dont know what your data is, so unfortunately I can not help more. Maybe scatter3(ttim(:),ttre(:),z(:)) works, but I do not recommend it for the huge amount of data you have, it may crash your computer.
However, maybe z=1:64 is not the best option. It means that you will have 64 layers (like floors from a building) of scattered data, not sure if that's what you want.

Plotting 3 vectors in Matlab GUI axes handle

I am trying to plot 3 vectors onto matlab GUI in a serial object's callback.
I want to plot this on axes handle but the problem is it only plot last vector;
plot(handles.axes1,sensor1,'r');
plot(handles.axes1,sensor2,'b');
plot(handles.axes1,sensor3,'g');
I searched on internet and find that this issue can be solved with hold on and hold of feature so I tried this
plot(handles.axes1,sensor1,'r');
hold on ;
plot(handles.axes1,sensor2,'b');
plot(handles.axes1,sensor3,'g');
hold off;
but in this case a new figure is opened(dont know why) and again only the last plot is drawn.
I am stucked. If any one have idea of what would be the issue?
Thanks
I'm not sure why your first try using "hold" didn't work. Seems like it should have.
But in any case, you can get the desired behavior in a single command:
plot(handles.axes1,length(sensor1),sensor1,'r',...
length(sensor2),sensor2,'b',...
length(sensor3),sensor3,'g');
This specifies both an X = length(sensor_) and a Y = sensor_ to the plot command. When you only give plot a Y input, it assumes an X of length(Y). But you can't combine multiple traces in a single plot command by giving only the Y input for each, because it will try to treat the inputs as X,Y pairs.
As the vectors are the same length we can simply combine them as the columns of a matrix and then plot the matrix
plot(handles.axes1,[sensor1',sensor2',sensor3'])
However these will have the default colour order. Without specifying x values setting colors within the plot command is tricky. However (luckily) the default order starts:
blue,green,red...
so swapping the column order will plot the lines with the colours requested
plot(handles.axes1,[sensor2',sensor3',sensor1'])
(this assumes the vectors are rows, if they are columns don't transpose them)

MATLAB: plotting multiple columns of a matrix

Inside a MATLAB function I have built a matrix A, whose dimensions M and N are set as parameters of the function. I would like to plot all the columns of this matrix, given a vector of indices B with length M. Hence, I use these lines:
figure
plot(B,A)
I specified figure as the MATLAB function returns more different plots.
My problem is that the program plots just two columns of the matrix with different colours (blue and violet). Where is my mistake?
Thank you for your attention.
go for
plot(repmat(B,1,N),A);
or
plot(repmat(B,N,1),A);
(depending on your rows/columns). You need to have same size matrices in plot.
Moreover, if B are just consecutive indexes, you may want to consider Plot(A) (or Plot(A')).
I noticed that there was an error which caused the overlap of the different curves, so the way which I used to plot the colums of a matrix is valid. However, the method proposed by Acorbe is a possibility, too.

Handles for individual matrix columns/rows/elements -- Matlab

I'm currently using Matlab and I am plotting the contents of the rows of a matrix, where each column is an independent data set. As the matrix is large I don't want to have to go through the tedius task of writing up the plot labels for each data set individually, so I was wondering if there is a specific way to include a handle/name for each column in such a way that it will automatically apply the plot label, and will adjust accordingly if columns are added or removed from the matrix?
Thanks!
Specifics, if they help:
Amplified spontaneous emission (ASE) in an optical fibre amplifier. Rows act as storage for a discretised ASE spectrum, columns are a given position along the fibre amplifier (it is this position -- the distance along the fibre corresponding to the column -- which I want to use as the label) and each element contains power information. The plot gives spectral power of ASE in the fibre for different positions along its length.
If by labels you mean the plot legend, you can do that by using cells. Consider matrix A
A = repmat([1:3], 3, 1)
A =
1 2 3
1 2 3
1 2 3
You can call plot to plot the columns of the matrix
plot(A);
Here, you will get 3 horizontal lines at y=1, 2 and 3. You can create your legend as follows
l{1} = 'dataset1';
l{2} = 'dataset2';
l{3} = 'dataset3';
Then you type
legend(l)
to show the legend. However, no one will create the legend for you, so you must create the cell array yourself. You can do it automatically, of course, e.g. the above legend can be created by a simple loop
for i=1:size(A, 2)
l{i} = ['dataset' num2str(i)];
end