Unwanted line shows up in matlab 3d triplet line - matlab

I am plotting 3d lines using MATLAB's plot3 command.
I have arranged my data into X, Y, Z triplets to run through the function as such:
Where points is an array with 6 columns.
The 1st three columns in row one are the coordinates of the i'th node of the 1st element. Similarly the 2nd three columns are the j'th node's coordinates.
points =
[x1 y1 z1 x2 y2 z2]
0 0 0 60 0 0
60 0 0 90 0 0
60 0 30 60 0 0
60 0 30 60 30 30
60 30 30 60 30 0
plot3 ( [x1 ;x2] , [y1 ; y2] , [z1 ; z2], '-or')
With some other processing my result is this:
Looking closely at the points table, there is no such line that goes from between points [60 0 0] and [60 30 30]
Does anyone have any advice for how I might remove this unsightly artefact?
Note: The analysis using these elements and nodes are not affected, I suspect it is purely graphical.

Yes, you are correct. It is graphical.
The way MATLAB plots points is that it connects a line between successive points. Therefore, it joins a line between points 1 and 2, then 2 and 3, 3 and 4, etc.
If you want to achieve what you want, so without the diagonal line, you'll need to plot the lines in a particular order. What I would do is first plot the line parallel to the z = 0 axis, then draw the square shape after.
%// Draw parallel line
plot3([90 0], [0 0], [0 0], '-or');
%// Draw square shape
x = [60 60 60 60];
y = [0 0 30 30];
z = [0 30 30 0];
hold on;
plot3(x, y, z, '-or');
axis ij; %// Invert y axis
Take a look at how I defined the square shape. I started from the left of the shape, then traced around in a clockwise manner. I first plot the parallel line, then the square shape after.
This is what I get:

I am trying to plot a system of links that are between nodes, connections are made at nodes, both links and nodes are defined explicitly. For example, one must have nodes1 N1 and N2 to create a link L1(N1,N2). I form this information into an array where row1 = [N1 N2] where N contains a 3 column vector with the cartesian coordinate. I have partitioned out the X,Y,Z vectors from this matrix and attempted to pass them all together into the plot3 command. As shown in a simple case, this lead to the plot of another line segment that was unintentional (frankly I am still not clear as to why). The surefire way that I have found is by looping over the X,Y,Z vectors and plotting the segments individually.
hold on
for i = 1:length(x1)
plot3 ( [x1(i) ;x2(i)] , [y1(i) ; y2(i)] ,[z1(i);z2(i)], '-or')
end
hold off

Related

Find contour/edge in pcolor in Matlab

