How to auto adjust an Axes to fit a 3D plot in Matlab GUI

13 次查看(过去 30 天)
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 个评论
Benjamin Avants
Benjamin Avants 2014-5-13
编辑:Benjamin Avants 2014-5-13
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.
Randima Hettiarachchi
Hi Benjamin,
Thanks a lot for your response. Yes, you are right, I checked the 3D rotate view of the plot with the command "rotate3d on" and when I rotate the plot, a view almost similar to the one given in second figure above can be seen. I also noticed that the plot is cropped from corners for some rotated views. I guess this issue has something to do with the DataAspectRatio or PlotBoxRatio, because as you mentioned some axes are scaled differently in the two plots given above. But I could not find a way to fix it yet.

请先登录,再进行评论。

回答(1 个)

Benjamin Avants
Benjamin Avants 2014-5-15
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.
  1 个评论
Randima Hettiarachchi
Thanks a lot for your response. I already tried to get the parameters from the new figure and set those for the axes object in GUI. It didn't go so well, but I did not play around with all the properties as you have suggested (I only tried DataAspectRatio and PlotBoxAspectRatio). So I will give it a try. I will also try the position properties as you mentioned.
I just noticed that "axis tight" command gives a slightly better result. It is not exactly what I expected, but acceptable.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by