![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1426783/image.png)
How to save stacked plot with data cursor tips displayed?
20 次查看(过去 30 天)
显示 更早的评论
Hi is there a way to save the stacked plot with all the y values dsiplayed when hovering the cursor over the plot (see attached figure)? screenshot or printscr would be my last option as i am aiming to save a vector based image format specifically .eps.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1426383/image.png)
0 个评论
回答(1 个)
Subhajyoti Halder
2023-7-5
Hi Arjun,
It is my understanding that it is required to plot multiple graphs vertically stacked and get corresponding values of all the graphs when you click on a particular x-axis position.
With ‘subplot’ function, you can plot all the plots vertically stacked and using the ‘datacursormode’ function you can get the values in each plot when you click on it.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1426783/image.png)
Here, I have implemented the same using ‘datacursormode’ in MATLAB R2023a.In the below code, when you click on any graph in the ‘figure’ pop-out, it prints the values of the correspond x-axis for all the graphs in the output panel
% Sample data
x = 1:100;
y1 = rand(1, 100);
y2 = rand(1, 100);
y3 = rand(1, 100);
% Create a figure
figure;
% Plot the first line chart
subplot(3, 1, 1);
plot(x, y1);
title('Line Chart 1');
% Plot the second line chart
subplot(3, 1, 2);
plot(x, y2);
title('Line Chart 2');
% Plot the third line chart
subplot(3, 1, 3);
plot(x, y3);
title('Line Chart 3');
% Enable data cursor mode
datacursormode on;
% Capture mouse clicks
disp('Click on the line charts to display values. Press Ctrl+C to exit.');
while true
try
[x_click, ~] = ginput(1);
% Display values for all line charts
disp(['X-axis value: ', num2str(x_click)]);
disp(['Line Chart 1 - Y value: ', num2str(interp1(x, y1, x_click))]);
disp(['Line Chart 2 - Y value: ', num2str(interp1(x, y2, x_click))]);
disp(['Line Chart 3 - Y value: ', num2str(interp1(x, y3, x_click))]);
catch
break;
end
end
Here, I have used sample data, which you can substitute with your original data.
Also, to modify the terminal output, you can tweak the code within the while loop, which is printing the result.
For more details on the ’datacursormode’ function, kindly go through the following documentation
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polar Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!