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.
Related
I want to plot four curves in a single figure from matlab, so I am using hold on. Furthermore, I want to create an legend to each curve, so I wrote the code:
clear all
x=linspace(0,10,100);
x2=linspace(-5,15,100);
x3=linspace(-10,20,100);
x4=linspace(35,40,100);
figure(1)
plot(x,x2)
legend('x2')
hold on
plot(x,x3)
legend('x3')
hold on
plot(x,x4)
legend('x4')
hold on
plot(x,x)
legend('x')
hold off
But the result is that all my curves are in the same color, and just the last legend "x" has appeared in the figure (see it below).
How can I set one legend to each curve? All curves must have different colors.
This depends a bit on your matlab version. In older versions (and in octave), plots added through the use of hold on get the same color. In R2015b (i don't know when this was introduced), the individual plots get different colors, but still only one legend is displayed.
To get multiple colors and multiple legend entries, you can specify all data to be plotted in one call, the same for the legends:
plot(x, x, x, x2, x, x3, x, x4);
or
plot(x, [x', x2', x3', x4']);
For the legends, approach the same way:
legend('x', 'x2', 'x3', 'x4');
If you want to build up a legend without knowing the number of entries before hand, you would want to search for "dynamic legends". See for example here:
http://undocumentedmatlab.com/blog/legend-semi-documented-feature
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
I have some sparse data and want to plot them as markers connected by a smooth, interpolated line - like the default behaviour of Microsoft Excel.
There are solutions to this problem easily found on the internet, but I find them unsatisfactory. What they do is: plot the sparse data as one data set drawing it as markers without lines, interpolate it with a method of choice and plot the interpolation as the second data set, with lines without markers.
The problem with these tricks is that in the legend the two data sets will be listed separately. I would expect a single data set depicted in the legend as a line crossing through a marker.
Is it possible in Matlab?
If you want to plot an interpolated line there are lots of ways to do that. You can try generating an interpolated line using the matlab interp1() function.
Let's create x and y data with no NaN.
x = randn(1,10)
y = randn(1,10)
If you want 1000 data points where previously you only had a few, that's pretty easy:
x2 = min(x):(max(x)-min(x))/1000:max(x)
y2 = interp1(x,y,x2,'cubic')
and you can plot your data and spline using
plot(x,y,'r+')
hold on
plot(x2,y2,'r-')
A custom legend is straightforward when you use handle graphics. You can plot a dummy data set with a red line passing through a marker using
h(1) = plot(NaN,NaN,'r-+')
lstring{1} = 'Data';
You can then add a legend that points to this data set using
legend(h,lstring)
You'll end up with something that looks roughly like this:
The nice thing about using handle graphics (i.e. the h) is you can throw whatever data series you want into the legend as h(end+1) and lstring{end+1}.
When plotting multiple data series using both line specification (X,Y,linespec) triplets and (PropertyName,PropertyValue) doublets, only a single MarkerSize can be specified and this size applies to all data series. For instance,
plot(X1,Y1,'.b',X2,Y2,'-r','MarkerSize',5)
Is it possible to specify a different MarkerSize for each of the different data series without resorting to plotting the data series separately or subsequently altering plot handle properties? Neither of the following two commands is valid, but they give an idea of the desired result:
plot(X1,Y1,'.b',X2,Y2,'-r','MarkerSize',[5 10])
plot(X1,Y1,'.b','MarkerSize',5,X2,Y2,'-r','MarkerSize',10)
Try:
h = plot(X1,Y1,'.b',X2,Y2,'*r');
set(h(1),'MarkerSize',5);
set(h(2),'MarkerSize',2);
You can use scatter. It has the SizeData property which is a vector.
x = rand(10,1);
y = rand(10,1);
s = scatter(x,y);
set(s,'SizeData',linspace(1,100,10))
If you want to use line plot with markers, you can draw your plot, use hold on, and then draw scatter on top of it.
For that it is probably
plot(x1,0,'+','MarkerSize',10)
Or any other plot within the loop just
plot(x?, 0, '+', 'MarkerSize', 10, 'MarkerEdgeColor', 'r')
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.