Plotting a 1D function by hovering on a 2D plot

4 views (last 30 days)
Let's say I have a 2D surface plot of function x and y. So z is a function of x and y. I also have a variavle F which is a function of x, y and v. What I want to do is to have a tool where I hover on the 2D plot (x, y, z) and based on the x and y co-ordinates, it should update another 1D plot that is F vs v at that (x, y).
How to make this tool?
close all;
[x, y] = meshgrid(linspace(1, 100, 100), linspace(1, 100, 100));
z = rand(100, 100);
figure; surface(x, y, z); shading flat; colorbar; % I want to hover on this plot
% example of the 1D plot
v = linspace(-15, 15, 128);
for i = 1:100
for k = 1:100
F(i, k, :) = rand(1, 128);
x_indx = 30;
y_indx = 40;
F_squeeze = squeeze(F(x_indx, y_indx, :));
figure; plot(v, F_squeeze);

Accepted Answer

Matt on 19 Oct 2022
You can use impixelinfoval to get the position of your cursor and a callback function to update the figure
close all
img = rand(50);
F = rand(50);
h = figure('WindowButtonDownFcn',@button_down);% update when clicking
ax1 = subplot(1,2,1)
h_image = imagesc(ax1,img);
ax2 = subplot(1,2,2)
% htool = impixelinfo(h,h_image)
htool = impixelinfoval(h,h_image)
function button_down(src, event)
htool = getappdata(src,'htool')
Position = htool.String;
ax2 = getappdata(src,'ax2');
F = getappdata(src,'F');
% instead of plotting random data you can extract cursor position from Position and use
% it to extracxt the part of F you want to plot

More Answers (1)

John D'Errico
John D'Errico on 19 Oct 2022
We all want things we cannot easily have. For example, I would very much want to see world peace. Will I ever see it? Probably not, as much as I might want it. Lacking that, how about a nice red Lamborghini for X-mas? Not gonna happen either. ;-)
Seriously, there is no tool that will do what you want in MATLAB. However, you could probably write it, with some degree of effort. You would need to set up callbacks on the plot, so that when you click on it, your code will see where you clicked, and recover the (x,y) coordinates for that point. Then it will evaluate the desired function F, and plot F, as a function of (x,y,v), with x and y fixed as a new 1-d plot. So doable. But not as anything that exists directly off the shelf. If you want it badly enough, you just need to write the code. It would require a mouse click though. I think hovering will not be sufficient.
And, yes, if I really wanted that Lambo badly enough, I could buy it. But, I'm pretty sure a Lambo gets terrible traction in a foot of snow up where I live.
  1 Comment
Tworit Dash
Tworit Dash on 19 Oct 2022
A mouse click is still fine. I must admit that I searched for an easier route to just ask it here with what I have tried. My daily job is that of PhD student in STEM field and I usually don't get enough time to do a lot of such implementations. As I chose an easier route, I am grateful for whatever I get as a response. Many thanks on that regard really.

Sign in to comment.


Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by