Show line above surf plot in 2D view

조회 수: 9 (최근 30일)
Jacob
Jacob 2014년 8월 6일
댓글: Ben11 2014년 8월 11일
Hello,
I'm trying to modify ginput such that it will allow the following:
I have a 2D matrix. I plot the values in an axes using "surf" with a custom colormap. This part works.
Click a point on the graph. Move the mouse. Draw a line, connecting the first point click to the current position of the mouse.
The problem is, even with my efforts to draw the line in front, it shows up hidden behind my plot.
This is the code that executes in ginputmodded after the first click:
if isempty(out1)
lineconnect = line('LineWidth',5, ...
'XData',[pt(1,1),pt(1,1)], 'YData',[pt(1,2),pt(1,2)])
v = allchild(gca)
vsize = length(v);
lineposinv = find(v == lineconnect)%index of lineconnect in v
uistack(lineconnect,'up', lineposinv-1)%move lineconnect to front
v = allchild(gca)
linestore(lineconnect);
end
The following executes when the mouse is moved (WindowButtonMotionFcn):
function dummy()
pts = pointstore;
if ~isempty(pts) %block only executes after a point is clicked
lineconnect = linestore();
cp = get(gca,'CurrentPoint');
set(lineconnect,'XData',[pts(1,1),cp(1,1)],...
'YData',[pts(1,2),cp(1,2)]);
v = allchild(gca)
vsize = length(v);
lineposinv = find(v == lineconnect)%index of lineconnect in v
uistack(lineconnect,'up', lineposinv-1)%move to front
v = allchild(gca)
linestore(lineconnect);
end
end
linestore and pointstore are helper functions that store a persistent variable. They work fine.
Any ideas how I could make this work properly? I'm using MATLAB R2014a on Windows 7.

채택된 답변

Jacob
Jacob 2014년 8월 8일
I crossposted this on StackOverflow and got a solution from user patrik. Thought I'd share it here in case anyone else comes looking. Turns out using surf with view(2) still preserves the z-axis. So I just plotted the connecting line with 'ZData' at 2^16, guaranteed larger than any of my plot values. Works splendidly.
"Ok, I am not sure this solves the problem, but it likely will. The thing with 2D view: This is only another perspective of the plot. The data will still have 3D coordinates. I am not entirely sure, but if I recall correctly, ginput gives the coordinates in xy. This means that if you want to display a line above the surf plot you need to set its z-coordinate to larger than maximum surf value."

추가 답변 (1개)

Ben11
Ben11 2014년 8월 6일
You could try to inverse the order of your axes' children after drawing the line, so that it comes up at the front and the plot stays at the back:
set(gca,'children',flipud(get(gca,'children')));
I can't test it now though so it might very well not work in your case :)
  댓글 수: 1
Jacob
Jacob 2014년 8월 6일
Nope, didn't work, the line still appears behind the plot.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by