I'm trying to make a contour that follows the edges of the 'pixels' in a pcolor plot in Matlab. This is probably best explained in pictures. Here is a plot of my data. There is a distinct boundary between the yellow data (data==1) and the blue data (data==0):
Note that this is a pcolor plot so each 'square' is essentially a pixel. I want to return a contour that follows the faces of the yellow data pixels, not just the edge of the yellow data.
So the output contour (green line) passes through the mid-points of the face (red dots) of the pixels.
Note that I don't want the contour to follow the centre points of the data (black dots), which would do something like this green line. This could be achieved easily with contour.
Also, if it's any help, I have a few grids which may be useful. I have the points in the middle of the pixels (obviously, as that's what I've plotted here), I also have the points on the corners, AND I have the points on the west/east faces and the north/south faces. IF you're familiar with Arakawa grids, this is an Arakawa-C grid, so I have the rho-, u-, v- and psi- points.
I've tried interpolation, interweaving grids, and a few other things but I'm not having any luck. Any help would be HUGELY appreciated and would stop me going crazy.
Cheers, Dave
EDIT:
Sorry, I simplified the images to make what I was trying to explain more obvious, but here is a larger (zoomed out) image of the region I'm trying to separate:
As you can see, it's a complex outline which heads in a "southwest" direction before wrapping around and moving back "northeast". And here is the red line that I'd like to draw, through the black points:
You can solve this with a couple of modifications to a solution I posted to a related question. I used a section of the sample image mask in the question for data. First, you will need to fill the holes in the mask, which you can do using imfill from the the Image Processing Toolbox:
x = 1:15; % X coordinates for pixels
y = 1:17; % Y coordinates for pixels
mask = imfill(data, 'holes');
Next, apply the method from my other answer to compute an ordered set of outline coordinates (positioned on the pixel corners):
% Create raw triangulation data:
[cx, cy] = meshgrid(x, y);
xTri = bsxfun(#plus, [0; 1; 1; 0], cx(mask).');
yTri = bsxfun(#plus, [0; 0; 1; 1], cy(mask).');
V = [xTri(:) yTri(:)];
F = reshape(bsxfun(#plus, [1; 2; 3; 1; 3; 4], 0:4:(4*nnz(mask)-4)), 3, []).';
% Trim triangulation data:
[V, ~, Vindex] = unique(V, 'rows');
V = V-0.5;
F = Vindex(F);
% Create triangulation and find free edge coordinates:
TR = triangulation(F, V);
freeEdges = freeBoundary(TR).';
xOutline = V(freeEdges(1, [1:end 1]), 1); % Ordered edge x coordinates
yOutline = V(freeEdges(1, [1:end 1]), 2); % Ordered edge y coordinates
Finally, you can get the desired coordinates at the centers of the pixel edges like so:
ex = xOutline(1:(end-1))+diff(xOutline)./2;
ey = yOutline(1:(end-1))+diff(yOutline)./2;
And here's a plot showing the results:
imagesc(x, y, data);
axis equal
set(gca, 'XLim', [0.5 0.5+size(mask, 2)], 'YLim', [0.5 0.5+size(mask, 1)]);
hold on;
plot(ex([1:end 1]), ey([1:end 1]), 'r', 'LineWidth', 2);
plot(ex, ey, 'k.', 'LineWidth', 2);
Take a look at the following code:
% plotting some data:
data = [0 0 0 0 0 0 1 1
0 0 0 0 0 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 0 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1];
p = pcolor(data);
axis ij
% compute the contour
x = size(data,2)-cumsum(data,2)+1;
x = x(:,end);
y = (1:size(data,1));
% compute the edges shift
Y = get(gca,'YTick');
y_shift = (Y(2)-Y(1))/2;
% plot it:
hold on
plot(x,y+y_shift,'g','LineWidth',3,'Marker','o',...
'MarkerFaceColor','r','MarkerEdgeColor','none')
It produces this:
Is this what you look for?
The most important lines above is:
x = size(data,2)-cumsum(data,2)+1;
x = x(:,end);
which finds the place of shifting between 0 to 1 for every row (assuming there is only one in a row).
Then, within the plot I shift y by half of the distance between two adjacent y-axis tick, so they will be placed at the center of the edge.
EDIT:
After some trials with this kind of data, I have got this result:
imagesc(data);
axis ij
b = bwboundaries(data.','noholes');
x = b{1}(:,1);
y = b{1}(:,2);
X = reshape(bsxfun(#plus,x,[0 -0.5 0.5]),[],1);
Y = reshape(bsxfun(#plus,y,[0 0.5 -0.5]),[],1);
k = boundary(X,Y,1);
hold on
plot(X(k),Y(k),'g','LineWidth',3,'Marker','o',...
'MarkerFaceColor','r','MarkerEdgeColor','none')
It's not perfect, but may get you closer to what you want in a more simple approach:
OK, I think I've solved it... well close enough to be happy.
First I take the original data (which I call mask_rho and use this to make masks mask_u, mask_v, which is similar to mask_rho but is shifted slightly in the horizontal and vertical directions, respectively.
%make mask_u and mask_v
for i = 2:size(mask_rho,2)
for j = 1:size(mask_rho,1)
mask_u(j, i-1) = mask_rho(j, i) * mask_rho(j, i-1);
end
end
for i = 1:size(mask_rho,2)
for j = 2:size(mask_rho,1)
mask_v(j-1, i) = mask_rho(j, i) * mask_rho(j-1, i);
end
end
I then make modified masks mask_u1 and mask_v1 which are the same as mask_rho but averaged with the neighbouring points in the horizontal and vertical directions, respectively.
%make mask which is shifted E/W (u) and N/S (v)
mask_u1 = (mask_rho(1:end-1,:)+mask_rho(2:end,:))/2;
mask_v1 = (mask_rho(:,1:end-1)+mask_rho(:,2:end))/2;
Then I use the difference between the masks to locate places where the masks change from 0 to 1 and 1 to 0 in the horizontal direction (in the u mask) and in the vertical direction (in the v mask).
% mask_u-mask_u1 gives the NEXT row with a change from 0-1.
diff_mask_u=logical(mask_u-mask_u1);
lon_u_bnds=lon_u.*double(diff_mask_u);
lon_u_bnds(lon_u_bnds==0)=NaN;
lat_u_bnds=lat_u.*double(diff_mask_u);
lat_u_bnds(lat_u_bnds==0)=NaN;
lon_u_bnds(isnan(lon_u_bnds))=[];
lat_u_bnds(isnan(lat_u_bnds))=[];
%now same for changes in mask_v
diff_mask_v=logical(mask_v-mask_v1);
lon_v_bnds=lon_v.*double(diff_mask_v);
lon_v_bnds(lon_v_bnds==0)=NaN;
lat_v_bnds=lat_v.*double(diff_mask_v);
lat_v_bnds(lat_v_bnds==0)=NaN;
lon_v_bnds(isnan(lon_v_bnds))=[];
lat_v_bnds(isnan(lat_v_bnds))=[];
bnd_coords_cat = [lon_u_bnds,lon_v_bnds;lat_u_bnds,lat_v_bnds]'; %make into 2 cols, many rows
And the result grabs all the coordinates at the edges of the boundary:
Now my answer goes a bit awry. If I plot the above vector as points plot(bnd_coords_cat(:,1),bnd_coords_cat(:,2),'kx' I get the above image, which is fine. However, if I join the line, as in: plot(bnd_coords_cat(:,1),bnd_coords_cat(:,2),'-' then the line jumps around, as the points aren't sorted. When I do the sort (using sort and pdist2) to sort by closest points, Matlab sometimes chooses odd points... nevertheless I figured I'd include this code as an appendix, and optional extra. Someone may know a better way to sort the output vectorbnds_coords_cat:
% now attempt to sort
[~,I]=sort([lon_u_bnds,lon_v_bnds]);
bnd_coords_inc1 = bnd_coords_cat(I,1);
bnd_coords_inc2 = bnd_coords_cat(I,2);
bnd_coords = [bnd_coords_inc1,bnd_coords_inc2];
bnd_coords_dist = pdist2(bnd_coords,bnd_coords);
bnd_coords_sort = nan(1,size(bnd_coords,1));
bnd_coords_sort(1)=1;
for ii=2:size(bnd_coords,1)
bnd_coords_dist(:,bnd_coords_sort(ii-1)) = Inf; %don't go backwards?
[~,closest_idx] = min(bnd_coords_dist(bnd_coords_sort(ii-1),:));
bnd_coords_sort(ii)=closest_idx;
end
bnd_coords_final(:,1)=bnd_coords(bnd_coords_sort,1);
bnd_coords_final(:,2)=bnd_coords(bnd_coords_sort,2);
Note that the pdist2 method was suggested by a colleague and also from this SO answer, Sort coordinates points in matlab. This is the final result:
To be honest, plotting without the line is fine. So as far as I'm concerned this is close enough to be answered!

Translating 2D image with RGB colours along axes

I am trying to create my own voronoi diagram. I have an arbitrary shape defined by their x and y coordinates stored in separate vectors. Inside this shape are some points of interest (with known coordinates) that belong to two different groups and act as seeds to the voronoi diagram. As an example, the whole diagram ranges from x=-10 to x=90 and y=-20 to y=60. The boundary shape is not rectangular but falls within the axes range above.
What I have done so far is to create a 3D matrix (100 X 80 X 3), C, with all ones so that the default colour will be white. I then looped through from i=1:100, j=1:80, testing individual pixels to see if they fall within the shape using inpolygon. If they do, I then find out which point is the pixel closest to and assign it a colour based on whether the closest point belongs to group 1 or 2.
All is good so far. I then used imagesc to display the image with a custom axis range. The problem is that the voronoi diagram has the general shape but it is off in terms of position as the pixel coordinates are different from the actual world coordinates.
I tried to map it using imref2d but I do not know how it really works or how to display the image after using imref2d. Please help me on this.
I am open to other methods too!
Thank you!
Edit:
As requested, let me give a more detailed example and explanation of my problem.
Let us assume a simple diamond shape boundary with the following vectors and 4 points with the following coordinate vectors:
%Boundary vectors
Boundary_X = [-5 40 85 40 -5];
Boundary_Y = [20 50 20 -10 20];
%Point vectors
Group_One_X = [20 30];
Group_One_Y = [10 40];
Group_Two_X = [50 70];
Group_Two_Y = [5 20];
Next I plot all of them, with different groups having different colours.
%Plot boundary and points
hold on
plot(Boundary_X,Boundary_Y)
scatter(Group_One_X,Group_One_Y,10,'MarkerFaceColor','Black',...
'MarkerEdgeColor','Black')
scatter(Group_Two_X,Group_Two_Y,10,'MarkerFaceColor','Red',...
'MarkerEdgeColor','Red')
hold off
axis([-10, 90, -20, 60])
This is the result:
Boundary with points
Next I test the whole graph area pixel by pixel, and colour them either cyan or yellow depending on whether they are closer to group 1 or 2 points.
%Create pixel vector with default white colour
C=ones(100,80,3);
Colour_One = [0 1 1];
Colour_Two = [1 1 0];
%Loop through whole diagram
for i=1:100
for j=1:80
x=i;
y=j
if inpolygon(x,y,Boundary_X,Boundary_Y)
%Code for testing which point is pixel closest to
%If closest to group 1, assign group 1 colour, else group 2
%colour
end
end
end
%Display image
hold on
imagesc(C)
hold off
This is the result
Failed Voronoi Diagram
The shape is somewhat correct for the right side but not for the others. I understand that this because my world coordinates start from negative values but the pixel coordinates start from 1.
Hence I am at a lost as to how can I solve this problem.
Thank you!
One thing to notice is that you have to convert between image and plot coordinates. The plot coordinates are (x,y) where x goes to the right, and y goes up. The matrix coordinates are (i,j) where i goes down, and j to the right. An easy way to do this is with the use of vec_X,vec_Y as shown below.
Another solution for the newer Matlab versions would be - as you said - using imref2d but unfortunately I have no experience with that command.
%Boundary vectors
Boundary_X = [-5 40 85 40 -5];
Boundary_Y = [20 50 20 -10 20];
%Point vectors
Group_One_X = [20 30];
Group_One_Y = [10 40];
Group_Two_X = [50 70];
Group_Two_Y = [5 20];
%Coordinate system
min_X = -10;
max_X = 90;
min_Y = -20;
max_Y = 60;
axis([min_X, max_X, min_Y, max_Y])
%Create pixel vector with default white colour
rows_N = 100;
columns_N = 80;
C=ones(rows_N,columns_N,3);
%These vectors say where each of the pixels is in the plot coordinate
%system
vec_X = linspace(min_X,max_X,columns_N);
vec_Y = linspace(min_Y,max_Y,rows_N);
Colour_One = [0 1 1];
Colour_Two = [1 1 0];
%Loop through whole diagram
for i=1:100
for j=1:80
if inpolygon(vec_X(j),vec_Y(i),Boundary_X,Boundary_Y)
%calculate distance to each point
Distances_One = zeros(size(Group_One_X));
Distances_Two = zeros(size(Group_Two_X));
for k=1:numel(Group_One_X);
Distances_One(k) = norm([Group_One_X(k),Group_One_Y(k)]-[vec_X(j),vec_Y(i)]);%assuming euclidean norm, but can be adjusted to whatever norm you need
end
for k=1:numel(Group_Two_X);
Distances_Two(k) = norm([Group_Two_X(k),Group_Two_Y(k)]-[vec_X(j),vec_Y(i)]);%assuming euclidean norm, but can be adjusted to whatever norm you need
end
if min(Distances_One) < min(Distances_Two);
C(i,j,:) = Colour_One;
else
C(i,j,:) = Colour_Two;
end
end
end
end
%Display image
imagesc(vec_X,vec_Y,C) %lets you draw the image according to vec_X and vec_Y
%Plot boundary and points
hold on
plot(Boundary_X,Boundary_Y)
scatter(Group_One_X,Group_One_Y,10,'MarkerFaceColor','Black',...
'MarkerEdgeColor','Black')
scatter(Group_Two_X,Group_Two_Y,10,'MarkerFaceColor','Red',...
'MarkerEdgeColor','Red')
hold off

How do I change the number of ticks in matlab histogram and change axis numbers font size

I only want
the x-axis to display 1 2 3 4 5 6
the y-axis to display 0 20 40 60 80 100
change the numbers font size to 14
I've tried by setting different axis property (ref. to the commented lines of code in the script below) however none of them affect the graph.
%Code to generate the diceSum
DiceSum = myDiceRoller(1,500);
figure(1)
%Create the histogram
hist(DiceSum,NDice*6)
%Label the axes
xlabel('Value of Roll','FontSize',16)
ylabel('Number of Times Rolled','FontSize',16)
%set(gca,'X','FontSize',14)
%set(gca,'YTickLabel',{'0' ;'100'})
%set('xtick','FontSize',14)
%set('Xlim',[0,6], 'Ylim',[0 ,100])
%set('xtick',[0:1:6],'ytick',[0:20:100])
%set(gca,'XLim',[0 6])
%set(gca,'XTick',[0 1 2 3 4 5 6])
%set(gca,'XTickLabel',str2mat{'0','1','2','3','4','5','6')
%xlim([0 6])
This is a separate function I'm using to create the data and the histogram
function [DiceSum] = myDiceRoller(NDice,NRolls)
DiceSum = zeros(1,NRolls);%
for i = 1:NRolls;% on roll 1...roll 2
for j = 1:NDice;% on roll 1 , roll #s of die
n = ceil(rand(1)*6);
DiceSum(1,i) = DiceSum(1,i) + n;
end
hist(DiceSum,NDice*6)
xlabel('Value of Roll')
ylabel('Number of Times Rolled')
end
To have the x-axis displaying 1 2 3 4 5 6 you have to two possibilities:
to change the way you call the hist function as follows:
% hist(DiceSum,1:NDice*6)
hist(DiceSum,1:6)
this because in the call to hist with 2 parameters, the second one should be a vector, in that case, hist returns the distribution of Y among length(x) bins with centers specified by x (being x the second parameter) - R2012b hist help
to directly set the x-axis xtick as follows:
set(gca,'xtick',[0:6])
To have the y-axis displaying 0 20 40 60 80 100 you have to set the y-axis ytick as follows:
set(gca,'ytick',[0:20:100])
To change the x and y axis tick font size to 14 you have to set the axis fontsize as follows:
set(gca,'FontSize',14)
Hope this helps.

Reconstruct 3D graph with surf in matlab?

I usually use surf function to plot 3D figures in matlab, but now the data is different, so I am using plot3 and I have the below figure. Do you have any idea how I reconstruct this figure to be more understandable even if by using different function.
To be more concise, I have X values, with each X value there is a value of Y and value of Z.
X = [ 1 ;2 ;4; 8; 16; 32; 64];
Z = [ 1; 1.8 ; 3.46 ; 6.74 ; 13.18 ; 24.34 ; 39.33]
Y = [0 ; 56.92 ; 91 ; 109.95 ; 119 ; 123.57 ; 125.51]
fig = plot3(log(X),Y,Z,'b.-');
XLABEL=[ 1 2 4 8 16 32 64];
set(gca,'XTickLabel',XLABEL);
set(gca,'XTick',log(XLABEL));
YLABEL= [ 0 30 60 90 120 150 180];
set(gca,'YTickLabel',YLABEL);
set(gca,'YTick',YLABEL);
ZLABEL= [0 5 10 15 20 25 30 35 40 45 50 55];
set(gca,'ZTickLabel',ZLABEL);
set(gca,'ZTick',(ZLABEL));
ylim([0 180]);
zlim([0,55]);
grid on
It's difficult to say, because we don't have a context. Common options are:
Plotting x/y and x/z in two separate plots. Precisely readable but difficult to get the connection between y and z. subplot
Plotyy, same as previous but in one plot. Y and Z values which correspond to the same x-value are aligned. plotyy
Use a plot3 as shown above, but connect each point to the x/z plane. (details below)
Project the line on one or multiple planes and draw it there. (Plot the line again, setting x, y or z to 7 0 or 180, which is the location of your axis)
If two axis are of major importance, use a simple 2d plot and represent the third dimension using color/dotsize/annotations etc...
Code for Option 3:
At the end of your code, add the following code:
X2=[X';X';nan(size(X'))];
X2=X2(:);
Y2=[Y';Y';nan(size(Y'))];
Y2=Y2(:);
Z2=[Z';zeros(size(Z'));nan(size(Z'))];
Z2=Z2(:);
hold on
plot3(log(X2),Y2,Z2,'--')
To understand it, you have to know that matlab skips nans while plotting. Thus the code above generates a independent line segment for each point, connecting it to the ground plane.

MATLAB Matrix step plot

I have this problem that I should plot a step plot from a matrix. For example:
[0 0 10 20 50;
50 100 100 300 50]
The second line should be the x-axis so there would be points at 50, 150, 250, 550 and 600. And the according y-values should be 0, 0, 10, 20 and 50. The function stairs(B(1,:)) gives me a step plot but it's someway off. I'd appreciate the help!
stairs can take in two sets of values, your x and your y.
So the first issue is that you need to define both x and y;
y = B(1,:);
x = B(2,:);
The second is that your second line is the steps along x not the actual values, and stairs needs. So we need to change your x values, using cumsum which performs a cumulative sum. Since we have a couple of points with y=0, as well as calling stairs with two inputs I'm adding some LineSpec options to ensure those points are visible.
x = cumsum(x);
stairs(x,y, '-.xk');
The last point may be a little difficult to see, so you may want to adjust the axis:
xlim([0 700])
ylim([0 60])