Best way to plot marks on a line in Matlab - matlab

This question is quite basic, but I am looking for the best way to do this.
For a plotted line in matlab, how can a few points on the line be marked. I know it is possible to plot directly using marks. But, in case the line has been plotted and the marks are just to differentiate the plots. Instead of selecting a few points and plotting the second time. An example is shown in this figure below

Right after creating the initial plot you can use the command 'hold on' to keep that plot 'live' and then replot the points as markers. For example:
x=1:10;
y=2*x+4;
plot(x,y)
hold on
plot(x,y,'+')
You can use '*', '.' or any other marker instead of '+'.
If you want the markers to be evenly spaced (and your data is not ,originally) you may create an evenly spaced x vector and then, assuming your plot is not too extreme, interpolate the y values and add just the points. For example:
x=[1,1.5,2,2.3,3,4,4.8,5,6.1,6,7,8,9,10];
y=2*x.^2+4;
plot(x,y)
hold on
x_lin=linspace(min(x),max(x),20);
y_lin=interp1(x,y,x_lin,'linear');
plot(x_lin,y_lin,'+')
hold off
With the following result:
If 'linear' doesn't give a good enough result you can try other interpolation methods like 'cubic', 'spline'...

If you don't want to "add" the marked points in a second moment (but I don't think you will have some advantage), consider to
A) use two plot instructions, separating the array of points "to be marked" using
C = setdiff(A,B)
alternatively
B) plot every point in a for cycle with counter i under a condition
%not tested solution
c1 = '.r' %red point, if they are near they seems a line
c2 = '*b' %blue marker
if (marker_condition == true)
plot(x(i), y(i), c2)
else
plot(x(i), y(i), c1)
end

Related

Matlab - Plot difference between two graphs in specific points

I have two graphs, one is the exact graph of a solution, the other is a numerical approach. I have 4 specific points in my figure (t=0.25,0.5,0.75,1), where I want to illustrate the difference between the two graphs with a straight line. I found the errorbars function but i don't see any use there. Hope you can help me!
Edit:
this is the example figure:
t = [0:0.25:1];
y = t.*4;
x = t.^2+3;
plot(t,y,t,x)
I have 4 points now, t=0.25; t=0.5; t=0.75; t=1; At this points, I just want a vertical line between the two plots. I already have tried this: plot([t(1),y(1)],[t(1),x(1)])
but it just creates a line over the whole figure.
✶ It seems that you're not using hold on before using plot command the second time because otherwise you'd have got the desired result (which is actually not a correct way of plotting a vertical line).
✶ You're mixing up the values of x and y for plot(x,y). To plot a vertical line, it should be used like this: plot([x,x], [y1,y2])
For your case, you may not notice the difference between plot([t(1),y(1)],[t(1),x(1)]) (which is incorrect) and plot([t(1),t(1)],[x(1),y(1)]) (which is correct) because it is by chance that the values are same. Plot it for some other points and you'll realize the difference.
Fixed Code:
t = [0:0.25:1];
y = t.*4;
x = t.^2+3;
plot(t,y,t,x)
hold on
plot([t(1) t(1)],[x(1) y(1)])
% You have 't' on your horizontal axis and 'x'and 'y' values on the vertical axis
axis equal % just for better visualization
Output:

plot multiple 3d lines

I'm trying to plot 2 lines in a single 3D graph. I have 3 coordinate data matrices for each line.
This is my code:
plot3(pathline_x1 , pathline_y1 , pathline_z1,'g');
hold on
plot3(pathline_x1,pathline_y1,pathline_z1,'r');
hold on
From some reason it plots only the last one. Can someone help me plot both lines?
x = [1 2];y=x;
figure;plot(x,y,'r');hold on;plot(x,y,'b');
This will overwrite your line, since x and y are exactly equal.
To see it actually works, plot two different lines:
figure;plot(x,y,'r');hold on;plot(x,y+2,'b');
Note that I have called hold on only once, since it will hold all plots within the figure until you call hold off or open a new figure using figure.

Customized Legend in Matlab

Here I have a for loop to plot the content of my matrix.
Based on 'type' value I determine the plot object (ks,bO,rX)
for k = 1:length(data1)
if(type(k)==1)
h=plot(data1(k,1),data1(k,2),'ks');set(h,'linewidth',2);hold on;
elseif(type(k) ==0)
h=plot(data1(k,1),data1(k,2),'bO');set(h,'linewidth',2); hold on;
else
h=plot(data1(k,1),data1(k,2),'rX');set(h,'linewidth',2); hold on;
end
end
I am little bit confused to find a way to put legend in my final figure, which shows my own explanation regarding each object(ks,bO,rX).
By default, MATLAB will see the output of this loop as not three plots but as many individual plotted points. Even though some of the points are plotted with the same settings, it doesn't automatically recognise them as part of the same series. If you give it three legend entries, it will assign them to whatever the first three points plotted were.
The simplest way around this is to change the way you plot and use logical indexing, rather than a loop:
h=plot(data1(type==1,1),data1(type==1,2),'ks'); set(h,'linewidth',2);
hold on;
h=plot(data1(type==0,1),data1(type==0,2),'bO'); set(h,'linewidth',2);
h=plot(data1(type==-1,1),data1(type==-1,2),'rX'); set(h,'linewidth',2);
Now we have only three plots, so giving legend the three should give us the correct match between those plots (in the order they were plotted) and our labels:
legend({'Type 1'; 'Type 0' ; 'Type -1'})

Plot 2d matrix with legend from another matrix

So say I have the 2d following matrix:
a = [1,2,3,4,5;
1,2,3,4,5;
1,2,3,4,5;
1,2,3,4,5]
and another matrix with the following values:
b = [0.3,0.4,0.6,0.9,1.2]
Not, I need to plot all the column vectors in a. I can very easily do this with :
plot(a)
However, I want a legend on the side which also shows just which line in the plot corresponds to what value from b.
I've done this using:
legend(b)
after the plot line. However, I wasn't sure if this is plotting the correct correspondence as in the first line color in the legend from b is pointing to the first line in a. Could someone tell me if I am right or at least rectify what I'm doing in that case.
You can get a handle from plot and pass it to legend. Also, you need to convert b to strings to use it in legend.
h = plot(a); % returns a vector of handles to the individual plots
legend(h,num2str(b(:)))

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.