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

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

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.

Draw a function from 2D to 1D as an heat map: 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.

plot 2D intensity figure in 3D coordinate system

I have a 2D intensity plot, as in this example:
[xx yy] = meshgrid(0:0.1:1, 0:0.1:1);
figure(1)
imagesc(sin(xx)) %(x,y)-plot at z=0
Now, as I have noted in the comment, this plot is in the xy-plane and I have taken z=0. I'd like to plot this in a 3D coordinate system as a function of x and y, but taking z=0. I tried with plot3 in this way, plot3(xx,yy,cos(yy)), but that only plots lines and gives them a curvature which I am not interested in. I'd like only a plane at z=0.
You can use surf to accomplish this. We use the xx and yy meshgrid outputs as the x and y positisions, a matrix of zeros as the z value and sin(xx) as the color. We also remove the edges by setting the EdgeColor to none.
surf(xx, yy, zeros(size(xx)), sin(xx), 'EdgeColor', 'none')

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

Multiple plots on a logarithmic scale

I'm trying to plot two lines (data and linear fit) in a single graph with logarithmic scale. My code:
Iots = I_An./Temp.^2; % I Over T Squared
Oot = 1./Temp; % One Over T
[p,~] = polyfit(Oot,Iots,1);
linfit = polyval(p,Oot);
figure('color','w','units','normalized','outerposition',[0 0 1 1]);
hold on
loglog(Oot,Iots,'.','LineWidth',2);
loglog(Oot,linfit,':r','LineWidth',2);
The result is not a logarithmic scale graph:
If I run just one of the plot lines, it works on its own. What should I do? Are there any contradicting commands?
You want to call hold on after creating your first loglog plot. Also, you only need to use loglog on the first plot to create the logarithmic axes. After than you can just call normal plot and it will use the logarithmic axes.
x = linspace(0, 100);
loglog(x, x, '.', 'LineWidth', 2);
hold on
plot(x, x.^2, '.r', 'LineWidth',2);