Extract information from a 3-D graph - matlab

I used the plot3 method for creating a 3-D graph. Now I want to extract all the points with z > 0.
How can I do this?

First you need to have the data used to make the plot; if you have them directly, that is the simple case. If not - for example, if you have a plot from some other script, or a figure file saved by someone else that you're just loading - you can get the data from the plot like this:
%# make sure the plot is the current axes object by clicking on it
%# or else use the actual axes handle instead of gca
X = get(gca,'xdata');
Y = get(gca,'ydata');
Z = get(gca,'zdata');
Next, use logical indexing:
index = Z > 0;
X_of_interest = X(index);
Y_of_interest = Y(index);
Z_of_interest = Z(index);
The new variables contain the X,Y,Z values of all points where the condition Z>0 is true.

Related

In Matlab, how to programmatically add datatip to trisurf objects with known vertices index?

Trisurf(f,x,y,z) function can draw a patch object. How can I add datatip to this object at the known vertices index with code?
I tried to use the following codes in 2017b
cursorMode = datacursormode(gcf);
hdtip = cursorMode.createDatatip(h_surf); %h_surf is the handle of trisurf
hdtip.Cursor.Position=pos; %this commond seems cannot find the exact vertex and depends on the view angle, where pos is the vertex coordinates.
hdtip.Cursor.DataIndex=idx; %try to use this command to assign the vertex index, but not successful.DataIndex seems not the vertex index.
Accordint to #Woffie's suggestions, I tuned a little bit to reproduce the error with different view angle in 2017b and 2019b.
First to enable view([180,0]); line. Run the code to get the correct result. And then without closing the figure,comment this line, only remain view([0,0]); to get the wrong result. But if add Close all to the front of the script, should get the correct result each time. (But still don't know the reason, whether it's a bug or not and how to avoid it smartly.)
It might be good to note, in 2D plots, this usually is not a problem since one doesn't usually change the view.
ngrid=1000;%actualy doesnot matter.
x=linspace(-3,3,ngrid+1);
y=linspace(-3,3,ngrid+1);
[x,y] = meshgrid(x,y);
tri = delaunay(x,y);
z = peaks(x,y);
x=(x+3)/6;y=(y+3)/6;
h_surf = trisurf(tri,x,y,z,'EdgeAlpha',0.1);
x0=0.25;y0=0.44;%the above normlization are not necessary but easier to setup the query node.
z0=z(round(y0*ngrid)+1,round(x0*ngrid)+1);
pos=[x0,y0,z0];
view([0,0]);
view([180,0]); %toggle between these two view angles to see the difference.
cursorMode = datacursormode(gcf);
hdtip = cursorMode.createDatatip(h_surf);
hdtip.Cursor.Position = pos;
hdtip.Cursor.Position
BTW, I also figured out how to use hdtip.Cursor.DataIndex=idx. Basically, DataIndex here is the linear index for h_surf.XData(or YData or ZData), while the XData, YData, ZData are mxn matrice as the node/vertex coordinates of all faces in h_surf, where m is the number of nodes in each face and n is the number of the faces. So it will be straightforward to convert to node index into DataIndex.
I'll use the trisurf example from the docs to create a mesh:
[x,y] = meshgrid(1:15,1:15);
tri = delaunay(x,y);
z = peaks(15);
h_surf = trisurf(tri,x,y,z);
Now the tri matrix contains the indices within x, y, and z for each vertex in the mesh. We can choose a vertex index idx, and the corresponding value from each of the x/y/z arrays to use as the .Position property of your data cursor. Contrary to your comment, the Position property does not depend on the view angle, it is the position relative to the axes (and therefore your data).
So
idx = 123; % intex within triangulation "tri"
xcurs = x(tri(idx)); % = 5 in this example
ycurs = y(tri(idx)); % = 6 in this example
zcurs = z(tri(idx)); % = 0.65233 in this example
cursorMode = datacursormode(gcf);
hdtip = cursorMode.createDatatip(h_surf);
hdtip.Cursor.Position = [xcurs,ycurs,zcurs];
You could determine idx however you want, then use a common idx to extract the aligned x, y, and z coordinates.
Two different views to show the data tip stays in place:
This example was run using MATLAB R2017b.

