Hovering mouse over multiple plots
15 次查看(过去 30 天)
显示 更早的评论
Hi All
I have multiple subplots, all with the same time stamp.
Is it possible that when i hover over one plot, i can also see the values from another plot at the same time as if i the mouse over that plot as well?
This how my plot looks:
0 个评论
回答(1 个)
VINAYAK LUHA
2023-9-22
编辑:VINAYAK LUHA
2023-9-22
Hi Dharmesh,
It is my understanding that you have multiple subplots plotted at same timestamps in a figure. While hovering over line points in a subplot, you want to know a workaround to see the corresponding y value in another subplot at the same x values.
Here’s a possible workaround for your reference using callbacks function which gets triggered on hovering over line points and leverages MATLAB Graphics object’s hierarchical relationships to find and display y values as tooltips in the subplot from another configured subplot.
close all
time = 7:16;
data1= randi([1,50],1,10)
data2= randi([51,100],1,10)
figure;
subplot(2, 1, 1);
plot(time, data1);
title('Plot 1');
subplot(2, 1, 2);
plot(time, data2);
title('Plot 2');
dcm_obj = datacursormode(gcf);
set(dcm_obj, 'UpdateFcn',@callbackfcn);
function output_txt = callbackfcn(~, event_obj)
pos = event_obj.Position;
x = pos(1);
% fetch y values from other subplot using child-parent relationship
y1 = interp1(event_obj.Target.XData, event_obj.Target.YData, x);
y2 = interp1(event_obj.Target.Parent.Parent.Children(1).Children.XData, event_obj.Target.Parent.Parent.Children(1).Children.YData, x);
% display data as tooltip
output_txt = {['X: ', num2str(x)], ['Y1: ', num2str(y1)], ['Y2: ', num2str(y2)]};
end
Result
Explore the following documentations for more details:
datacursormode - https://in.mathworks.com/help/matlab/ref/matlab.graphics.shape.internal.datacursormanager.html
Regards
Vinayak Luha
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!