How to save stacked plot with data cursor tips displayed?
조회 수: 15 (최근 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.
댓글 수: 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.
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에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!