matlab colorbar for lineplot colored by value - matlab

I have 2d line plot in matlab where each line is colored according to a value. I would like to add a colorbar showing the color that corresponds to these values.
I got a solution to plot the lines according to the value I want, however I can not figure out to get the colorbar correctly. I have been searching on this but I am stuck.
Define an RGB color matrix COL.
(N x 3 low red to dark matrix corresponding to equally spaced values 0:1).
Sort the data according to their z value.
Interpolate the COL matrix to get values for all z values, giving the TRUECOL matrix for the lines.
Set the axiscolor-ordering to the TRUECOL matrix and plot the data.
minimalistic example:
% Generate 10 lines of 10 points
x = normrnd(0,1,10,10);
% The corresponding values are
% Note that these do not have to linearly spaced in real code
z = [0,0.05,0.1,0.11,0.12,0.2,0.4,0.45,0.8,0.9];
% Define colormatrix
COL = [0.996078431372549 0.878431372549020 0.823529411764706;...
0.937254901960784 0.231372549019608 0.172549019607843;...
0.403921568627451 0 0.0509803921568627];
% Interpolate the COL matrix to get colors for the data
TRUECOL = interp1(linspace(0,1,3),COL,z,'pchip');
% Set the axis coloring qnd plot the data
set(gcf,'DefaultAxesColorOrder',TRUECOL);
plot(x);
colormap(TRUECOL);
colorbar
I then change the colormap and plot the colobar, however the colors in the colorbar to not correspond to the values z. Is there a way of telling matlab which color corresponds to which value? Looking at the colorbar editor I see that CData must have something to do with it, but I cant find a way to specify that CData should be z.

My understanding is that you want the labels on the colorbar to go from 0 to 1, not 0 to 11. To fix this, use this caxis command. To get finer gradations of colors in the colorbar, you need to more finely interpolate the colormap. Try this:
colormap(interp1(linspace(0,1,size(COL,1)), COL, linspace(0,1,100)));
caxis([0,1]);
colorbar

Related

Is it possible to change the 'colormap' scale in Matlab?

I have 2 matrices. Matrix A contains values between 0 and 1 and matrix B contains values between 0 and 90. I would like to display an image with a different color for the numbers in each matrix.
When I use the colormap function with:
figure; colormap(jet); imshow(A);
The image displayed has several levels of gray, when I am supposed to have several colors (because I am using jet).
When I use the colormap function with:
figure; colormap(jet); imshow(B);
The image displayed is completely white, probably because my values are higher than 64 (which is the max of jet).
How can I solve these two problems? I read a lot of tutorials in several forums but I can't find the answer...
Thank you very much for answering my problem!
Just normalize the matrix by its max value if the values are more than 1. So for your B matrix try:
imshow(B/max(B(:)))
You can specify the colormap scaling and the number of actual colors within the colormap like so:
figure; imshow( A, [0 1], 'Colormap', jet(100) );
figure; imshow( B, [0 100], 'Colormap', jet(100) );
The jet(100) indicates 100 unique colors within the colormap to be used.
You are using the wrong function for the task in hand.
imshow expects an N by M by 3 array input, of the RGB channels of an image. When you use a 2D matrix the function assumes it's a grayscale image (it's like replicating it to 3 identical matrices to create these three channels - if all the channels in RGB have the same values you get grayscale colors). You can use this function together with a colormap to get a colored matrix, but there are much more convenient alternatives.
One simple function for getting a colored representation of a matrix is imagesc or (image if you want to scale the values by yourself). This function takes the values in your matrix, and assign them a color from the colormap you choose:
A = rand(10);
figure; colormap(jet); imagesc(A);
Another option is pcolor, which works a little different but give a similar result. pcolor attach the values to the vertices of the cells (in oppose to the center, as imagesc does), and interpolate the color in each cell from its' vertices. The resulted colored matrix is always smaller in one row and one column because it takes n+1 points (values in the original matrix) to define n gaps (the cells in the colored matrix). Here is an example:
A = rand(10);
figure; colormap(jet); pcolor(A);
shading flat

Plot multiple columns with different colors in MATLAB

I have a 372x15 matrix. I'm trying to graph this in such a way that columns 1-14 will be on the x-axis with different colors for each column, whereas the 15th column will be treated as the y-axis. For example, the plot with follow (x1, y), (x2, y) so on so forth, where x1 is all the data points in column 1. This is a simple scatterplot. How can I do this on MATLAB?
A simple way to do that is just use plot(A(:,1:end-1), A(:,end), '.'). Here's an example:
A = [(1:14)-.6*rand(372,14) ((1:372).'+rand(372,1))]; % example A. Uses implicit expansion
plot(A(:,1:end-1), A(:,end), '.') % do the plot
axis tight % optionally make axis limits tight
The above cycles through the 7 predefined colors. If you prefer to customize the colors, set the 'ColorOrder' property of the axes before calling plot, and use hold on to prevent Matlab from resetting it:
clf % clear figure
cmap = autumn(size(A,2)); % example colormap
set(gca, 'ColorOrder', cmap); % set that colormap
hold on % needed so that the colormap is not automatically reset
plot(A(:,1:end-1), A(:,end), '.')
axis tight
You can specify different markers or marker sizes; see plot's documentation.

Color the same line using two different colors

I have to plot a linear function and color it in a way that all negative values are red and the positive values are blue.
This is just an example of what I want to ask.
Generalizing, is it possible to color specific intervals of a function in a different color than that of the rest of the function, without having to do different plots?
No. Create a red coloured plot for the negative values, use hold on and then a blue coloured plot for the positive values.
x = [-10:0.1:10].'; %//range
x1 = x(x<=0); %//negative values and zero
x2 = x(x>=0); %//positive values and zero
figure; %//open figure
hold on %// plot things in the same figure
plot(x1,x1,'r') %//plot negative values
plot(x2,x2,'b') %//plot positive values

"Density" plots in Matlab, but not in the sense of density of data points

I would like to plot a sort of "density map" in Matlab, but have not found the right tool yet.
I have "continuous" data with x between (x_min and x_max), and y between (y_min and y_max). At each of these pairs of points (x_i,y_i), there is associated to it a value between 0 and 1.
I would like to plot this information in a 2d graph, such that in each small square containing (x_i,y_i) the plot shades the square black for the value 0, white for the value 1, and the appropriate shade of gray for intermediate values.
Can this be done easily in Matlab?
http://www.mathworks.com/help/images/ref/mat2gray.html seems to do exactly what I need.
If the data is in a matrix A, you can just use
image(255*A); colormap gray(256); axis image;
I'm not sure what you mean by continuous (uniformly spaced?), so my answer won't make too many assumptions other than that there is a reason why you mention the coordinates (if just a regular mesh, then just image or imagesc). So, only assuming your x and y coordinates are possibly non-uniformly spaced, but at least monotonically increasing samples, try surf with view(2):
surf(X,Y,data)
view(2)
colormap gray
By default surf sets the FaceColor property with the 'flat' option:
flat — The values of CData determine the color for each face of the surface. The color data at the first vertex determine the color of the entire face.
In other words, the value will determine the shade.
Assuming your data is in data and your x and y coordinates are in x and y, here is how to do it:
imagesc(x, y, data) % to create a heat map
colormap(gray) % for gray levels
caxis([0 1]) % to set 0 to black and 1 to white
axis xy % if you want the y axis to point up
colorbar % to display the colorbar

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.