Matlab logarithmic range colorbar imagesc - matlab

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.

Related

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.

How to add a regression line to a scatter plot in MATLAB

I am trying to add a regression line onto a plot in MATLAB.
this is the code I have:
errorbar(x,y,SEM,'o')
hold on % Retains current plot while adding to it
scatter(x,y)
title('The Effect of Distance Between Images on the Flashed Face Distortion Effect','FontSize',14); % Adds title
xlabel('Distance (Pixels)','FontSize',12); % Adds label on the x axis
ylabel('Average Distortion Rating','FontSize',12); % Adds label on the y axis
hold off
And this is my code for a regression:
mdl = fitlm(x,y,'linear');
Could anyone tell me how to combine the two so i get the regression line on the plot?
I am using psychtoolbox on MATLAB on Windows.
Thanks!
Before the hold off statement, add the following lines:
xf = [min(x), max(x)];
plot(xf, polyval(polyfit(x,y,1), xf));
You may want to decorate your plot call with supplemental arguments setting the line style, and no additional toolboxes are required.

Using streamslice with color for velocities

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])

MATLAB - Plotting a smooth volume from 3D scatter plot data

So I have data in the form [x y z intensity] that I plot on a scatter3 figure with xyz axes. The colour of the data is used to dictate the intensity value. Problem is, using a scatter plot means the data points show up as discrete points. What I need, is a smooth shape - so I guess I need some kind of interpolation between the points?
I've tried using trisurf, but the problem with this one is that it interpolates between points that it shouldn't. So where I should have 'gaps' in my surface, it joins up the edges instead so it fills in the gaps. See the attached pics for clarification.
Does anyone have any suggestions?
The code I use is as below (the commented out scatter3 is what does the scatter plot, the rest does the trisurf):
% Read in data
dataM = csvread('3dDispersion.csv');
% scatter3(dataM(:,1), dataM(:,2), dataM(:,3), 5, dataM(:,4),'filled');
% Plot
hold on;
x = dataM(:,1);
y = dataM(:,2);
freq = dataM(:,3);
tri = delaunay(x,y);
h = trisurf(tri, x, y, freq);
% Make it pretty
% view(-45,30);
view(3);
axis vis3d;
lighting phong;
shading interp;
Use the boundary function in Matlab. This will apply a mesh similar to shrinkwrap over your points. In order to reduce the "gap closers", you will want to increase the "shrink factor".
Try K = boundary(X,Y,Z,0.9)
Where X, Y & Z are the vectors of your data points
https://www.mathworks.com/help/matlab/ref/boundary.html
You can then use trimesh or related surface plotting functions depending on how you want to display it.

MATLAB contour plot of 2D scatter

What I want to do is very simple, I just cannot seem to get MATLAB to do it. I would like to plot contours using my 2D data set.
My data set is large; 2 x 844240. I can do a scatter plot just fine,
scatter(Data(1,:), Data(2,:));
Reading through the forums I found Scatter plot with density in Matlab, where a hisogram was plotted. This would suffice, however, I would like to overlay the plots.
The issue is that they have different axis, my scatter data has an axis of [0 0.01 0 2500]; whereas the histogram is [0 100 0 100].
Is there a way to change the axis values of the histogram without modifying the image?
Thanks!
If I understand correctly, you are using hist3 to construct a histogram and then using imagesc to plot it. You can use the second output argument of hist3 to get the histogram bin centers, and then pass those on to imagesc, e.g.
nBins_x = 100;
nBins_y = 100;
[counts, bin_centers] = hist3(Data, [nBins_x nBins_y]);
x_bin_centers = bin_centers{1};
y_bin_centers = bin_centers{2};
imagesc(x_bin_centers, y_bin_centers, counts)
A couple other notes:
In your case, you will need to transpose your [2 x N] matrix when passing it to hist3, which expects an [N x 2] matrix.
imagesc puts the first axis (which I've been calling the "x" axis) on the vertical axis and the second on the horizontal axis. If you want to flip it, you can use:
imagesc(y_bin_centers, x_bin_centers, counts')
If you want to specify the histogram bins explicitly (e.g. to match your scatterplot) you can specify that in the arguments to hist3:
x_bin_centers = linspace(0, .01, 100);
y_bin_centers = linspace(0, 2500, 100);
counts = hist3(Data, {x_bin_centers, y_bin_centers};
And if you want a contour plot, you can use (note that contour takes the axes arguments in a different order than imagesc):
contour(x_bin_centers, y_bin_centers, counts');
If you are unhappy with the jaggedness of the contours, you may consider using a kernel density estimate instead of a histogram (check out ksdensity) (oops, looks like ksdensity is 1-D only. But there are File Exchange submissions for bivariate kernel density estimation).