How to auto adjust an Axes to fit a 3D plot in Matlab GUI
10 views (last 30 days)
Show older comments
Hi,
I have an axes object in my GUI to which I'm filling a 3D plot during run time.
However, this 3D plot is not properly displayed within the axes and I suspect it happens due to predetermined size of the axes object I put in my GUI (please see the code and the result I get below).
axes(handles.axes3);
plot3(Y1(1,:),Y1(2,:),Y1(3,:),'g', 'Marker','o');
hold on;
plot3(Y2(1,:),Y2(2,:),Y2(3,:),'r', 'Marker','o');
The same plot is displayed properly when I plot it in a new figure with the "figure" command (please see the code and the result below).
figure
plot3(Y1(1,:),Y1(2,:),Y1(3,:),'g', 'Marker','o');
hold on;
plot3(Y2(1,:),Y2(2,:),Y2(3,:),'r', 'Marker','o');
I tried all axis commands (eg:- axis square, axis normal, etc), but none of them worked. I also noticed that the DataAspectRatio is different in these two axes objects when I used get(gca,'DataAspectRatio') .
Any help on solving this issue is greatly appreciated.
Thanks in advance...
2 Comments
Benjamin Avants
on 13 May 2014
Edited: Benjamin Avants
on 13 May 2014
Do you have the plot tools turned on in your GUI? Specifically rotate, pan, and the zoom buttons? If not, add them to the GUI via Tools... Toolbar Editor. Then try moving the plot around to get a better idea what is happening. It looks like the lines connecting your data points are not being drawn properly but its hard to tell why from your screen cap.
Also, the LLE2 axis is not sized the same between the two plots and I suspect there are differences in the LLE3 axis as well.
Answers (1)
Benjamin Avants
on 15 May 2014
One potential way to solve this in a general case where the data in the 3D plot will be different for each use would be to generate a new figure with the 3D plot and use its parameters to set the parameters of the embedded axis. This is assuming that the new plot looks the way you would like.
You can generate the new figure with its visible property set to 'off' so that it does not appear on the screen.
Maybe something like this...
f = figure('Visible','off');
plot3(...) % Ellipses '...' are used as place holders for code in this example
a = get(f,'Children');
myXLim = get(a,'XLim');
myYLim = get(a,'YLim');
...
plot3(handles.myAxis,...)
set(handles.myAxis,'XLim',myXLim,'YLim',myYLim,...);
delete(f);
I think you've touched on most of the properties that are likely to affect the display but here are the ones that come to mind for me:
X, Y, & Z Lim; DataAspectRatio; PlotBoxAspectRatio; CameraPosition; & CamerViewAngle
I would suggest looking up axes properties in the MATLAB documentation.
Within the tolerance of your GUI, you can also change the size of the embedded plot as follows:
position = get(handles.myAxis,'Position');
position2 = get(a,'Position');
position(3:4) = position2(3:4);
set(handles.myAxis,'Position',position);
This method assumes that the new plot and your axis are close to the same size and that they both use the same units for positioning (not normalized, but Characters or Pixels for instance). This might prevent some of the cropping you've seen in your GUI axis.
If you are likely to be plotting the same (or very similar) data in your GUI, you could just play around with the settings until the display looks correct and then hard code them into your program. While your GUI is running, you can query your axis' current properties with its handle.
myAxis = findall(0,'Tag','TheTagISetForMyAxis'); % Get the handle
get(myAxis) % Query and display the properties
You can also set the properties manually in the same way using the handle and set() function.
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!