Force plot() to use specific boundaries - matlab

I want to use the 2d plot([x1,x2,x3,x1],[y1,y2,y3,y1]) to draw a triangle in my plot image. But how do I define the borders? the Diagram should not start on the motleft point and so forth but for example on the point of origin or any other point I like to use. Also it should end where I want. How to do that?
here the full code:
xs = [0,10,20,0];
ys = [30,50,30,30];
plot(xs,asinh(tan(ys*pi/180)));
the result I wanted:
xs = [0,10,20,0];
ys = [30,50,30,30];
plot(xs,(asinh(tan(ys*pi/180))*180/pi));
xlim([-10 30])
ylim([-10 60])

I'm not sure I understand your question. If you want to set the limits of the axes of your plot so that you can place your triangle anywhere within the plot:
Try xlim([xmin, xmax]) and ylim([ymin, ymax]) after you run the plot command: see http://www.mathworks.com/help/techdoc/ref/xlim.html
If you want to know how to draw a triangle by plotting points and connecting the points:
Try simply ordering x1, x2,x3 etc in the order in which you want to connect the dots and run plot so that it plots lines (which I believe it does by default). But to be explicit you can run plot([x1,x2,x3,x1],[y1,y2,y3,y1],'b-') to make a blue line connecting the points.

Related

Graph different 2D ellipses in 3D axes at different heights in MATLAB

I want to graph different ellipses at different heights (z-coordinates).
My idea was to write the following code:
z=0:1/64:3/8;
t=linspace(-pi,pi,25);
[t,z]=meshgrid(t,z);
x=cos(-t);
y=cos(-t-4*pi*z);
I would like MATLAB to read my code like:
"Find x and y, and plot at the corresponding height (z). By doing so, join the points such that you'll form an ellipse at constant z".
I'm not sure what kind of function I could use here to do this and was hoping for someone to tell me if there exists such a function that will do the job or something similar.
In case you're wondering, I want to graph the polarization of light given two counterpropagating beams.
EDIT: While this is similar to the question draw ellipse and ellipsoid in MATLAB, that question doesn't address plotting 2D ellipses in 3D axes, which is what I am trying to do.
This can be solved by removing the meshgrid, and just using a plain old for-loop.
t = linspace(-pi,pi,25);
z = 0:1/64:3/8
f = figure;
hold on;
for i = 1:length(z)
x=cos(-t); y=cos(-t-4*pi*z(i));
plot3(x,y,z(i)*ones(length(z),1));
end
The problem in the original code is that you're trying build the ellipses all at once, but each ellipse only depends on a single z value, not the entire array of z values.
When I run this code, it produces the following plot:

Plot all lines creating mesh in matlab/octave plot

I make point plots like this one:
But now I need to plot all the lines between the points as well to create rectangular mesh under a deformation (i.e. do the connection to the closest neighbors in x and y direction - not connect all the points with all the points).
How can I convince matlab/octave to do that?
The code I have used is here:
%T,H,W and k are defined above, it doesn't matter for the plot.
for ii=1:2:H
for jj=1:2:W
k=0.1;
x=jj;
y=ii;
U(ii,jj)=7*exp(0.05*(y-H))*cos(k*x-T);
V(ii,jj)=4*exp(0.1*(y-H))*sin(k*x-T);
X(ii,jj)=jj;
Y(ii,jj)=ii;
end
end
plot(X+U,Y+V,'k.');
A plot as you want it is easily done with mesh(X,Y,Z).
The problem with your code is that you have a lot of zeros in your matrices. These might be intentional. In that case I could provide another solution. But mesh() connects neighboring points in a matrix. Therefore having the 0 in every second line and row would connect every point to (0,0). The easiest way is to just let your ii,jj grow in steps of 1.
%T,H,W and k are defined above, it doesn't matter for the plot.
H = 20;
W = 40;
T = 2*pi;
for ii=1:1:H
for jj=1:1:W
k=0.1;
x=jj;
y=ii;
U(ii,jj)=7*exp(0.05*(y-H))*cos(k*x-T);
V(ii,jj)=4*exp(0.1*(y-H))*sin(k*x-T);
X(ii,jj)=jj;
Y(ii,jj)=ii;
end
end
plot(X+U,Y+V,'k.');
hold on
mesh(X+U, Y+V, zeros(size(X)));

Removing the edge line on a contour plot

