Using streamslice with color for velocities - matlab

I am using streamslice command to visualize my flow. I want to add color depending on magnitude of velocities but there seems to be no function argument in streamslice to do so. The function is given as:
% x - x-coordinates
% y - y-coordinates
% u,v - vector volume data
h = streamslice(x,y,u,v)
The function produces this image

If you want to use streamslice, I can suggest something. It would need some tweaking to get it look like a cool figure, but it does the job, I guess. the idea is to combine a surf plot with the streamsilces plot.
Look at the result. I guess that with better a colormap and with some tricks getting the data handle of the streamsliceto change the line colour it could work nicely, speciall yin Matlab R2014b or higher.
CODE:
clear;clc;
load wind
% Use only a piece of this datasheet
x=x(:,:,5);
y=y(:,:,5);
u=u(:,:,5);
v=v(:,:,5);
mag=sqrt(u.^2+v.^2);
figure
hold on
surf(x,y,mag-max(mag(:)),'FaceColor','interp','Edgecolor','none')
colormap('hot')
streamslice(x,y,u,v)
axis([min(x(:)) max(x(:)) min(y(:)) max(y(:)) -max(mag(:)) 0])

Related

MATLAB Plotting Contour Map from Different Plots

I am trying to write a MATLAB script to give me a contour map. The contour map must be created from inputs that I generated from 100 images.
The story is like this:
I have 100 images on which I ran an image processing algorithm for optimization. Now, I got their energy curves. So, I have 100 energy curves. I want to create a contour map that will show me where the points are denser on the plot. (the energy curves are plotted as energy vs. iteration with fixed number of iterations)
The following is my variable:
energy(iteration,numImages)
Hope I explained it well.
Thanks in advance.
I interpret your question to boil down to how can I create a surface plot with colors according to the energy found in energy. I would solve this by using the contour function with a grid generated using meshgrid. If each image is described in 1000 data points with 100 files the plot can be generated as follows:
% using stuff as random junk instead of energy
numPoints = 1000;
numFiles = 100;
stuff = rand(1000,100); % replace with actual information
[X, Y] = meshgrid(1:numFiles, 1:numPoints);
contour(X,Y,stuff);
You can also create a 3D surface plot using surf and the same logic.
From what i see of you graph (and using the comments also), one possible way is to use plot3 to plot a line in 3D for every plot.
For doing so, you can use something like this code:
x=(0:0.01:1)';
aexp=zeros(100,numel(x));
hold on
for ii=1:100;
% aexp(ii,:)=exp((-x+ii/10)); %exponential
aexp(ii,:)=exp(-(x-ii/100).^2); %~gaussian
% aexp(ii,:)= x*ii; %linear increase
plot3(x,aexp(ii,:),ii*ones(1,numel(x)));
end
% set(gca,'yscale','log'); % uncomment if you need logscale.
giving
I have a few options of plot. It always plot from the XY view. I changed by hand, but you can use the view command. Notice that i used a simple counter to make the spacing in the z direction.
In a similar manner, you can plot using the contour. For my code, after the data have been generated in the for loop, remove/comment the plot3 and add:
contour(aexp) %outside the for loop,
giving
Notice that i have not really take care what i'm plotting. You can find more info on contour in the Matlab page .
You commented that the x-axis should be number of iterations, y-axis should be energy and z-axis should be the information containing how many lines are passing through from some areas. For this, make a qq variable, being it qq=number_of_lines(number of iterations,energy) . Make a discrete grid for the energy if you don't have one. Number of iterations is probably discrete anyway. The function is you who need to devise, but i would go for something which checks the number of lines for every energy and every iteration. In this case you will have the z-function that depends on y and x, that is the case to use contour or surface.
My function above make a line for every ii point, to have a 3d function. An edition for another extra loop is not hard. Just remember to have the same regular grid for every point, otherwise you will have trouble.

Create 2D Spectrogram in Matlab

