Displaying multiple figures with differents colors - matlab

I have a matlab code that generates 3D points , so I want to plot each 3D point with different colors.
My last code for generat all points are
figure(i),plot3(mx,my,mz,'r*');
and this one plot all peaks but with same color which are red.

instead of figure(i),plot3(mx,my,mz,'r*'); you can plot each datapoint separately and assign a different color using the property 'Color' of the plot3.
such an example would be:
figure(i),hold on
for j=1:length(mx)
plot3(mx(j),my(j),mz(j),'Color',rand(1,3));
end
hold off
The way each point is coloured is up to you just change the rand to something that makes sense.

What about using e.g. hsv:
M = length(mx);
cols = hsv(M); % specify M colors by hsv
figure(i);
hold on;
for pIdx = 1:M
plot3(mx(pIdx),my(pIdx),mz(pIdx),'Color',cols(pIdx,:));
end

Related

How to color multiple lines based on their value?

I produced a plot that contains 50 curves and each of them corresponds to a specific value of a parameter called "Jacobi constant", so I have 50 values of jacobi constant stored in array called jacobi_cst_L1:
3.000900891023230
3.000894276927840
3.000887643313580
3.000881028967010
3.000874419173230
3.000867791975870
3.000861196034850
3.000854592397690
3.000847948043080
3.000841330136040
3.000834723697250
3.000828099771820
3.000821489088600
3.000814922863360
3.000808265737810
3.000801695858850
3.000795067776960
3.000788475204760
3.000781845363950
3.000775192199620
3.000768609354090
3.000761928862980
3.000755335851910
3.000748750854930
3.000742084743060
3.000735532899990
3.000728906460450
3.000722309400740
3.000715644446600
3.000709016645110
3.000702431180730
3.000695791284050
3.000689196186970
3.000682547292110
3.000675958537960
3.000669315388860
3.000662738391370
3.000656116141060
3.000649560630930
3.000642857256680
3.000636330415510
3.000629657944820
3.000623060310100
3.000616425935580
3.000609870077710
3.000603171772120
3.000596554947660
3.000590018845460
3.000583342259840
3.000576748353570
I want to use a colormap to color my curves and then show in a lateral bar the legend that show the numerical values corresponding to each color of orbit.
By considering my example image, I would want to add the array of constants in the lateral bar and then to color each curve according the lateral bar.
% Family of 50 planar Lyapunov orbits around L1 in dimensionless unit
fig = figure;
for k1 = 1:(numel(files_L1_L2_Ly_prop)-2)
plot([Ly_orb_filt(1).prop(k1).orbits.x],[Ly_orb_filt(1).prop(k1).orbits.y],...
"Color",my_green*1.1); hold on %"Color",my_green*1.1
colorbar()
end
axis equal
% Plot L1 point
plot(Ly_orb_filt_sys_data(1).x,Ly_orb_filt_sys_data(1).y,'.',...
'color',[0,0,0],'MarkerFaceColor',my_green,'MarkerSize',10);
text(Ly_orb_filt_sys_data(1).x-0.00015,Ly_orb_filt_sys_data(1).y-0.0008,'L_{1}');
%Primary bodies plots
plot(AstroData.mu_SEM_sys -1,0,'.',...
'color',my_blue,'MarkerFaceColor',my_blue,'MarkerSize',20);
text(AstroData.mu_SEM_sys-1,0-0.001,'$Earth + Moon$','Interpreter',"latex");
grid on;
xlabel('$x$','interpreter','latex','fontsize',12);
ylabel('$y$','interpreter','latex','FontSize',12);
How can I color each line based on its Jacobi constant value?
You can use any colour map to produce a series of RGB-triplets for the plotting routines to read (Or create an m-by-3 matrix with elements between 0 and 1 yourself):
n = 10; % Plot 10 lines
x = 1:15;
colour_map = jet(n); % Get colours. parula, hsv, hot etc.
figure;
hold on
for ii = 1:n
% Plot each line individually
plot(x, x+ii, 'Color', colour_map(ii, :))
end
colorbar % Show the colour bar.
Which on R2007b produces:
Note that indexing into a colour map will produce linearly spaced colours, thus you'll need to either interpolate or calculate a lot to get the specific ones you need. Then you can (need to?) modify the resulting colour bar's labels by hand to reflect your input values. I'd simply use parula(50), treat its indices as linspace(jacobi(1), jacobi(end), 50) and then my_colour = interp1(linspace(jacobi(1), jacobi(end), 50), parula(50), jacobi).
So in your code, rather than using "Color",my_green*1.1 for each line, use "Color",my_colour(kl,:), where my_colour is whatever series of RGB triplets you have defined.

How to mark a point (I have x,y,z for it) in a 3D big data scatter plot with different color using matlab?

I have two 3D volume images and I want to locate one point from the first image (I have specific x, y, and z values for this point) and mark it with a different color. I mean how I can insert the values of x, y, and z and get this point in my graph inside all the points with a different color.
Here are 2 options:
Option 1
Use hold to overlay another scatter only with the points you want to color differently:
data = rand(100,3); % some data
p = randi(100); % choose some point
scatter3(data(:,1),data(:,2),data(:,3),'Fill')
hold on
% here you plot only one point (p):
scatter3(data(p,1),data(p,2),data(p,3),'r','Fill')
hold off
Option 2
If you want to color more than one point, and/or use different colors for your points, it may be better to set the color by the point when you call scatter in the first time:
data = rand(100,3); % some data
p = randi(size(data,1),5,1); % choose some points
c = ones(size(data,1),1); % default color
c(p) = 2:(numel(p)+1); % set different color for each points in p
col = lines(numel(p)+1); % set the colormap for the points
scatter3(data(:,1),data(:,2),data(:,3),[],col(c,:),'Fill')
Assuming you're using scatter3, you can just make your scatterplot, then use "hold on" and add a scatterplot with your single point in a different color that will cover the original point, e.g.:
hold on;
scatter3(x,y,z,'MarkerEdgeColor','k','MarkerFaceColor',[0 .75 .75]);

How to make 2D scatter plot in Matlab with colors depending on values?