multiple matlab contour plots with one level

I have a number of 2d probability mass functions from 2 categories. I am trying to plot the contours to visualise them (for example at their half height, but doesn't really matter).
I don't want to use contourf to plot directly because I want to control the fill colour and opacity. So I am using contourc to generate xy coordinates, and am then using fill with these xy coordinates.
The problem is that the xy coordinates from the contourc function have strange numbers in them which cause the following strange vertices to be plotted.
At first I thought it was the odd contourmatrix format, but I don't think it is this as I am only asking for one value from contourc. For example...
contourmatrix = contourc(x, y, Z, [val, val]);
h = fill(contourmatrix(1,:), contourmatrix(2,:), 'r');
Does anyone know why the contourmatrix has these odd values in them when I am only asking for one contour?
UPDATE:
My problem seems might be a failure mode of contourc when the input 2D matrix is not 'smooth'. My source data is a large set of (x,y) points. Then I create a 2D matrix with some hist2d function. But when this is noisy the problem is exaggerated...
But when I use a 2d kernel density function to result in a much smoother 2D function, the problem is lessened...
The full process is
a) I have a set of (x,y) points which form samples from a distribution
b) I convert this into a 2D pmf
c) create a contourmatrix using contourc
d) plot using fill
Your graphic glitches are because of the way you use the data from the ContourMatrix. Even if you specify only one isolevel, this can result in several distinct filled area. So the ContourMatrix may contain data for several shapes.
simple example:
isolevel = 2 ;
[X,Y,Z] = peaks ;
[C,h] = contourf(X,Y,Z,[isolevel,isolevel]);
Produces:
Note that even if you specified only one isolevel to be drawn, this will result in 2 patches (2 shapes). Each has its own definition but they are both embedded in the ContourMatrix, so you have to parse it if you want to extract each shape coordinates individually.
To prove the point, if I simply throw the full contour matrix to the patch function (the fill function will create patch objects anyway so I prefer to use the low level function when practical). I get the same glitch lines as you do:
xc = X(1,:) ;
yc = Y(:,1) ;
c = contourc(xc,yc,Z,[isolevel,isolevel]);
hold on
hp = patch(c(1,1:end),c(2,1:end),'r','LineWidth',2) ;
produces the same kind of glitches that you have:
Now if you properly extract each shape coordinates without including the definition column, you get the proper shapes. The example below is one way to extract and draw each shape for inspiration but they are many ways to do it differently. You can certainly compact the code a lot but here I detailed the operations for clarity.
The key is to read and understand how the ContourMatrix is build.
parsed = false ;
iShape = 1 ;
while ~parsed
%// get coordinates for each isolevel profile
level = c(1,1) ; %// current isolevel
nPoints = c(2,1) ; %// number of coordinate points for this shape
idx = 2:nPoints+1 ; %// prepare the column indices of this shape coordinates
xp = c(1,idx) ; %// retrieve shape x-values
yp = c(2,idx) ; %// retrieve shape y-values
hp(iShape) = patch(xp,yp,'y','FaceAlpha',0.5) ; %// generate path object and save handle for future shape control.
if size(c,2) > (nPoints+1)
%// There is another shape to draw
c(:,1:nPoints+1) = [] ; %// remove processed points from the contour matrix
iShape = iShape+1 ; %// increment shape counter
else
%// we are done => exit while loop
parsed = true ;
end
end
grid on
This will produce:

Add space between plots for the plotmatrix function

Is there a way to add some space between the plots for the plotmatrix function? (I would like to label every x and y axis)
You can do it by using a specific output argument in the call to plotmatrix. You can then retrieve the position of each individual axes and modify it (making it smaller).
Example:
clc
clear
rng default
X = randn(50,3);
Y = reshape(1:150,50,3);
%// Use this output argument
[~,AX,~,~,~] = plotmatrix(X,Y);
%// Fetch all the positions and alter them (make axes smaller)
AllPos = get(AX(:),'Position');
AllPos = vertcat(AllPos{:});
NewPos = [AllPos(:,1)+.05 AllPos(:,2)+.05 AllPos(:,3)-.1 AllPos(:,4)-.1]
%// Update the plot
for k = 1:numel(AX)
axes(AX(k))
set(AX(k),'Position',NewPos(k,:))
xlabel(sprintf('Axes %i',k))
end
Outputs the following:
In contrast to the original plot:

