colored dots on matlab plot - matlab

I want to make a PCA-plot, where the colour of each dot is given by a special number. The colour of the dot should be from blue (small number) to red (large number). I am trying to do this:
scatter(pc(1,:),pc(2,:),15,c,'filled')
c - is a 1x40 array, where the numbers for each dot are written. I get only differently coloured dots, but not from blue to red.
Could someone help me please?

Your c argument is a vector if it is 1x40. According to the help for the SCATTER function:
When C is a vector the same length as X and Y, the values in C are linearly mapped to the colors in the current colormap.
This means that the colors you see are dependent on your colormap.
If you need each marker to be a specific color, you can take advantage of this behavior:
When C is a length(X)-by-3 matrix, it directly specifies the colors of the markers as RGB values.

Related

I have a 2D plot in Matlab where I want to specify the RGB value for each point to vary to color

I am trying to plot a 2D line in Matlab with a color that varies based on an RGB code that I assign to each point. The code below works well for a given colormap ('col' values defining the color), but I am trying to keep tighter control over the color assignment, so that values always render the same color across multiple charts.
surface([x;x],[y;y],[z;z],[col;col],...
'facecol','no',...
'edgecol','interp',...
'linew',2);
The question is how do you decide which colour does each point have? defining the colormap is easy (shown below).
col=colormap(hot(128)) %% or other colour style like hsv or jet
if you have a variable (lets called it V) that for each point has a certain value and you want the colours to change based on that:
first define the values for the extremes in the colormap:
min_col=0; %%%can be the minimum of V
max_col=1; %%%can be the maximum of V
Then interpolate to your data
new_col=interp1(linspace(min_col,max_col,length(col)),col, V(:))

Matlab: Applying multiple colormaps to the same imagesc output

I have constructed a confusion matrix in Matlab and I wish to display it graphically.
Here is a good example of what I am trying to do: How do I visualize a matrix with colors and values displayed?
However, I wish to apply one colormap to the diagonal (i.e. white to green) and one to the off-diagonal (i.e. white to red).
How can I do this?

Is there a way to change the color scheme in different regions of the same plot in MatLab?

I've made a matlab plot that looks like this: http://i.imgur.com/8S4WzrA.jpg .
This is a plot of symmetric complex data that shows magnitude information on the upper triangle, phase information on the lower triangle, and a (mostly) constant coefficient along the diagonal.
My question is, can I change the color scheme for each section? For example: shades of red for the upper triangle, shades of blue for the lower, and grayscale for the diagonal? Also, would it be possible to have an individual colorbar for each of the sections?
Thanks!

Using different colors in Matlab

In a 3D scatter graph of MATLAB I have 15 different clusters of data that I want to highlight. I can see MATLAB has 8 specific colors. Is there any other way I could use 7 more colors just to distinguish the clusters?
Thanks
I would recommend to use this File Exchange submission - Generate maximally perceptually-distinct colors
It allows you to create a colormap with very distinguished colors and apply them with COLORMAP function. See help for this submission for more options.
colors = distinguishable_colors(n_colors);
For 3D scatter you can use this colors as an argument (C) in SCATTER3:
scatter3(X,Y,Z,[],colors)
To use this colors for different lines set them as default color order either for current figure:
set(gcf,'DefaultAxesColorOrder',colors)
or all figures:
set(0,'DefaultAxesColorOrder',colors
You can use the color property using set. You must first get a handle h to the drawing objects and set(h,'color',[0.2 0.3 0.9]). The color is rgb ranging from 0 to 1 for each channel.
From the Matlab documentation:
scatter(X,Y,S,C) displays colored circles at the locations specified
by the vectors X and Y (which must be the same size).
S determines the area of each marker (specified in points^2). S can be
a vector the same length as X and Y or a scalar. If S is a scalar,
MATLAB draws all the markers the same size. If S is empty, the default
size is used.
C determines the color of each marker. When C is a vector the same
length as X and Y, the values in C are linearly mapped to the colors
in the current colormap. When C is a 1-by-3 matrix, it specifies the
colors of the markers as RGB values. If you have 3 points in the
scatter plot and wish to have the colors be indices into the colormap,
C should be a 3-by-1 matrix. C can also be a color string (see
ColorSpec for a list of color string specifiers).
So, for example, say that your clusters are given by the columns of the matrices X and Y, with the k'th column being the k'th cluster, X being the X coordinates, and Y being the Y coordinates. We can do what you want as follows:
% make some random data in clusters:
n = 15;
m = 42;
X = 0.2*rand(m,n) + repmat(rand(1,n),m,1);
Y = 0.2*rand(m,n) + repmat(rand(1,n),m,1);
% lets change the colour map:
colormap(jet);
% now plot each, one at a time, and each with a different colour:
hold on;
for k=1:n
scatter(X(:,k),Y(:,k),40,k/n*ones(m,1));
end
If you don't like these colours, you can change the colormap, and if you don't like the color maps, you can, as the other answer points out, insert any RGB values you want.

How to plot 2D data with different colors and markers

Im faced with a problem where i need to plot a two dimensional data with different colors and markers.
We are given with 2 array, namely points (n x 2 dimension) and Label (n x 1 dimension). Im not sure about the number of unique values in the Label array but maximum could be 10. I would like to plot the points with different color and markers based on its corresponding Label value.
Can any one help me in this regard
Use gscatter, which does a scatter plot, using a group (Label in your case) to plot in different colours/makers.
GSCATTER(X,Y,G,CLR,SYM,SIZ) specifies the colors, markers, and
size to use. CLR is either a string of color specifications or
a three-column matrix of color specifications. SYM is a string
of marker specifications. Type "help plot" for more information.
For example, if SYM='o+x', the first group will be plotted with a
circle, the second with plus, and the third with x. SIZ is a
marker size to use for all plots. By default, the marker is '.'.
So you can specify colours like 'rgcmykwb' to do red for the first group, green for the second, etc or [] just to have Matlab sort it out.
By default Matlab uses the same marker for each group, so you need to specify what markers you want to be used for each group. If you do '.ox+*sdv^<>ph' you'll just cycle along all the markers that Matlab has.
n=50;
% make nx2 matrix of random points.
points = random('unif',0,1,n,2);
% make nx1 matrix of random labels from {1,2,...,5}
labels=round(random('unif',1,5,n,1));
% plot. Let Matlab sort out the colours and we will specify markers.
gscatter(points(:,1),points(:,2),labels,[],'ox+*sdv^<>ph.')
It looks a bit like this: