how can i combine these 3 matlab graphs to a single one? - matlab

hello i have these three polar plots:
polar(a,(V1).^(-1),'-r')
polar(a,(V2).^(-1),'-g')
polar(a,(V3).^(-1),'-m')
where V1,V2,V3 are matrices , how can i combine these 3 graphs to a single one (not like subplot , more like copyobj)?
Note: The axes remain the same in every graph.
Thank you

You can use hold on as you would for any other types of graphs.
Example:
clear
clc
close all
figure;
theta1 = 0:0.01:2*pi;
rho1 = sin(2*theta1).*cos(2*theta1);
theta2 = 0:0.1:2*pi;
rho2 = cos(2*theta2).*cos(2*theta2);
theta3 = 0:0.1:2*pi;
rho3 = cos(2*theta2).*tan(2*theta2);
polar(theta1,rho1,'--r')
hold on
polar(theta2,rho2,'-b')
polar(theta3,rho3,'-g')
output:

Use hold on. It will "hold" every plot you make and put the others on top. If you want to stop holdin, then hold off.

thanks guys, solved it with:
figure;
b=polar(a,(V3).^(-1),'-m')
hold on
c=polar(a,(V2).^(-1),'-g')
hold on
a=polar(a,(V1).^(-1),'-r')
hold off;
for some reason when i change the series that i write the polars (for example a,b,c) the method doesn't work... But now it's ok, so never mind :)

Related

Colormap 2D for a scalar - Matlab

i'm simulating a wave propogation in time and place. i want to make a colormap of its values for every time step, in space. i mean, i want to make a figure of 2 axes (x and y) and displays the wave's values at those points by color (the wave varible is V).
how can i do it?
i'v tried:
for ind1 = 1:length(t)
figure()
trisurf(x1,y1,V(:,ind1),'EdgeColor', 'None', 'facecolor', 'interp');
view(2);
end
but i got a message that z (=V) suppose to be a function and not a scalar.
any suggestions?
I have two options, I don't think they will be perfect, but it might help.
First, interpolate the data onto a rectangular mesh and use contourf:
F=scatteredInterp(x,y,V(:,ind1));
X=linspace(min(x),max(x));
Y=linspace(min(y),max(y));
contourf(X,Y,F(X,Y))
Secondly, use scatter to plot points with varying colour:
scatter(x,y,25,V(:,ind1))
where the 25 controls the size of each marker, you may have to experiment with it.
Hope that gives you some ideas.
i've made a loop that finally works:
clear heart_movie
Vnorm = mat2gray(V(:,1:2000));
x1_new = x1-min(x1)+1;
y1_new = y1-min(y1)+1;
for ind1 = 1:2000
heart = zeros(max(x1_new),max(y1_new));
z = Vnorm(:,ind1);
for ind2 = 1:length(z);
heart(y1_new(ind2),x1_new(ind2))= z(ind2);
end
colormap(jet);
imagesc(flipud(heart));
end

How to overlay histograms in matlab

