Plotting a 1D function by hovering on a 2D plot
24 views (last 30 days)
Show older comments
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?
clear;
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);
end
end
x_indx = 30;
y_indx = 40;
F_squeeze = squeeze(F(x_indx, y_indx, :));
figure; plot(v, F_squeeze);
0 Comments
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
clear
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)
setappdata(h,'htool',htool);
setappdata(h,'F',F);
setappdata(h,'ax2',ax2);
function button_down(src, event)
htool = getappdata(src,'htool')
Position = htool.String;
ax2 = getappdata(src,'ax2');
F = getappdata(src,'F');
plot(ax2,rand(1,30))
% 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
end
More Answers (1)
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.
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!