Hovering mouse over multiple plots

15 vues (au cours des 30 derniers jours)
Dharmesh Joshi
Dharmesh Joshi le 17 Mai 2022
Commenté : Dharmesh Joshi le 22 Sep 2023
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:

Réponses (1)

VINAYAK LUHA
VINAYAK LUHA le 22 Sep 2023
Modifié(e) : VINAYAK LUHA le 22 Sep 2023
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:
Regards
Vinayak Luha
  1 commentaire
Dharmesh Joshi
Dharmesh Joshi le 22 Sep 2023
Hi Vinayak
Yes to a degree, but the data should not be displayed on the plot i am hover over, but on the original plot where the data is displayed. In simple terms i am trying to hover my mouse over multple plots at the same time, but in actual fact the mouse would be over only on of the plots.

Connectez-vous pour commenter.

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by