I have had some success in the past with seeking help here at SO and have garnered many good hints and tips from you guys. I have run into three problems that I would really like help with.
Firstly here is a picture of the scatter plot I'm using as input:
Scatter plot
Here is the code I've used to generate that exact plot (minus the manual arrows/lines):
Source Code
I would like to know if someone could help me with three questions I have:
How do I programatically draw those lines I have entered manually given the points above? I've tried a LS approach and it produces ok(ish) results and I am uncertain of get 'tighter' straight lines.
There are sometimes outliers in my data that don't exactly conform with the other data in row/column fashion. Is there a way to remove such outliers (as seen in Point A on the Plot)?
Finally is there a way to calculate the 'centres' of each of the 'rectangles' given that the points aren't ordered in a specific order? Could one order these points in a specific order to help? Or is there a smarter way?
Thanks for taking the time to look at my question. I really hope somebody can help me with this - I've been wracking my brain trying to figure out how to do these things and I cannot come up with a solution.
EDIT: Removed the link to my LS approach as it is incorrect in the horizontal direction.
Alright, so I had a little downtime to try to lend a hand here. I think I can at least get you started on programatically drawing the lines and hopefully it will give you some insight into tackling the other two questions you have here.
I pretty much copied the code you had and started from there. What I paste here is what I've added/tweaked after you declared you cx and cy matricies.
Now to explain my thought process and logic. Cy is designed to be the "zipped" matrix of cx and cy but sorted along the Y coordinate value. Cx is the same except sorted along the X coordinate instead. The logic and such should be pretty straight forward there I hope. If not just say so and I'll explain!
Now the diffY and diffX. I took the difference of each coordinate (Xs and Ys seperately) since we know there is a pattern here that causes the XY pairs to form a grid-like shape. I'm essentially banking on the fact that there is a sizeable difference the the Y (or X) value for each line of points (horizontal and vertical). Once I have this difference between adjcent points I look for a difference greater than 2 (this is post-hoc after seeing what the differences were). The -1 is used to give me the actual end of the row instead of the start of the next row since we're going to be starting from item (1,:) in our list (makes more sense if you look at the lines in the code).
(This is of course a HUGE assumption and makes this specific for this particular data set. It can likely be generalized for most grid-like shapes though, but that has to be tested.)
Also a note. I did notice that later on in the list of points I gleaned using the difference my thought process started to break down. You can continue to plot more lines following the pattern I have in the code to see what I mean.
As far as your other two questions. You can likely take the idea that I have here and improve/expand upon it. I'm thinking that you should be able to snag the outlier by simply looking at all the points in a given row (idx[1] -> idx[2]) and making sure they fit along the line. Essentially requiring that they satisfy the general formula for the equation of a line that you can get from using the points that are used to draw the line. You will of course have to have a little error tolerance for that.
Then for the rectangle you can use the ordered group of points to create your rectangles and probably utilize the regionprops function to calculate the centroid (center in a 2D world) of a rectangle specified by the data points you can glean.
Best of luck!
Cy = zeros(length(cy),2);
[Cy(:,2), IX] = sort(cy);
Cy(:,1) = cx(IX)';
diffY = [0; diff(Cy(:,2))]
idx = find(diffY>2)-1
Cy(idx,:) % Last mark in given row, appears to be slight issue further along.
Cx = zeros(length(cx),2);
[Cx(:,1), IX] = sort(cx);
Cx(:,2) = cy(IX)';
diffX = [0; diff(Cx(:,1))]
idx2 = find(diffX>2)-1
Cx(idx2,:) % Last mark in given row, appears to be slightly off later on. (top of grid though)
%%
figure('Position',[0,0,c,r]);
scatter(Cy(:,1),Cy(:,2),'.');
axis([0 c 0 r])
set(gca,'YDir','reverse')
% Horizontal lines
line([Cy(1,1) Cy(idx(1),1)], [Cy(1,2) Cy(idx(1),2)], 'Color', 'red', 'LineWidth', 2)
line([Cy(idx(1)+1,1) Cy(idx(2),1)], [Cy(idx(1)+1,2) Cy(idx(2),2)], 'Color', 'red', 'LineWidth', 2)
line([Cy(idx(2)+1,1) Cy(idx(3),1)], [Cy(idx(2)+1,2) Cy(idx(3),2)], 'Color', 'red', 'LineWidth', 2)
% Vertical lines
line([Cx(1,1) Cx(idx2(1),1)], [Cx(1,2) Cx(idx2(1),2)], 'Color', 'red', 'LineWidth', 2)
line([Cx(idx2(1)+1,1) Cx(idx2(2),1)], [Cx(idx2(1)+1,2) Cx(idx2(2),2)], 'Color', 'red', 'LineWidth', 2)
Related
I would like to look at two dimensional data in a time-series - the first idea I had was to use a scatter plot, where you can easily explore timepoint-to-timepoint. Is there a function I could use for this? I looked at scatter3 but it can only plot perfectly-cubic data, not as below:
e.g.
data=rand(5,5,3);
scatter3(data(1,:,:),data(:,1,:),data(:,:,1)) %throws an error
thanks
edit: Originally I had something like >this< in mind
scatter3 seems to be for 3D plots, but you say your data is 2D.
For a simple time-series graph you could presumably even just use plot:
figure
nPoints = 25;
dataX = 1:nPoints;
dataY = rand(1,nPoints);
plot(dataX,dataY, 'o-')
However, the example you give in your link looks like something else, so it seems like scatter (rather than scatter3) might be what you're after. Maybe something like this?
figure
nPoints = 25;
dataX = 1:nPoints;
dataY = rand(1,nPoints);
dataArea = rand(1,nPoints)*100;
dataColours = rand(nPoints,3);
scatter(dataX,dataY,dataArea,dataColours)
EDIT:
I think I understand better what you mean, sorry I didn't see the buttons at the bottom of the link, but correct me if I'm wrong. So you have a set of XY coordinates for multiple objects at different points in time, and ideally you want to plot how the XY coordinates of each object (in 2 dimensions) change over time (in 3 dimensions). Your initial approach in using scatter3 was to try and make a simple 3d graph, but maybe ideally you want a 2d graph that can be either animated or interactive, to change the time point displayed at any given time?
Going back to your original question, I think the issue with your attempt to use scatter3 (or plot3 might be useful too) is I'm not sure what your dummy data would represent. You created data as a 5x5x3 matrix, and I assume that might represent 25 data points, at 3 different time intervals? However, which data would represent the X and which the Y coordinates? It would work with something like the following, where each variable represents the X/Y/Z coordinates of 6 objects (columns) at 5 different time points (rows)
myX = rand(5,6,1);
myY = rand(5,6,1);
% I'm making each time point increase linearly.
myZ = repmat((1:size(myX,1))', 1, size(myX,2));
plot3(myX, myY, myZ, 'o-')
grid on
% Note I find the default dimensions it treats as X, Y and Z unintuitive (e.g. the Z dimension is the vertical dimension), but one could swap the input arguments around to overcome this.
However, especially if you have a lot of points, I'm not sure how clear a graph like this will be, especially compared to the example in your link.
Instead it seems like you ideally want only the XY coordinates of all objects to be plotted for only one time point at once, and a way to cycle through each time point sequentially. This seems trickier, and maybe someone else will be able to answer better than I have. A couple more questions though that might be useful:
How much do you care about the smoothness of the transition. In the example link the circles move smoothly from one position to another, rather than just jumping/teleporting between points.
Ideally do you want a function that would produce an 'animation', cycling through all the time points from begining to end, or a way of manually specifying/changing which time point is being displayed. If the former, maybe this function would be useful (though I've tried it myself yet) https://uk.mathworks.com/matlabcentral/fileexchange/42305-multicomet
I have a dataset with points (X,Y coordinates) that represent the shape of a glacier. However, when I plot them with
% Import glacier shape
Glaciershape = readtable('dem_glacierlocation.txt');
figure(1);
S = Glaciershape(:,1);
T = Glaciershape(:,2);
plot(S,T,'-')
It seems that an points connect when they don't need to (see attachment, at the left upper corner of the shape). Is there a way to fix this? You can download the dataset in the link below
Thanks!
Download text file glacier
If you are looking for the shortest path connecting all dots in a loop, this is known as the traveling salesman problem, which is very difficult to solve.
If you are just looking for an easy way to visualize try:
plot(S,T,'.')
where you replace the '-' you had above with '.'. This will plot the x,y coordinates unconnected and you can let the human brain do the connections, which it is good at.
Here is the image without connections. It looks like at the top there are two areas which are holes, which is why connecting all the points might be problematic.
plot will assume every set of x,y coords is sequential in a series and plot them all without a care as to content.
If you could make some assumption about the distribution of points then you could use that to break on those that fail. For example, a point in the series is always less than 100 units from the previous point. Otherwise, this begins a new series list. Using that you could do a check on distance between subsequent points using diff as in:
% assume data stored in xcoord,ycoord
% check for distance greater than 100
idx = find(sqrt(diff(xcoords).^2 + diff(ycoords).^2) > 100 );
% in this particular data set there are 3 disjoint sections
% plot out each section - here each done explicitly for illustration
plot(xcoords(1:idx(1)),ycoords(1:idx(1)));
hold on;
plot(xcoords(idx(1)+1:idx(2)),ycoords(idx(1)+1):idx(2));
plot(xcoords(idx(2)+1:idx(3)),ycoords(idx(2)+1):idx(3));
plot(xcoords(idx(3)+1:end),ycoords(idx(2)+1):end);
edit: added other plot's after looking at data file provided
hope that helps...
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:
I want to assign vector to a contourf graph, in order to show the direction and magnitude of wind.
For this I am using contourf(A) and quiver(x,y), where as A is a matrix 151x401 and x,y are matrices with the same sizes (151x401) with magnitude and direction respectively.
When I am using large maps i get the position of the arrows but they are to densily placed and that makes the graph look bad.
The final graph has the arrows as desired, but they are to many of them and too close, I would like them to be more scarce and distributed with more gap between them, so as to be able to increase their length and at the same time have the components of the contour map visible.
Can anyone help , any pointers would be helpful
i know its been a long time since the question was asked, but i think i found a way to make it work.
I attach the code in case someone encounters the same issues
[nx,ny]= size(A) % A is the matrix used as base
xx=1:1:ny; % set the x-axis to be equal to the y
yy=1:1:nx; % set the y-axis to be equal to the x
contourf(xx,yy,A)
hold on, delta = 8; %delta is the distance between arrows)
quiver(xx(1:delta:end),yy(1:delta:end),B(1:delta:end,1:delta:end),C(1:delta:end,1:delta:end),1) % the 1 at the end is the size of the arrows
set(gca,'fontsize',12);, hold off
A,B,C are the corresponding matrices ones want to use
I would like to plot a vertical line (I'd prefer any orientation, but I'd be happy with just vertical right now) with two-color dashes, say red-blue-red-blue-...
I know I could do it like this:
plot([1,1],[0,1],'r'),
hold on,
plot([1,1],[0,1],'--b')
However, since I need to be able to move the line, among others, it should only have a single handle. How could I do this?
EDIT
Thank you for your answers. I guess I should indeed give some more information.
I have some data that is classified into different parts. I want to be able to manually adjust the boundaries between classes. For this, I'm drawing vertical lines at the classification boundaries and use draggable to allow moving the lines.
For the boundary between the red and the blue class, I'd like to have a red/blue line.
plot(ones(10,1),linspace(0,1,10),'-bs','MarkerFaceColor','r','MarkerEdgeColor','none','linewidth',6)
is what I'm actually using at the moment. However, it's not so pretty (if I want equal spacing, it becomes a real pain, and I want to give both colors the same weight), and I would like to have the possibility to use three colors (and not with marker edge and face being different, because it makes my eyes bleed).
Unfortunately, draggable does not allow me to use multiple handles, and grouping the lines with hggroup does not seem to create a draggable object.
cline looks like a promising approach, but rainbow colors won't work for my application.
You can use the code you have, and just concatenate the handles from each line into a vector of handles. When you want to change the properties of both lines simultaneously, the SET function is able to accept the vector of handles as an argument. From the documentation for SET:
set(H,'PropertyName',PropertyValue,...)
sets the named properties to the
specified values on the object(s)
identified by H. H can be a vector of
handles, in which case set sets the
properties' values for all the
objects.
Here's an example:
h1 = plot([1 1],[0 1],'r'); %# Plot line 1
hold on;
h2 = plot([1 1],[0 1],'--b'); %# Plot line 2
hVector = [h1 h2]; %# Vector of handles
set(hVector,'XData',[2 3]); %# Shifts the x data points for both lines
UPDATE: Since you mention you are using draggable from the MathWorks File Exchange, here's an alternate solution. From the description of draggable:
A function which is called when the
object is moved can be provided as an
optional argument, so that the
movement triggers further actions.
You could then try the following solution:
Plot your two lines, saving the handle for each (i.e. h1 and h2).
Put the handle for each in the 'UserData' property of the other:
set(h1,'UserData',h2);
set(h2,'UserData',h1);
Create the following function:
function motionFcn(hMoving) %# Currently moving handle is passed in
hOther = get(hMoving,'UserData'); %# Get the other plot handle
set(hOther,'XData',get(hMoving,'XData'),... %# Update the x data
'YData',get(hMoving,'YData')); %# Update the y data
end
Turn on draggable for both lines, using the above function as the one called when either object is moved:
draggable(h1,#motionFcn);
draggable(h2,#motionFcn);
I've never used it, but there's a submission by Sebastian Hölz called CLINE on the Mathworks File Exchange that seems related.
I don't know how to do exactly what you want, but presumably the reason you want to do this is to have some way of distinguishing this line from other lines. Along those lines, take a look at MathWorks' documentation on 2-D line plots. Specifically, this example:
plot(x,y,'--rs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10)
should give you plenty of ideas for variation. If you really need the two-color dashes, it might help to specify why. That way, even if we can't answer the question, perhaps we can convince you that you don't really need the two-color dashes. Since you've already ruled out the over-lapping solution, I'm fairly certain there's no solution that answers all of your needs. I'm assuming the two-colorness is the most fluid of those needs.