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

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(:))

Related

How to plot Tsne in matlab using specific color

I am trying to plot the result of Tsne using gscatter in Matlab.
I want to use specific color for training and other color for Anchors
Y=tsne(xtrain; xAnxhors]);
gscatter(Y(:,1),Y(:,2));
That is the code that I used but I got the figure in one color only so I want to show the diffrence of colors between xtrain and xAnchors
The function gscatter allows you to specify two more arguments. With the first one you can already define groups (a vector that indicates for each point what group it belongs to) which will automatically assign different colours to different groups. The second argument allows for an even more fine grained control about the actual choice of colours. For reference an example from the documentation:
Plot the Displacement values on the x-axis and the Horsepower values on the y-axis. gscatter uses the variable names as the default labels for the axes. Group the data points by Model_Year.
load carsmall
gscatter(Displacement,Horsepower,Model_Year)

Drawing black areas in matlab contourf plot

I am generating a series of plots using matlab contourf. I need to do the following with the resulting figure. From this state:
Make this:
Important note: I know the coordinates of pixels which should be blackened.
The easiest way is possible to use ind2rgb, do the "blackening" manually, then use imagesc and deal with the axes propeerties. But using this I will lose the contourf graphics (e.g. the contour lines).
Any better ideas?
You can manipulate the figure colormap by adding black color to the one you use.
M=colormap;
M=[0,0,0 ; M];
colormap(M)
Now assign to the "should be black" pixels a value smaller than the minimum. This will map this value to the minimum color which is now black.
To assign the value efficiently use subs2ind

How to give different colors when I loop for plot in MATLAB?

I have some data say X with size (100,2). This X is composed of data for 10 categories (set of 10). Now I would like to look the pattern in the data for each category. For this I need to have different colors assigned to each category. I am trying to loop instead of doing 10 different plots. I tried the below.
hold on
for i=1:10:100
plot(X(i:i+9,1),X(i:i+9,2),'.')
end
hold off
This gave me a plot with same color. How can I assign different colors for different range?
The answers mentioning hold all are correct and useful for cycling through the colors specified by the ColorOrder axes property (even though just hold on is now equivalent to hold all). However, by default MATLAB only specifies a short list of colors (just 7 as of R2013b) to cycle through, and on the other hand it can be problematic to find a good set of colors for more data series. For 10 plots, you obviously cannot rely on the default ColorOrder, so a great way to define N visually distinct colors is with the "Generate Maximally Perceptually-Distinct Colors" (GMPDC) submission on the MATLAB Central File File Exchange. It is best described in the author's own words:
This function generates a set of colors which are distinguishable by reference to the "Lab" color space, which more closely matches human color perception than RGB. Given an initial large list of possible colors, it iteratively chooses the entry in the list that is farthest (in Lab space) from all previously-chosen entries.
For example, here are the colors generated when 25 are requested:
The GMPDC submission was chosen on MathWorks' official blog as Pick of the Week a few years ago in part because of the ability to request an arbitrary number of colors (in contrast to MATLAB's built in 7 default colors). They even made the excellent suggestion to set MATLAB's ColorOrder on startup to,
distinguishable_colors(20)
Of course, you can set the ColorOrder for a single axis or simply generate a list of colors to use in any way you like. For example, to generate 10 "maximally perceptually-distinct colors" and use them for 10 plots on the same axis (not using ColorOrder):
% Starting with X of size 100x2
X = reshape(X,10,10,2); % for clarity, column is category, row is observation
mpdc10 = distinguishable_colors(10) % 10x3 color list
hold on
for ii=1:10,
plot(X(:,ii,1),X(:,ii,2),'.','Color',mpdc10(ii,:));
end
Alternatively, using the ColorOrder axis property simplifies the process:
X = reshape(X,10,10,2); % for clarity, and to avoid loop
mpdc10 = distinguishable_colors(10) % 10x3 color map
ha = axes; hold(ha,'on')
set(ha,'ColorOrder',mpdc10)
plot(X(:,:,1),X(:,:,2),'-.') % loop NOT needed, 'Color' NOT needed
APPENDIX
To get the ColorOrder RGB array used for the current axis,
get(gca,'ColorOrder')
To get the default ColorOrder for new axes,
get(0,'DefaultAxesColorOrder')
Example of setting new global ColorOrder with 10 colors on MATLAB start, in startup.m:
set(0,'DefaultAxesColorOrder',distinguishable_colors(10))
The easiest solution is to replace hold on by hold all.
If you want more control you have to manually define your line specifications (more info here) and then pass them to plot:
linespec = {'b.', 'r-', 'g--o'}; % define your ten linespecs in a cell array
hold on
for i=1:10:100
plot(X(i:i+9,1),X(i:i+9,2),linespec{i})
end
hold off
hold on makes sure the new plot command adds to the plot instead of replacing it. However, each command works as if it were generating a fresh plot, including starting with the first line color (blue). If you want subsequent plots use different colors, use hold all instead. That way the standard 7 line colors are used in turn.
Since you have 10 lines to plot, you might want to specify the colors explicitly to make sure they are all different. For that, use the syntax
plot(..., 'Color', [r g b])

Multiple Marker Types for one Function

I am using the plot3c function to map a matrix of data in the x,y,z, and color axes. For clarification, my z-data and color data are one and the same, but represented on the two axes. Due to instrumental limitations, my data has a set of false color values where an unreadable point is represented with a 0. I would like to have every z/color value of 0 represented with a different marker type than the rest of the data. I know how to change marker type for a plot but I do not know how to set a marker type for specific values within a plot. How can I do this?
You could just overlay a second plot that only plots where z is 0:
% Your original plot, I'm assuming plot3c(x,y,z,z)
hold on
mask = z==0;
plot3(x(mask), y(mask), z(mask), '^')
Or more efficiently as suggested in radarhead's comment:
mask = z==0;
plot3c(x(~mask), y(~mask), z(~mask), z(~mask))
hold on
plot3(x(mask), y(mask), z(mask), '^')

color triangles with exceptions to some

I am trying to draw a 2D mesh of triangles. I want to color each triangle according to a given array A of positive scalars, one per triangle; For instance assume the A holds the area of each triangle, and I want large triangles to be pinker than smaller ones. I know how to do this:
patch('Faces',tri,'Vertices',V,'FaceColor','flat',
'FaceVertexCData',A,...
'CDataMapping','scaled');
colormap(pink);
However, I also have another boolean array B, one boolean per triangle. For instance it marks which of the triangles are isosceles triangles.
In case a triangle is marked as 1 in B, I want to color it yellow.
I assume this can be achieved by something like A(B==1)=-1, changing the colormap and clever setting of caxis, but is this the most elegant way?
Do you want to skip coloring of some triangles (color them with one color), or use a different colormap for those triangles?
The first case is relatively simple for 2D plots. You can set A in those triangles to NaN, and patch will not draw them. Additionally, changing the background color of the plot will effectively show the non-drawn triangles in the color you want
A(B)=NaN;
set(gca,'Color',[1 0 0]); % red
If you want to use a different color, or a range of colors, you have to append the colormaps and set the values in A for the respective triangles to be 'larger enough' than A for any other triangles:
cmap = [colormap; [1 0 0]]; % red
colormap(cmap);
A(B) = max(A)+1;
No need to play with axis.
Otherwise, if you want to include two different color scales, you have to play with CData property of your patch plots, see a good tutorial here for drawing two plots, one in grayscale and one in color on the same figure.
There is also this post about how to plot two different data sets with two different colormaps. It is done by appending two colormaps and making sure that two datasets access distinct parts of the final colormap.