In matlab, how can I zoom in on a graph in a script

I would like to zoom in using a script. I'm only interested in horizontally limited scaling. So I would like to do something like

p = plot(myData); z = zoom; set(z, 'ZoomInToPoints' , [50 100]); 

or

 p = plot(myData); myZoom([50, 100]); 

Thus, any of these functions will increase the image scale, as when magnified using a tool with a magnifying glass. I indicate only two points, because I only want to zoom in horizontally.

Note. I already tried using xlim for this. While it works, it does not allow me to use the text command on my graphs, which I need.

+4
source share
2 answers

Calling text will fix the text in a specific set of coordinates on the chart. Have you tried updating them after calling xlim?

EDIT: You can always adjust the position of the text:

 x=1:.1:10; y=sin(.1*x); plot(x,y) text(6,.8,'test') %#Sample figure F=get(0,'children'); %#Figure handle A=get(F,'Children'); %#Axes handle T=findobj(A,'Type','text'); %# Text handle oldxlim=xlim; %#grab the original x limits before zoom oldpos=get(T,'Position'); %#get the old text position set(A,'xlim',[5 15]); %#Adjust axes newxlim=xlim; newpos=[(oldpos(1)-oldxlim(1))*(diff(newxlim))... /(diff(oldxlim))+newxlim(1) oldpos(2:end)]; %#interpolate to place the text at the same spot in the axes set(T,'Position',newpos) %#Finally reset the text position 

Not pretty, but it should work. If you have several annotations on the axes or axes in the drawing, you can always throw the above code in a loop.

+2
source

What is the problem with text and xlim ? Isn't that the type of behavior you need?

 plot(1:100,randn(100,1)) text(80,1.5,'text') set(gca,'XLim',[70 100]) % notice that text stays at same point in "data space" but moves in "axis space" text(80,1,'text2'); % new text appears in axis space as well 

If I'm wrong, and you want the text to appear at a specific point in your axis space (and not in the data space that text uses), no matter how you scale, you can create a different set of axes for your text:

 inset_h = axes('position',[0.5 0.5 0.2 0.2]) set(inset_h,'Color','none'); axis off text(0,0,'text') 
+1
source

All Articles