I have multiple histograms that I would like to overlay on top of each other but I don't know how to do it. I found the code below but I don't know how to modify it to run on a loop instead of just two histograms.
data1 = randn(100,1); % data of one size
data2 = randn(25, 1); % data of another size!
myBins = linspace(-3,3,10); % pick my own bin locations
% Hists will be the same size because we set the bin locations:
y1 = hist(data1, myBins);
y2 = hist(data2, myBins);
% plot the results:
figure(3);
bar(myBins, [y1;y2]');
title('Mixed size result');
or what is a better way of comparing histograms if they are more than 10 or 20.
Your question is very general. Firstly I do not understand why you insist on a for loop.
Personally I don't like the included bar plot. It quickly gets messy (especially since the bars are not at the "original" location)
If you got a lot of histograms I would consider a stairstep plot as it doesn't fill the plot area so much. Or you could come up with your own - eg using transparent patches.
If it get's lots of curves there are many ways to visualize them google for "multivariate visualization" and be amazed. One of the most amusing ways would be Chernoff faces.
it's much easier now:
histogram(data1, myBins);
hold on;
histogram(data2, myBins);
You could do the following, although it's not the only way:
data = cell(1, N);
y = cell(1, N);
yBar = zeros(N, 10);
for i=1:N
data{1, i} = randn(10*round(rand(1,1)), 1);
y{1, i} = hist(data{1, i}, myBins);
yBar(i, :) = y{1, i};
end
yBar = yBar';
figure(3);
bar(myBins, yBar);
title('Mixed size result');
Using the y cell is not obligatory of course, I left it there to actually show what's happening.
I would suggest this. It's simple and does not require for loops:
bar([y1.' y2.'],'stacked')
Here's a way that was useful to me:
I'm plotting a histogram for each column of the matrix ao.
The code was:
for i = 1:size(ao,2)
[h, y] = hist(ao(:,i), linspace(-5,10,100));
h = i + (0.95./max(h(:))) .* h;
barh(y, h, 'BarWidth', 1, 'BaseValue', i, 'LineStyle', 'none');
hold on;
end
grid;
Note that just changing barh to bar will give the same thing but going up-down instead of left-right (i.e. the figure rotated by 90° anti-clockwise).

Append figure with new plots

I want to visualize two sets of data in different figures. Here is how do I realize it now:
f1 = figure;
for i=0:6
plot(stim(i)+i);
hold on;
end;
f2 = figure;
for i=0:6
plot(data(i)+i);
hold on;
end;
I think there have to be the way to combine these loops.
Sure. As per Matlab reference: first you create empty figures and do the hold-on (you don't need to do it every time):
f1 = figure;
hold on
f2 = figure;
hold on
Then you loop:
for i=0:6
figure(f1)
plot(stim(i)+i);
figure(f2)
plot(data(i)+i);
end
This time the figures are already created; calling figure here therefore switches the active figure so you can plot on it;
And without for loops, something like this:
idx = (0:6).';
figure(f1);
plot(stim(idx)+idx);
figure(f2);
plot(data(idx)+idx);
Make sure that you apply the solution to the right dimension. The column vectors are interpreted as one dataset; And for points, remember to set the markers.

How to plot two figures in MATLAB

I am implementing a clustering algorithm for n data points and I want to plot n data points in a figure before clustering and in another figure after clustering meaning that there should be two figures in same file with same data points.
My code is like:
X = 500*rand([n,2]);
plot(X(:,1), X(:,2), 'r.') 1
%Some coding section here
After:
symbs = {'r+','g.','bv','m*','ko'};
hold on
for i = 1: length(I)
plot(X(C==i,1), X(C==i,2), symbs{i}) 2
end
I just want to plot (1) in one figure and (2) in another.
Try subplot:
figure;
subplot(1,2,1)
plot(firstdata)
subplot(1,2,2)
plot(seconddata)
This will create two axes areas within the same figure window... from your description, this is my best guess as to what you want.
Edit: From the comments below, here is what you are doing
n=50;
X = 500*rand([n,2]);
subplot(1,2,1); #% <---- add 'subplot' here
plot(X(:,1),X(:,2),'r.')
symbs= {'r+','g.','bv','m*','ko'};
subplot(1,2,2); #% <---- add 'subplot' here (with different arguments)
hold on
for i = 1: length(I)
plot(X(C==i,1),X(C==i,2),symbs{i})
end
If all you want is a second figure window, instead of doing subplot you can simply say figure in the place where I put the second call to subplot and a new figure window will be created.
figure; #% <--- creates a figure window
n=50;
X = 500*rand([n,2]);
plot(X(:,1),X(:,2),'r.') #% <--- goes in first window
symbs= {'r+','g.','bv','m*','ko'};
figure; #% <---- creates another figure window
hold on
for i = 1: length(I)
plot(X(C==i,1),X(C==i,2),symbs{i}) #% <--- goes in second window
end
You just need to add figure before each plot to get two plots in two separated figures

Two time series plots and shading between them...MATLAB

I am using MATLAB to plot two lines of a time series... (a min and max line)
I have the points converging at a single point at the end of the data.
I am trying to fill the area in between the lines and then plot other lines on top of the shaded area.
Here is my problem:
When I use "fill" it does exactly what I want it to do...but it draws a line from the last point of the data back to the initial data point. How do I get rid of it?
Here is a very vague sketch of my 2 examples:
The line below the graph is what I am talking about...
Any ideas how to avoid that?
Thanks!
I guess that you create the fill with
fill([xData1;xData2],[yData1;yData2])
where xData1 is a n-by-1 array of x-data for your first curve. This will lead to a weirdly-shaped polygon because the 'corners' of the polygon are not properly ordered.
Instead, you should do
fill([xData1;xData2(end:-1:1)],[yData1;yData2(end:-1:1])
i.e. flip the order of one of the two data sets.
As #Jonas explained (beat me to it), you need to properly order the data of the two time-series. Let me add an example to that:
%# first series
x1 = linspace(pi/4, 5*pi/4, 100);
y1 = cos(x1);
%# second series
x2 = linspace(pi/4, 5*pi/4, 100);
y2 = sin(x2);
subplot(121), fill([x1 x2], [y1 y2], 'r')
subplot(122), fill([x1 fliplr(x2)], [y1 fliplr(y2)], 'r')
hold on
plot(x1,y1, 'Color','b', 'LineWidth',3)
plot(x2,y2, 'Color','g', 'LineWidth',3)