I am in need of plotting a 2D spectrogram of a signal in Matlab. I need it for a printed assignment, hence the 3D image makes no sense. However, when the signal is plotted using Spectrogram it automatically produces a 3D plot of the signal.
My Code:
Dataset = 1; % Dataset to be analysed
N = 1024; % Window size
Beta = 12; % Kaiser window beta value (small = narrow main lope)
Overlap = 800; % Window overlap
Threshold = -150; % Minimum magnitude before threshold
spectrogram(Enclosure{Dataset}(1:end),kaiser(N,Beta),Overlap,2048,fs,'MinThreshold',Threshold,'yaxis');
which produces a graph that looks like this:
But it is seen from the top, and the graph is really showing this:
The reason why i need it to specifically be 2D (and why i don't settle with a screenshot) is because i am using Matlab2tikz to convert Matlab figures into Tikz figures in LaTex. with the 3D images i get figures of +100 Mb and 2D will reduce the size to <1Mb.
I don't know what version of Matlab you are using but in 2015a you should be able to get a handle to the figure with the 3D plot and change the view angle to 2D:
view(0,90);
I've also got an example of how you can make your own 2D plot from the outputs of spectrogram() using a similar method:
x = [0:0.01:100];
y = sin(5*x);
y = awgn(y,0.1);
[S,F,T,P] = spectrogram(y,200,0,length(y)*5,100);
[m,n] = size(P);
figure(2)
surf(F,T,zeros(n,m),P','EdgeColor','none')
view(0,90)
xlabel('Frequency')
ylabel('Time (s)')
The output looks like this:
Hopefully since there is no altitude information, the figure size might be smaller but I can't test that since I don't have Matlab2tikz.
One option is to capture whatever its plotted and then plot it as an image. You can do this using getframe
if you do
F=getframe(gca);
cla;
imshow(F.cdata);
You'll get exactly what you will be seeing before, but as an image.
However I think it defeats a bit the purpose of Matlab2Tikz, as the idea os that you have Tikz code describing your data...
You can try the following:
[~,F,T,ps]=spectrogram(Enclosure{Dataset}(1:end),kaiser(N,Beta),Overlap,2048,fs,'MinThreshold',Threshold,'yaxis').
% Output the spectrum in ps
imagesc(T,F,10*log10(ps))
% Generate a 2d image
view(270,90)
xlabel('Time [s]')
ylabel('Frequency [Hz]')
c=colorbar;
c.Label.String='Power [dB]';
% Extra setting to make the plot look like the spectrogram
Good luck

Matlab logarithmic range colorbar imagesc

Using the function imagesc in Matlab, I plot my (X,Y, Z) data-X array distance, Y array time, and my data Z = Z(X,Y) a matrix.
I notice that the 80% of the image has one color, because the change of Z data occurred only in the end of X for almost all Y.
Right now I use colormap('hsv') which give I think the largest range of different colors.
I need to change the colorbar range to a logarithmic one to improve visual the range of the change of my output data through time along the distance X.
I have used also contourf but still I am not sure if it will better to use this function and not imagesc which the output is more smoothed.
Please, any idea, any method or any small script that I could use to show visual the difference in data in logarithmic scale in 2D using imagesc or another build in function is more than welcome!
thank you
There is a discussion at the Mathworks website where someone provided a function that does logarithmic color bars.
https://www.mathworks.com/matlabcentral/newsreader/view_thread/152310
EDIT: copying and pasting code from link
function cbar = colorbar_log(my_clim)
%COLORBAR_LOG Apply log10 scaling to pseudocolor axis
% and display colorbar COLORBAR_LOG(V), where V is the
% two element vector [cmin cmax], sets manual, logarithmic
% scaling of pseudocolor for the SURFACE and PATCH
% objects. cmin and cmax should be specified on a LINEAR
% scale, and are assigned to the first and last colors in
% the current colormap. A logarithmic scale is computed,
% then applied, and a colorbar is appended to the current
% axis.
%
% Written by Matthew Crema - 7/2007
% Trick MATLAB by first applying pseudocolor axis
% on a linear scale
caxis(my_clim)
% Create a colorbar with log scale
cbar = colorbar('Yscale', 'log');
% Now change the pseudocolor axis to a log scale.
caxis(log10(my_clim));
% Do not issue the COLORBAR command again! If you want to
% change things, issue COLORBAR_LOG again.

How can i specify the line colour using gplot in MATLAB?

I would like to plot a graph in MATLAB using gplot.
In the documentation for gplot, it says the proper syntax is:
gplot(A,Coordinates,LineSpec)
so if I do something like:
gplot(A,XY,'-or');
it will plot the graph in red with circles for the vertices. My problem is that i would like to plot it in grey, however the documentation for LineSpec seems to only allow line colours in the set: {r,g,b,c,m,y,k,w}; i can't seem to find anywhere in the documentation that lets you specify the line colour using an RGB triplet.
Am i just missing something?
Looking through the gplot code it's a bit strangely designed. It uses the standard plot function but the logic it uses to get the line spec precludes the use of PV pairs that work with plot. Other than modifying the code I don't see a way to specify the Color property with gplot like you can with a normal line plot.
However, there is undocumented behavior in gplot that will allow you to plot the data normally yourself using a standard plot call. From the code:
% [X,Y] = GPLOT(A,xy) returns the NaN-punctuated vectors
% X and Y without actually generating a plot. These vectors
% can be used to generate the plot at a later time if desired. As a
% result, the two argument output case is only valid when xy is of type
% single or double.
So we can get our XY data and plot ourselves:
k = 1:30;
[B,XY] = bucky;
[X, Y] = gplot(B(k,k),XY(k,:));
plot(X, Y, '-*', 'Color', [0 1 1]);
axis square

Matlab: Plot3 not showing the 3rd axis

All the three variables I am using to plot are matrix of size 1x1x100. I am using this code line to plot:
hold on;
for i=1:100
plot3(R_L(:,:,i),N_Pc(:,:,i),CO2_molefraction_top_of_window(:,:,i),'o');
xlabel('R_L');
ylabel('N_P_c');
zlabel('CO_2')
end
However, I am not getting the third axis, and hence the third variable CO2_molefraction_top_of_window on the plot. May I know where am I wrong?
Besides the above question, but on the same subject, I want to know if there is any option where I can plot 4 dimensional plot just like the 3 dimensional plot which can be drawn using plot3?
So I had the same problem when using plot3. For some reason, using the hold on command "flattens" the plot. I'm not sure why, but I suspect it has something to do with the operation hold on performs on the plot.
Edit: To clarify, the 3d plot is still there, but the perspective has been forced to change. If you use the "rotate 3D" tool (the one with an arrow around a cube), you can see the graph is 3d, the default perspective is just straight on so only two axes are visible and it appears flat.
Just a note --- you only need to do the xlabel ylabel zlabel commands once (outside the loop).
Also:
is there any reason your matrices are 1x1x100 instead of just 100x1 or 1x100?
Because if you reshape them to 2D you can just do the plotting in one hit.
What do you mean by "missing third axis"? When I run your code (or as close as I can get, since you didn't provide a reproducible example), I do get a 3rd axis:
.
X = rand(1,1,100); % 1x1x100 X matrix
Y = rand(1,1,100); % 1x1x100 Y matrix
Z = rand(1,1,100); % 1x1x100 Z matrix
% Now, we could do a for loop and plot X(:,:,i), Y(:,:,i), Z(:,:,i),
% OR we can just convert the matrix to a vector (since it's 1x1x100 anyway)
% and do the plotting in one go using 'squeeze' (see 'help squeeze').
% squeeze(X) converts it from 1x1x100 (3D matrix) to 100x1 (vector):
plot3(squeeze(X),squeeze(Y),squeeze(Z),'o')
xlabel('x')
ylabel('y')
zlabel('z')
This gives the following, in which you can clearly see three axes:
If it's the gridlines that you want to make the graph look "more 3D", then try grid on (which is in the examples in the Matlab help file for plot3, try help plot3 from the Matlab prompt):
grid on
You will have to clarify "missing third axis" a bit more.
I came across a similar problem and as #Drofdarb's the hold on seems to flatten out one axis. Here is a snippet of my code, hope this helps.
for iter = 1:num_iters:
% hold on;
grid on;
plot3(tita0,tita1, num_iters,'o')
title('Tita0, Tita1')
xlabel('Tita0')
ylabel('Tita1')
zlabel('Iterations')
hold on; % <---- Place here
drawnow
end
As opposed to:
for iter = 1:num_iters:
grid on;
hold on; % <---- Not here
plot3(tita0,tita1, num_iters,'o')
title('Tita0, Tita1')
xlabel('Tita0')
ylabel('Tita1')
zlabel('Iterations')
% hold on;
drawnow
end