Draw a function from 2D to 1D as an heat map: Matlab - matlab

Suppose I have a function w from 2D coordinate space to 1D values, such that
w(a,b) := min(a,b)
I want to represent this function as an heat map in Matlab.
For example, I draw a square on the Cartesian plane with vertices (0,0), (0,1), (1,0), (0,0). Then I colour the region inside the square, by assigning the same colour to any two pairs of points (a,b) and (c,d) if w(a,b)=w(c,d).
Is my understanding of a heatmap correct? If so, how can I achieve this in Matlab?

You can achieve this with surf, which plots a surface which looks like a heatmap when viewed from above. See the commented code for details:
% Define axes points as [0,0.1,0.2,...,1]
ax = 0:0.1:1;
% Make grid of values corresponding to min(x,y)
xy = bsxfun(#min, ax, ax');
% Plot
surf(xy); % Plot surface
view(2); % Look from "above" onto x-y plane
set(gca, 'XTickLabel', ax) % Set x-axis labels
set(gca, 'YTickLabel', ax) % Set y-axis labels
colorbar; % Show colorbar as a legend
Output:
You can extend this by using a custom function if min was just an example.
ax = 0:0.1:1;
% Define function
w = #(a,b) a - exp(b)
% Use custom function inside bsxfun
xy = bsxfun(w, ax, ax');
surf(xy); % ... etc
See the docs for details: https://uk.mathworks.com/help/matlab/ref/bsxfun.html
Also see the docs for pcolor which creates a 2D colour plot so no perspective change is needed, and contourf which outlines regions not all squares.

Related

How to create a color gradient using a third variable in Matlab? [duplicate]

This question already has answers here:
Change color of 2D plot line depending on 3rd value
(5 answers)
Closed 5 years ago.
How do you create a color gradient in Matlab such that you plot a 2D line plot of y=y(x), and you color it using another variable that also depends on x such that z=z(x). A scatter or point plot is also fine by me.
I would also like to have a colormap legend kind of thing showing the color gradient and it's actual representation of z. This stuff is quite common in visualisation tools such as VisIt and ParaView but I could not yet FIGURE it out in Matlab.
If a scatter plot is fine, you can use the 4th input to scatter:
x = -10:0.01:10;
y = sinc(x);
z = sin(x);
scatter(x,y,[],z,'fill')
where z is the color.
The only way I know of to do this is with a little trick using surf:
% Create some sample data:
x = cumsum(rand(1,20)); % X data
y = cumsum(rand(1,20)); % Y data
z = 1:20; % "Color" data
% Plot data:
surf([x(:) x(:)], [y(:) y(:)], [z(:) z(:)], ... % Reshape and replicate data
'FaceColor', 'none', ... % Don't bother filling faces with color
'EdgeColor', 'interp', ... % Use interpolated color for edges
'LineWidth', 2); % Make a thicker line
view(2); % Default 2-D view
colorbar; % Add a colorbar
And the plot:
To manipulate the color of the line continuously, you'll want to use surface.
While at first look, this function looks most useful for plotting 3d surfaces, it provides more flexibility for line coloring than the basic plot function. We can use the edges of the mesh to plot our line, and take advantage of the vertex colors, C, to render interpolated color along the edge.
You can check out the full list of rendering properties, but the ones you are most likely to want are
'FaceColor', 'none', do not draw the faces
'EdgeColor', 'interp', interpolate between vertices
Here's an example adapted from MATLAB Answers post
x = 0:.05:2*pi;
y = sin(x);
z = zeros(size(x)); % We don't need a z-coordinate since we are plotting a 2d function
C = cos(x); % This is the color, vary with x in this case.
surface([x;x],[y;y],[z;z],[C;C],...
'FaceColor','none',...
'EdgeColor','interp');
Generate a colormap, e.g. jet(10). The example would generate a 10*3 matrix.
Use interp1 to interpolate between the values in the RGB space using your data by setting the first color as the lowest value and the last color as the highest value. This would generate a n*3 matrix where n is the number of data points.
Use scatter with the optional parameter c to plot the points with the interpolated colors.
activate colorbar to show the colorbar.

matlab: Graph 2d lines on x,y and z axis in a 3d plot

Given a complex signal I have 3 dimensions: I-real part, Q-imaginary part, T-time.
I have been able to use the:
plot3(I,T,Q)
to plot the signal in matlab. I would now like to add the Q-imaginary part line graph to the z-plane and the I-real part line graph to the x,y plane. How can I add the additional lines to the graph? I included a picture of what I want it to look like:
What I have so far is this:
Commented code below:
% hold on for multiple plots
figure(1)
hold on
% Plot 3D Figure
plot3(I,T,Q)
% Plot on XY plane - means function in Z = 0
plot3(I,T,Q.*0)
% Plot on YZ plane - means function in X = 0
plot(I.*0,T,Q)
hold off
In your case the planes being plotted on aren't actually the axes' zeros. You may want to set the zero vector in each 2D plot to be any single-valued vector, which you can make the correct length by methods like this:
% vector of 2s the same size as Q
A = (Q./Q).*2;
% or
A = ones(size(Q)).*2;
% or
A = A.*0 + 2;
For instance, plotting a similar function to your image:
x = linspace(0,20,1000);
hold on
plot3(sin(x),x,cos(x))
plot3(sin(x),x,cos(x).*0 - 1)
plot3(sin(x).*0 + 1,x,cos(x))
grid on
hold off

Color coded 2D plot in MATLAB

I have a d1×d2 array A of integer values and want to color code display them on the xy-plane in a rectangle scaled to d1×d2 using colors that match the magnitude of the value of array at that location. I also want to display the color chart which indicates which color is which magnitude as in the following figure:-
Is there a simple code that can do this?
Or is this kind of charting need special packages?
Will this work ('A' is matrix with non-negative entries)?
function plot2Ddistprf(A, Length, Breadth)
Amax=max(A(:));
A=A/Amax;
G1 = linspace(0,Length,size(A,1));
G2 = linspace(0,Breadth,size(A,2));
[X,Y] = meshgrid(G1,G2);
% plot data
figure; % create a new figure
contourf(X,Y,A); % plot data as surface
caxis([1,100]); % set limits of colormap
colorbar; % display the colorbar
No external library or special package is needed to create such a plot. You can use contourf to plot the data. Then, set the colormap to gray. With caxis you can control the range of colors. colorbar shows the bar on the right side.
The result looks like this:
Here is the code:
% generate sample data
d1 = linspace(-3,3,200);
d2 = linspace(-3,3,200);
[X,Y] = meshgrid(d1,d2);
A = -abs(peaks(X,Y))+100;
% plot data
figure; % create a new figure
contourf(X,Y,A); % plot data as surface
colormap(gray); % use gray colormap
caxis([91,100]); % set limits of colormap
colorbar; % display the colorbar
title('The Title');
xlabel('y');
ylabel('x');
Try the contourf function and then add the colorbar
contourf(A)
colorbar

plotting trajectory data in matlab

I have trajectory information in 3 dimensions in matlab. These are of a gesture someone is making. When I connect the points in matlab by using plot3, I can see the trajectory nicely.
However, the trajectory is a line in the plot, but I don't know in which direction the gesture has been made as the time is not visualized. Is it possible to visualize this in a 3d plot (where the dimensions are x, y and z)? For example, the colour at the start is bright red and the colour at the end is black.
Thanks for your help,
Héctor
You need the comet3 plot (if you don't mind animations).
If you do mind animations, and you're looking for a static figure, I'd use a quiver.
Example:
% value of the parameter in the parametric equation
t = 0:0.5:2*pi;
% modified coordinate axes
u = [1 0 0].';
v = [0 2 0].';
% coordinates of the ellipse
Ell = bsxfun(#plus, bsxfun(#times, u, cos(t)), bsxfun(#times, v, sin(t)));
% difference vectors between all data points will be used as "velocities"
dEll = diff(Ell, 1,2);
% Quiver the ellipse
quiver3(...
Ell(1,1:end-1), Ell(2,1:end-1), Ell(3,1:end-1), ...
dEll(1,:), dEll(2,:), dEll(3,:), ...
2, 'r') % = scale, LineSpec
axis tight equal
Result:

Matlab: How to smooth colors of matrix in plot3( ) function?

I am using plot3 to plot a matrix such that the resultant figure shows different colors for each vector of that matrix:
plot3(Grid.x1(:,:),Grid.x2(:,:),phi(:,:))
How can I smooth the coloring of this plot? Thanks!
You can use varycolor from FileExchange to construct and control a continuous range color spectrum. This way the transition between different lines will seem more natural and proximal lines will have similar colors.
You can read more and see examples usage here http://blogs.mathworks.com/pick/2008/08/15/colors-for-your-multi-line-plots/
Example:
[X,Y,Z] = peaks(128);
% raw plot3()
figure; plot3(X, Y, Z);
% define set equal to line number
ColorSet = varycolor(128);
% smooth plot3()
figure; hold all;
set(gca, 'ColorOrder', ColorSet);
plot3(X, Y, Z); view(3);
Update:
For a continuous 3D plot (i.e. a surface) you can use surfl instead of plot3 and display your data as a 3-D Surface Plot (with Shading). You can additionally apply any colormap on the resulting surface, i.e. gray or ColorSet as above.
surfl(X,Y,Z);
shading interp;
colormap(gray);