How can I enlarge a subplot in a 'new' figure window by just clicking any of the subplot?
조회 수: 7 (최근 30일)
이전 댓글 표시
Lets say, I have 2 X 2 subplot grid in a single figure window. When the 4 subplots are shown in a single figure, I want to be able to click on any of the individual subplot, that should open that particular graph in a new figure window (eesentially, enlarging the subplot into a new figure). Is there a way to do this?
Thank You.
댓글 수: 0
답변 (1개)
Samayochita
2025년 3월 26일
Hi Ajeya,
The required functionality can be achieved in MATLAB by using the “ButtonDownFcn” property of each subplot which allows you to define a callback function that is executed when you click on the subplot. In the callback function, you can create a new figure and plot the data from the selected subplot.
Here is how one could implement this:
1) Create a 2x2 grid of subplots.
figure;
for i = 1:4
subplot(2, 2, i);
plot(rand(10, 1));
title(['Subplot ' num2str(i)]);
% Set the ButtonDownFcn property for each subplot
ax = gca; % Get the current axes
ax.ButtonDownFcn = @(src, event)enlargePlot(src);
end
2) Define a callback function “enlargePlot” that will be called when a subplot is clicked. This function will open a new figure and copy the contents of the clicked subplot to the new figure.
function enlargePlot(src)
% Create a new figure
newFig = figure;
% Copy the contents of the clicked subplot to the new figure
newAx = copyobj(src, newFig);
% Adjust the position to fill the new figure
set(newAx, 'Position', get(0, 'DefaultAxesPosition'));
end
For more information on the “ButtonDownFcn” property, please refer to the following documentation:
I hope this helps.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Subplots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!