Mouseover highlighting of plotted line?
조회 수: 41 (최근 30일)
이전 댓글 표시
The 'parallelplot' function has a very handy mouseover function that highlights the proximate line in the plot and displays its parameters automatically in a datatip. I had to write my own parallel coordinate plot function due to parallelplot's limited color control, line sorting and label control but the mouseover feature is a bridge too far for me. Can anyone please give me direction to accomplish this for the plain 'plot' function?
Thanks!
댓글 수: 0
채택된 답변
Abhilash Padma
2019년 7월 19일
Hi,
I understand that you wanted to implement the mouseover functionality for plotted lines.
MATLAB's pointer manager lets you assign functions to be called whenever the mouse pointer enters, traverses or exits any graphics object. I included an example here where a line changes to another color when mouse passes through that line.
% ======================================================================= %
% Your code with a few additions:
% Save the figure handle for later
hf = figure;
x = linspace(-3,3);
y = (x/5-x.^3).*exp(-2*x.^2);
hp=plot(x,y, 'tag', 'rollover','color','black');
% ======================================================================= %
% Define a pointer behavior struct containing the fields 'enterFcn',
% 'exitFcn', and 'traverseFcn'. These fields should contain function
% handles or an empty matrix ([]). MATLAB will call these functions when
% the mouse pointer first moves over an object ('enterFcn'), when the mouse
% pointer moves off of an object ('exitFcn') and when the mouse pointer
% moves across and object ('traverseFcn'). MATLAB passes two arguments when
% it calls these functions: the figure handle, and the cursor position.
% I've defined the enterFcn to find the object with the 'rollover' tag (the
% text box) and change it's string value to something more informative...
pointerBehavior.enterFcn =@(hfig, cpp)set(findobj(hfig, 'tag', 'rollover'), ...
'color', 'red');
% I don't care about what happens when the mouse moves across the text, so
% I'll leave the traverseFcn blank...
pointerBehavior.traverseFcn = [];
% The exitFcn is similar to the enterFcn, but it changes the string back to
% the shorter version...
pointerBehavior.exitFcn = ...
@(hfig, cpp)set(findobj(hfig, 'tag', 'rollover'), ...
'color', 'black');
% Now, I need to link the pointer behavior to the object (the text box):
iptSetPointerBehavior(hp, pointerBehavior);
% Now, I need to enable pointer management for the figure:
iptPointerManager(hf, 'enable');
% function changeColor(hfig, cpp)
% set(findobj(hfig, 'tag', 'rollover'), ...
% 'color', 'red');
% end
댓글 수: 2
cliffy
2019년 8월 3일
Hi Abhilash,
I'm using the pointerBehavior function. It works, it can show some more text when I move my cursor to a textbox in the figure. I wonder if you have encounter the problem of several objects i.e. when move the cursor over different objects, different text will show up. Thank you! And does the object have to be a textbox, or could it be some plots?
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Exploration에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!