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

조회 수: 24 (최근 30일)
Tworit Dash
Tworit Dash 2022년 10월 19일
댓글: Tworit Dash 2022년 10월 19일
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);

채택된 답변

Matt
Matt 2022년 10월 19일
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

추가 답변 (1개)

John D'Errico
John D'Errico 2022년 10월 19일
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
Tworit Dash
Tworit Dash 2022년 10월 19일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by