MATLAB Answers

Matt J

How to zoom in/out on an axis programmatically

Asked by Matt J
on 22 May 2013
Latest activity Answered by Mike Smith
on 17 Aug 2016

In a figure window toolbar, there are spyglass icons that let you zoom in and out on parts of an image or plot. I'd like to know how to do this programmatically.

  1 Comment

on 17 Apr 2015

I have a figure with the plots and axes out of screen and no scroll bar. Please I need help shrinking the plot size.


No products are associated with this question.

2 Answers

Answer by Youssef Khmou
on 22 May 2013
 Accepted answer


There is a function "zoom" with the following options :

 zoom on 
 zoom off
 zoom out
 zoom reset 
 zoom xon
 zoom yon
 zoom(fig, option) 
 h = zoom(figure_handle)


Matt J
on 2 Jun 2013

Well, I was mainly after a programmatic way to duplicate the spyglass figure window icon functionality... But how is the zoom scrollbar different from the spyglass?

It's not. The scroll bar just give you an absolute zoom level, which is the scroll bar value. So then you just do zoom('out') everytime to reset it back to the starting zoom, and then zoom(scrollbarValue) in the callback of the scrollbar.

% --- Executes on slider movement.
function sldZoom_Callback(hObject, eventdata, handles)
% hObject    handle to sldZoom (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% 	scrollbarValue = get(hObject,'Value');
% 	caption = sprintf('H value = %.2f', scrollbarValue);
% 	set(handles.txtZoom, 'string', caption);
		zoomFactor = get(hObject,'Value');
		txtInfo = sprintf('Zoom Factor = %.2f   (%d %%)\n\nOnce zoomed, you can pan by clicking and dragging in the image.', zoomFactor, round(zoomFactor * 100));
		set(handles.txtInfo, 'String', txtInfo);
		txtInfo = sprintf('Zoom Factor = %.2f\n\nOnce zoomed, you can pan by clicking and dragging in the image.', zoomFactor);
		set(handles.sldZoom, 'TooltipString', txtInfo);
		txtZoom = sprintf('Zoom Factor = %.2f   (%d %%)', zoomFactor, round(zoomFactor * 100));
		set(handles.txtZoom, 'String', txtZoom);
	% 	if zoomFactor ~= 1
	% 	else
	% 	end
		% Set up to allow panning of the image by clicking and dragging.
		% Cursor will show up as a little hand when it is over the image.
		set(handles.axesImage, 'ButtonDownFcn', 'disp(''This executes'')');
		set(handles.axesImage, 'Tag', 'DoNotIgnore');
		h = pan;
		set(h, 'ButtonDownFilter', @myPanCallbackFunction);
		set(h, 'Enable', 'on');
	catch ME
		message = sprintf('Error in sldZoom_Callback():\n%s', ME.message);
	return; % from sldZoom_Callback

Answer by Mike Smith
on 17 Aug 2016

If you know the zoom level that you want, you can do it by setting the Xlim and Ylim on the Children of the figure handle. So if you wanted to zoom the x axis between 400 and 500 you could do: h=gcf; set(h.Children,'Xlim',[400 500]);


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!