Matlab : Pan a figure programmatically - matlab

I've been plotting some figures and I want to save them multiple times zoomed in certain areas. All my views have pivot-point at y=0 except one, the last view. I've been using 'XLim' to change the view on the X axis. Now I need to pan the view on the Y axis. When using YLim, the aspect ratio of the image changes.
Is it possible to set some pivot point on the Y-axis?
Here is a minimum working code where my problem can be seen. Pauses 2 times.
f1 = figure(10);
hold on
axis equal
x = linspace(1,2*pi);
y = sin(x);
plot(x,y,'*')
xlims=[0 2*pi; 1 2*pi; 0.5 1.5];
for i = 1:size(xlims,1)
set(gca,'XLim',xlims(i,:))
disp('Here I am saving this view! (Paused)')
pause
end
% Now I want to PAN the Y axis and set the view around Y = 1.
% But still keep last set Xlims AND keep the aspect ratio of figure.
% When using YLim, the aspect ratio changes.
set(gca,'YLim',[0.9 1.1]) % Not what I have in mind.
% Just need to pivot Y=1...

There's no "panning" command as such. (there is a pan command but this just toggles the ability to pan using the mouse on or off).
But, there is the ability to set your axis limits dictating which portion of the plot to show, using the axis function, with manually specified limits.
e.g. axis([ xmin, xmax, ymin, ymax ]);
Furthermore, if you apply this after any other axis calls that affect shape / aspect ratio (e.g. axis square or axis equal) then these will be preserved.
Note that in your particular example, the use of axis equal therefore necessarily means that for a smaller range of Y your aspect ratio will be smaller. Therefore you might to opt against that.
Alternatively, you can also control the aspect ratio manually with the daspect function. e.g., in your example:
>> axis equal % your initial axis / aspect ratio situation
>> A = axis;
>> axis([0.5,1.5,0.9,1.1]); % your new values. aspect ratio will change to preserve equality
>> B = axis;
>> Y_factor = (A(4)-A(3))/(B(4)-B(3));
>> daspect([Y_factor, 1, 1]); % back to the old "aspect ratio"
However, this clearly means that the two axes are now not "equal", and the comparison with previous graphs might be misleading. This may or may not be a factor to take into consideration.
Finally, if what you're really after is simply to make sure your axes object does not change size, you can simply set the size of your axes object manually (e.g. set(gca, 'position', [x,y,x_width, y_width]) ) and ensure axis is on normal mode (i.e. adapting to the axes size).

The answer given in a comment by #jodag is perfect. Since comments have a tendency to disappear I thought I would reproduce it here.
To "pan", you need to change both limits without changing their interval. Since you can read the current interval with
get(gca, 'YLim')
you can simply pan the y axis by a distance dy with the command:
set(gca, 'YLim', get(gca, 'YLim')+dy)

Related

How to control the default distance between ticks of the Y axis?

