How to save stacked plot with data cursor tips displayed?

26 views (last 30 days)
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.

Answers (1)

Subhajyoti Halder
Subhajyoti Halder on 5 Jul 2023
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.
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
  1 Comment
arjun luther
arjun luther on 5 Jul 2023
Hi thanks for your answer. I tried both, using subplot and vetical stacking the tiled layout but what i am looking is not to print the values out. I want all the y axis value corresponding to a one x axis value could be shown on th plot itself like in the image in question. When using subplot, if we zoom in the axis, it has to be done individiually for all subplots.

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!