I have three vectors of the same lenght: x, y, and cls. I want to make a 2D plot of x and y but each point should have a color corresponding to the value of cls.
I thought about using the scatter function but you can chage the color of the whole plot, not of particular elements. Any ideas?
I would like to get something like in this example, when cls has elements of three values:
From the help of scatter:
scatter(x,y,a,c) specifies the circle colors. To plot all circles with the same color, specify c as a single color string or an RGB triplet. To use varying color, specify c as a vector or a three-column matrix of RGB triplets.
you can construct c as
c=zeros(size(x),3);
c(cls==1,:)=[1 0 0]; % 1 is red
% ...
scatter(x,y,1,c)
However, I dont know how to do the background. Did you apply some Machine learning algorithm to clasify the data? maybe you can get the equations to plot the background from there, but it depends on the method.
If you have the Statistics Toolbox, there is an easy way of doing this, it's called gscatter.
It takes similar inputs to scatter, but the third input is the group:
gscatter(x,y,cls)
You can add colours and markers - this plots with red, then green, then blue (order determined by the contents of cls, all markers circles.
gscatter(x,y,cls,'rgb','o')
Here's another solution splitting your data in three using logical indexing:
% Some random data
x = rand(100,1);
y = rand(100,1);
cls = round(2*rand(100,1));
% Split the data in three groups depending on the value in cls
x_red = x(cls==0);
y_red = y(cls==0);
x_green = x(cls==1);
y_green = y(cls==1);
x_blue = x(cls==2);
y_blue = y(cls==2);
% plot the data
scatter(x_red,y_red,1,'r')
hold on
scatter(x_green,y_green,1,'g')
scatter(x_blue,y_blue,1,'b')
hold off
One very simple solution with c being the color vector:
scatter3(X,Y,zeros(size(X,1)),4,c);
view(0,90);

How to mark points in a plot that are over a specified value in Matlab?

Say I have a data set with x values of longitude and Y values of 1 to 100. How can I plot the whole data set and represent all Y values over 90 with a different symbol?
Thanks for the help!
The easiest way would be to plot the sets separately, and specify a different symbol for each set i.e.
plot(x(Y<=90),Y(Y<=90),'bx',x(Y>90),Y(Y>90),'bo');
You could also do different colors. The scatter function has the ability to specify a different color for each point with the syntax scatter(x,y,s,c). For your example, you could do:
% make data
rng(0,'twister'); theta = linspace(0,2*pi,150);
x = sin(theta) + 0.75*rand(1,150); x = x*100;
y = cos(theta) + 0.75*rand(1,150); y = y*100;
mask = y>90;
% plot with custom colors for each point
c = zeros(numel(x),3); % matrix of RGB colorspecs
c(mask,:) = repmat([1 0 0],nnz(mask),1); % red
c(~mask,:) = repmat([0 0 1],nnz(~mask),1); % blue
scatter(x,y,10,c,'+');
Or instead of and RGB colorspec matrix, you can index into the current colormap. This allows you to get a nice smooth variation with some value:
scatter(x,y,10,y+x,'o') % x+y is mapped to indexes into default colormap, jet(64)
You can combine this color mapping with the approach of separating the data into two sets to also get different markers. Split the data, plot the first set with scatter as above, hold on, and plot the second set with a different marker. For example,
cv = x+y; % or just y, but this is an interesting example
scatter(x(mask),y(mask),10,cv(mask),'+');
hold on
scatter(x(~mask),y(~mask),10,cv(~mask),'o');
The result is different marker styles, where '+' is used where y>90 and '+' elsewhere, and different colors, where color is determined by mapping the values of cv=x+y onto the current colormap. The idea here is to look at 2 different modes of variations, but you could just use cv=y.

How to show color gradient on scatter plot in matlab?

I have a scatter plot that will overlay several sets of data. Each set of data currently displays as the next color in the default colormap. This is my code right now:
figure
hold on
for i=1:10
scatter(RunRawArea(i,:), RunRawNetLength(i,:));
end
hold off
What i would like is to color code each set of data (indexed by i) to be the next color on a gradient. For example, the data for i=1 would be blue, i=5 would be purple, and i=10 would be red.
How could I do this?
You should add another parameter to scatter - called CData
https://www.mathworks.com/help/matlab/ref/scatter.html
Description: scatter(x,y) creates a scatter plot with circles
at the locations specified by the vectors x and y. This type of graph
is also known as a bubble plot.
In your example:
figure
hold on
colorVec = linspace(1,0, size(RunRawNetLength,1));
colorVec = transpose(colorVec);
colorVec = repmat(colorVec,[1 3]);
for i=1:10
scatter(RunRawArea(i,:), RunRawNetLength(i,:),'CData', colorVec );
end
hold off
Method 1:
You can vectorize your data, so you don't need a loop and than add a color by series:
% specify your color map:
colorCode = lines(size(RunRawNetLength,1)); % or any other colormap...
% define the correct color for each series:
coloVev = repmat(colorCode,size(RunRawNetLength,1),1);
% plot it all at once without a loop:
scatter(RunRawArea(:),RunRawNetLength(:),[],coloVev)
Method 2:
If you have the Statistics and Machine Learning Toolbox you can also do this with gscatter:
% define the series of data:
group = repmat(1:size(RunRawNetLength,1),1,size(RunRawNetLength,1));
% plot it all at once without a loop:
gscatter(RunRawArea(:),RunRawNetLength(:),group,colorCode);
if your colormap has not enough colors for all the series, then the function loops over and start from the first one.
Result:
In both cases the result for some random data will be (the main difference is that gscatter fill the data points and add the legend by default):
Note that I used the colormap lines which has only 7 entries, so in this example with 10 series it repeats itself.