How to mark co-ordinates of points in Octave using "plot" command?

I am using the plot command in Octave to plot y versus x values. Is there a way such that the graph also shows the (x,y) value of every coordinate plotted on the graph itself?
I've tried using the help command but I couldn't find any such option.
Also, if it isn't possible, is there a way to do this using any other plotting function?
Have you tried displaying a text box at each coordinate?
Assuming x and y are co-ordinates already stored in MATLAB, you could do something like this:
plot(x, y, 'b.');
for i = 1 : numel(x) %// x and y are the same lengths
text(x(i), y(i), ['(' num2str(x(i)) ',' num2str(y(i)) ')']);
end
The above code will take each point in your graph and place a text box (with no borders) in the format of (x,y) where x and y are the coordinates for all of the points.
Note: You may have to play around with the position of the text boxes, because the above code will place each text box right on top of each pair of co-ordinates. You can play around by adding/subtracting appropriate constants in the first and second parameters of the text function. (i.e. text(x(i) + 1, y(i) - 1, .......); However, if you want something quick and for illustrative purposes, then the above code will be just fine.
Here's a method I've used to do something similar. Generating the strings for the labels is the most awkward part, really (I only had a single number per label, which is a lot simpler)
x = rand(1, 10);
y = rand(1, 10);
scatter(x, y);
% create a text object for each point
t = text(x, y, '');
% generate a cell array of labels - x and y must be row vectors in this case
c = strsplit(sprintf('%.2g,%.2g\n',[x;y]), '\n');
c(end) = []; % the final \n gives us an extra empty cell, remove it
c = c'; % transpose to match the dimensions of t
% assign each label to each text object
set(t, {'String'}, c);
You may want to play around with various properties like 'HorizontalAlignment', 'VerticalAlignment' and 'Margin' to tweak the label positions to your liking.
After a little more thought, here's an alternative, more robust way to generate a suitable cell array of coordinate labels:
c = num2cell([x(:) y(:)], 2);
c = cellfun(#(x) sprintf('%.2g,%.2g',x), c, 'UniformOutput', false);
There is a very useful package labelpoints
in MathWorks File Exchange that does exactly that.
It has a lot of convenient features.

Multiple colormaps using bar(z,'stacked')

I have read the article recommended by a post on another colormap thread http://www.mathworks.com/matlabcentral/answers/101346 and I understand the concept. I am having trouble understanding the values of CDATA when using the bar(z,'stacked') function.
I have one figure with a major axis plotted using cmap, and I have created and positioned a new axis for the bar chart and I want it to use cmap2.
For example, my code includes:
maps = colormap([cmap;cmap2]);
bH = bar(z,'stacked');
Where z = 25x10 (annual data for 10 years over 25 sites)
Now when I look at the CDATA
get(bH,'CDATA') A cell array is returned of size 1x10 with each cell containing the string 'scaled'.
Now if I look at the CDATA of each of the children
childH = get(bH,'children');
get(childH{i},'CDATA')
A matrix of size 25x10 is returned with every value equal.
e.g. childH{i}'s CDATA is a matrix of size 25x10 having all values = i
So how can I scale these to map to my colormap since
from the documentation above I need to perform:
m = size(colormap,1); % Number of colors in the current colormap
Data = get(H,'CData') % Where H is a handle to a surface or patch object
cmin = min(CData(:)); % Minimum color value
cmax = max(CData(:)); % Maximum color value
idx = min(m,round((m-1)*(CData-cmin)/(cmax-cmin))+1);
idx becomes min(m,nan) which is always m?
I really need help understanding this.
Am I missing something or is this function a special case?
First make sure that cmap2 has exactly the number of colors that you want to use, then get the barseries object to map into it directly. Something like:
childH = get(bH, 'children');
for a = 1:numel(childH)
C = get(childH{a}, 'FaceVertexCData');
C(:) = a+size(cmap, 1);
set(childH{a}, 'FaceVertexCData', C, 'CDataMapping', 'direct');
end