I used Matlab to create a polar coordinate and converted it into Cartesian coordinate.
[th,r] = meshgrid((0:0.5:360)*pi/180,0:.02:1);
[X,Y] = pol2cart(th,r);
I get data on this mesh and produce a contourf plot over it.
My problem is that I get a center line in my contourf plot which I would like to remove, could some help me with this
Thank you
If I extend a little bit your example to get something I can plot, I do reproduce the problem:
[th,r] = meshgrid((0:0.5:360)*pi/180,0:.02:1);
[X,Y] = pol2cart(th,r);
Z = sqrt( X.^2 + Y.^2 ) ;
isoLevel = 0:0.1:10 ;
[C ,hc] = contourf(X,Y,Z,isoLevel) ;
The black line at the interface is because the function contourf create patch objects and these objects tend to "close" themselves (they will create a line between the first and last point defined in their profile).
This is easier to observe if you do not complete the definition of your profile over 360 degrees. The picture on the right shows you the same example but with the grid only from 0:350 and with the LineStyle set to :.
As you can see, it is difficult to control how Matlab will actually render this specific profile limit. There are ways to control specific edges of patch objects but in this case it would involve retrieving the handle of each patch object (10 in my case but many more in more complex cases), locating the edge you want to control and basically redefining the patch (each of them). You'd be better off drawing the patches from scratch yourself.
Fortunately, there is a simple ways out of that: Get rid of all the patch edge lines...
but then you may miss your isolines! No problem, just plot them on top of the patches !
You get all your colored patches (with no border) and a set of (iso)lines over which you have full control.
Two easy way to get you patch without lines (i) set the shading to shading flat, or (ii) specify 'EdgeColor','none' in the parameter of the contourf function.
To get your isolines on top, use the sister contour function.
So using the same X,Y and Z data than previously:
isoLevel = 0:0.1:10 ;
[C ,hc] = contourf(X,Y,Z,isoLevel,'EdgeColor','none') ; %// set of patches without border
% shading flat %// use that if you didn't specify ('EdgeColor','none') above
hold on
[C2 ,hc2] = contour(X,Y,Z,isoLevel,'LineColor','k') ; %// now get your isolines
will render:
It's a good idea to store the handle hc2 in case you want to modify your isolines properties (color, style, thicknes etc ...).
Also, specifying the isoline levels is recommended. This way you can make sure both contour and contourf will use the same set of isovalues. It could probably work without this (because the underlying dataset is the same), but personally I always prefer to be explicit and not rely on background calculations.

How can we fill different levels of contour with a color in matlab

I have a figure which consists of different levels of a contour plotted using the hold on function. I want to fill the space between the levels of contour with a color. Could you please assist me how can I do that. I have already tried the contourf function. The figure consists of different red colored levels of a contour, what I want is a solid color filled between these contour levels.
I am not sure if I got exactly what you wanted but here goes a possible solution to your problem.
If you plotted using the hold on function I would therefore assume you have each contour in a different variable. If so you can use logic to check whether a contour is higher or lower relatively to another. If your problem is that you do not have the same x axis for each so that you can use logic just interpolate for each contour (for example between -0.8 and 0.8)
I will give an example of what I am saying. See if this helps.
%simulated contours
x=linspace(0,pi,100);
y = sin(x);
y2 = sin(x)*2;
y3 = sin(x)*3;
figure, hold on,
plot(x,y),
plot(x,y2),
plot(x,y3),
%fill contours
[X,Y]=meshgrid(x,0:3/100:3);
zzz=(Y<repmat(y,size(Y,1),1))+(Y<repmat(y2,size(Y,1),1))+(Y<repmat(y3,size(Y,1),1));
figure,imagesc(zzz)
set(gca,'YDir','normal')

joining dots of a scatter plot and create a line

How is possible to join dots of a scatter plot after plotting, and make a line from a dotted plot?
I'm guessing you generated a scatter plot from x and y coordinates by,
plot(x,y,'.');
Join them with
plot(x,y,'.');
hold on;
plot(x,y,'-');
Or in one command
plot(x,y,'.-');
Is this what you wanted?
If you have an existing plot as a scatter plot, you cannot simply just join the dots without knowing which points are connected to which others.
If you know the order/connectivity of the points, then you could simply have used the plot function to do that in the first place. The call
plot(x,y,'-')
will connect the dots with straight line segments. If you wish to use a marker symbol at each point along the line, then you can add one of the markers that plot allows, as this:
plot(x,y,'o-')
You can get a list of the allowed markers from
help plot
If you have used scatter on a set of points, and now wish to overlay a line connecting the points, then use the hold function to force matlab to plot on top of the scatter plot. For example,
scatter(x,y)
hold on
plot(x,y,'-')
hold off
Again, any of these variations require you to know the connectivity between the points. There are some schemes that can sometimes work to recover that connectivity from a list of isolated points. One of these methods is called CRUST, often used for 3-d surface reconstruction. I found many references by a simple search for "crust algorithm".
If you have a scatterplot (made with the scatter function I suspect) and for some reason don't want to redraw it with plot, here is what you can do to connect the dots:
h = findobj(gca,'type','hggroup');
hold on
for k=1:numel(h)
x = get(h(k),'xdata');
y = get(h(k),'ydata');
plot(x,y,'-')
end
hold off
The dots will be connected by their original order. If you want you can sort the data before plot, for example by x:
[x,ind] = sort(x);
y = y(ind);
To answer the question of how to do this in Maple, you can simply use the PointPlot command from the Statistics package with the style option set to line or pointline. For example:
Statistics:-PointPlot([2, 4, 6, 4], xcoords=[1, 2, 3, 4], style=pointline);
Specifying the option style = pointline shows both the points and a connecting line; style = line shows just the line.