Here is a simple MATLAB code. How to control the default distance between ticks of the Y axis? I want to make it smaller to fit in my paper. Hint: I update the post with 2 picture that shows what I mean (they are the same but the distance between the y axis ticks is smaller in one picture that the other.
x = linspace(-10,10,200);
y = sin(4*x)./exp(x);
plot(x,y)
xlim([0 10])
ylim([-0.4 0.8])
You can control the tick by using the gca object of the plot. Here is an example for xtick. Change 'xtick' to 'ytick':
plot(x,y);
set(gca, 'xtick', [-10:2:10]);
If you want to change the x-axis tick labels with new labels, then you can change the values of the labels as follows:
% specify the ticks first where you want to change
xticks([0 2 4 6 8])
% change the corresponding labels to the required ones
xticklabels({'-1', '-2', '-3', '-4', '-5'})
You can modify the height of the graph, maintaining the number and values of the tick marks, which makes the distance between tick marks smaller.
To do so, set the figure window’s 'Position' property (this is equivalent to dragging the edges of the window to make the figure smaller), and setting the locations of the tick marks manually to prevent MATLAB from reducing their number. For example:
h = gcf; % figure handle
a = gca; % axes handle
ticks = get(a,'YTick');
pos = get(h,'Position');
pos(4) = pos(4) * 0.75; # reduce the size
set(h,'Position',pos);
set(a,'YTick',ticks)
You should also note the PaperPosition, PaperSize and other Paper... properties of the figure, as they are used when printing (also to file). You might want to manually set those properties before creating a PDF or EPS from the graph.
Here is even a simpler way then what #Cris suggested:
ax = axes;
ax.YTickMode = 'manual';
ax.Position(4) = ax.Position(4)*0.75;
by setting the YTickMode to manual you prevent Matlab from updating the ticks upon resizing of the axes. Then you change the hight of the axes by setting the position property directly.

Axes ticks of same length across subplots in MATLAB

I am creating a multi-panel figure in MATLAB (with multiple axes of different size). I would like all tick marks to have the same absolute size across all subplots.
According to the MATLAB user guide, tick length is normalized with respect to the longest axis:
TickLength. Tick mark length, specified as a two-element vector of the form [2Dlength 3Dlength]. [...] Specify the values in units normalized relative to the longest of the visible x-axis, y-axis, or z-axis lines.
In order to make all ticks of the same length, I am running the following code:
fixlen = 0.005; % Desired target length
for i = 1:numel(h) % Loop over axes handles
rect = get(h(i),'Position'); % Get the axis position
width = rect(3); % Axis width
height = rect(4); % Axis height
axislen = max([height,width]); % Get longest axis
ticklen = fixlen/axislen; % Fix length
set(h(i),'TickDir','out','TickLength',ticklen*[1 1]);
end
Unfortunately, the above code does not produce a figure in which all tick lengths are equal. Perhaps I am missing something?
Solution. There were two problems in my code.
First of all, I needed to switch from Normalized units to some fixed units (such as pixels). See the answer below.
In some part of the code, before the above snippet, I was resizing the figure and I had a drawnow to update it. However, MATLAB would reach the code snippet before the graphics commands had been executed, and therefore the reported sizes were not correct. I solved the issue by placing a pause(0.1) command after the drawnow.
By default, Axis objects have their Units property set to normalized. This means that the values in the Position property are normalized by the size of the figure. Hence your code may not be producing the desired behavior if the figure is not square.
One way to fix this is the following:
rect = get(h(i),'Position'); % Axis position (relative to figure)
hfig = get(h(i),'Parent'); % Handle to parent figure
rectfig = get(hfig, 'Position'); % Figure position (in pixels)
width = rect(3) * rectfig(3); % Absolute width of axis (in pixels)
height = rect(4) * rectfig(4); % Absolute height of axis (in pixels)
This will give you width/height in terms of pixels on your screen (assuming that you didn't change the Units property of the figure).
And if you use rectfig = get(hfig,'PaperPosition') then you will get width/height in terms of inches on the printed page (again, assuming default value for the figure's PaperUnits property).
Note, however, that you will need to adjust the value you use for fixlen to match the new units we're using here.

How to add a circle to simple matlab figure?

I have a plot of some data (simple 2-d line) and I would like to add circles around some more interesting spots on it. Surprisingly matlab seems to have no simple way to create a physically round circles. I looked on the internet and most answers i found was to either use rectangle('Curvature',[1 1]) or pts = linspace(0,2*pi, 100); plot(sin(pts), cos(pts)); and fixing the aspect ration of a plot to 1. In my case axes have scales that differ by several orders of magnitude so fixing the aspect ration is no option.
I was trying different ways to get the right x/y scale factor but it still seems I'm missing something. My current attempt is:
function hc = circle(x, y, xr)
gca_ylim = get(gca, 'ylim');
gca_xlim = get(gca, 'xlim');
gca_pos = get(gca, 'Position');
gcf_pos = get(gcf, 'Position');
gcf_ar = get(gca, 'DataAspectRatio');
%mod = gca_pos(4)/gca_pos(3) *abs(gca_ylim(2)-gca_ylim(1))/abs(gca_xlim(2)-gca_xlim(1))*gcf_pos(3)/gcf_pos(4);
mod = gca_pos(4)/gca_pos(3)*gcf_ar(2)/gcf_ar(1)*gcf_pos(3)/gcf_pos(4);
yr = xr*mod;
rectangle('Position',[x-xr,y-yr,xr*2,xr*mod*2], 'Curvature',[1,1]);
end
The circles that i got that way are still a bit elongated and I have no idea why. If there is any simple method to get circles in a plot - please share.
PS I know that if I resize plot or add some more stuff to it and change scaling the circles will re-scale with the entire plot. This is not a problem in my case - Figure gets printed out without manual manipulation (no window resizing) and I can add them as the last objects.
Another option:
>> h = plot(rand(1,5),rand(1,5),'o');
>> set(h, 'MarkerSize', 100);
If you want scale invariant circles, you could use scatter command. You can also set the size smaller or larger.
scatter(X,Y,S) draws each circle with the size specified by S. To plot
each circle with equal size, specify S as a scalar. To plot each
circle with a specific size, specify S as a vector with length equal
to the length of X and Y.
http://www.mathworks.com/help/matlab/ref/scatter.html

correct axis range (matlab)

how can i solve the problem of these two images automatically (with a code adaptable to different data) without having to fix the axes range for each plot (i have millions of these plots)?
problem: axis range should be smaller
problem: axis range should be bigger
also, i need axis to be correctly labeled from the first value to last (see example in comment please)
any help is highly appreciated. thank you so so much.
To set axis limit and visualize chart better you can use axis command like axis([xmin xmax ymin ymax]) where parameters set chart borders. It should help you. More information is here:
http://www.mathworks.se/help/matlab/ref/axis.html
In order to have a complete bounding box use box on.
In order to avoid large empty space around a plot (or no space at all) use xlim and ylim. Try the following:
figure
plot(x,y)
box on
x1 = min(x);
x2 = max(x);
dx = x2-x1;
y1 = min(y);
y2 = max(y);
dy = y2-y1;
fc = 10/100 % this is a factor of 10% of empty space around plot
xlim([x1-dx*fc x2+dx*fc])
ylim([y1-dy*fc y2+dy*fc])
If you want to have a tick value appear at the start and the end of the axis, you could either force it by set(gca,'Xtick',[values]), where values are those ticks you want to show; or by floor and ceil of the xlim and ylim min and max limits above.
Hope this is what you need

axis equal in a Matlab loglog plot

In Matlab the command 'axis equal':
sets the aspect ratio so that equal tick mark
increments on the x-,y- and z-axis are equal in size. This
makes SPHERE(25) look like a sphere, instead of an ellipsoid
However, when using the loglog plotting function, this doesn't work "properly". What I would like to happen is that I get an aspect ratio so that a given factor occupies the same visual distance. What actually happens is that
>> loglog(2.^[1:20]*1e10,(2.^[1:20]).^2)
>> axis equal
results in
rather than
So that the slope 2 (from the squared) could be easily observed, and so that there wouldn't be all that extra white space.
My question is:
Is there a Matlab command that does this for me? Alternatively, has anyone solved this problem before?
One solution is for you to modify the axes limits and 'DataAspectRatio' properties yourself so that a decade on one axis equals a decade on the other. Here's how you can do it for your example:
loglog(2.^[1:20]*1e10,(2.^[1:20]).^2); %# Plot your sample data
xLimits = [1e10 1e16]; %# Limits for the x axis
yLimits = [1 1e12]; %# Limits for the y axis
logScale = diff(yLimits)/diff(xLimits); %# Scale between the x and y ranges
powerScale = diff(log10(yLimits))/... %# Scale between the x and y powers
diff(log10(xLimits));
set(gca,'Xlim',xLimits,'YLim',yLimits,... %# Set the limits and the
'DataAspectRatio',[1 logScale/powerScale 1]); %# data aspect ratio
set(gca,'XTick',[1e10 1e12 1e14 1e16]); %# Change the x axis tick marks
And here's the resulting plot:
Notice that the space between the 100 and 102 tick marks on the y axis spans the same number of pixels as the space between the 1010 and 1012 tick marks on the x axis, thus making a decade on one axis equal to a decade on the other.
If you don't want to change the axes limits, and instead want to use the default limits chosen by MATLAB, you can simply fetch the limits from the axes to perform the computations:
xLimits = get(hAxes,'XLim');
yLimits = get(hAxes,'YLim');
However, in order to disable MATLAB's automatic axes resizing behavior you will still have to either set the axes limits to the same values or set the limit mode properties to 'manual' when you update the 'DataAspectRatio' property:
set(gca,'Xlim',xLimits,'YLim',yLimits,...
'DataAspectRatio',[1 logScale/powerScale 1]);
%# OR...
set(gca,'XLimMode','manual','YLimMode','manual',...
'DataAspectRatio',[1 logScale/powerScale 1]);
If all of this seems like a lot of work, you can simplify things by putting it all into a function. I will actually be submitting a function decades_equal to the MathWorks File Exchange based on the code in this answer. For the time being, here is a trimmed down version (i.e. no error checking or help) that you can use:
function decades_equal(hAxes,xLimits,yLimits)
if (nargin < 2) || isempty(xLimits)
xLimits = get(hAxes,'XLim');
end
if (nargin < 3) || isempty(yLimits)
yLimits = get(hAxes,'YLim');
end
logScale = diff(yLimits)/diff(xLimits);
powerScale = diff(log10(yLimits))/diff(log10(xLimits));
set(hAxes,'Xlim',xLimits,...
'YLim',yLimits,...
'DataAspectRatio',[1 logScale/powerScale 1]);
end
And you can call the function as follows:
loglog(2.^[1:20]*1e10,(2.^[1:20]).^2); %# Plot your sample data
decades_equal(gca); %# Make the decades equal sizes
How it works...
You may be wondering what the logic is behind how I chose the scaling factors above. When trying to make the displayed size of a decade equal for each axes, we have to take into account both the number and sizes of decades within the axes ranges. In the above code, I am basically computing the average decade size for each axis, then using the ratios of the average decade sizes to scale the axes accordingly. For example, diff(yLimits) gives the total size of the y axis, and diff(log10(yLimits)) gives the number of decades (i.e. powers of ten) displayed on the y axis.
This may be easier to see if I reorder the operations in the above code like so:
yDecade = diff(yLimits)/diff(log10(yLimits)); %# Average y decade size
xDecade = diff(xLimits)/diff(log10(xLimits)); %# Average x decade size
set(gca,'XLim',xLimits,'YLim',yLimits,...
'DataAspectRatio',[1 yDecade/xDecade 1]);
And this will give the same scaling results as before.