matlab: figure size, same axis size - matlab

I have a figure with fixed size, like that:
hFig = figure(1);
set(hFig, 'Position', [200 200 500 500])
But the thing is, that I want to have my AXIS with fixed size (i want them to be a square), not (necessary) the whole figure... - see image attached, Y axis is a bit longer than X axis (of course longer in a meaning of display... X and Y axis range is set to the same value). How to adjust it?
Thanks!

Use axis equal to set the spacing of the axis to be the same.

Related

Keep subplot in square while having (1,3) tiledlayout [duplicate]

So I have this matrix in MATLAB, 200 deep x 600 wide. It represents an image that is 2cm deep x 6cm wide. How can I plot this image so that it is locked into proper dimensions, i.e. 2cm x 6cm? If I use the image or imagesc commands it stretches it all out of shape and shows it the wrong size. Is there a way to lock it into showing an image where the x and y axes are proportional?
Second question, I need to then set this image into a 640x480 frame (20 pixel black margin on left and right, 280 pixel black margin on bottom). Is there a way to do this?
To keep aspect ratio, you can use axis equal or axis image commands.
Quoting the documentation:
axis equal sets the aspect ratio so that the data units are the same in every direction. The aspect ratio of the x-, y-, and z-axis is adjusted automatically according to the range of data units in the x, y, and z directions.
axis image is the same as axis equal except that the plot box fits tightly around the data`
For second question:
third_dimension_size=1; %# for b&w images, use 3 for rgb
framed_image=squeeze(zeros(640,480,third_dimension_size));
framed_image(20:20+600-1,140:140+200-1)= my_600_200_image;
imagesc(framed_image'); axis image;
set(gca,'DataAspectRatio',[1 1 1])
Second question:
new_image = zeros(480,640);
new_image(20:(200+20-1),20:(600+20-1)) = old_image;
As an alternative to the other answers, you might want:
set(gca, 'Units', 'centimeters', 'Position', [1 1 6 2])
Make sure you do this after plotting the image to get the other axis properties correct.
For the second question, take care with the number of colour channels:
new_image = zeros(480,640, size(old_image));
new_image(20:(200+20-1),20:(600+20-1),:) = old_image;

Matlab : Pan a figure programmatically

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)

Distance between axis number and axis in MATLAB figure

I struggle a little bit with overlapping axis numbers of the y and x axis like it is shown in the image. I'd like to keep the size of the numbers and therefore think that simply shifting the numbers away from the axis itself would be an appropriate way to handle this issue.
Is there a possibility to do that?
Thanks in advance,
Joe
Here is a little workaround using text annotations. Basically you clear the current XTick labels and replace them with similar labels, but you can specify the distance from the axis:
clc
clear
close all
x = 1:20;
hPlot = plot(x,sin(x));
set(gca,'xaxisLocation','top');
set(gca,'XTickLabel',[]); %// Clear current XTickLabel
ylim = get(gca,'YLim'); %// Get y limit of the plot to place your text annotations.
for k = 2:2:20
text(k,ylim(2)+0.1,num2str(k),'HorizontalAlignment','Center') %// Play with the 'ylim(1) -0.1' to place the label as you wish.
end
Giving this:
Of course now it's exaggerated and you can do the same for the y axis if you want (using the 'XLim' property of the current axis,gca).

Matlab subplots with fixed height and width

I have a figure with 12 subplots, arranged in a 6 by 2 matrix. I want the figure to fit on a A4 paper such that I can include it in a corresponding LaTex document later on. In the end, it should look like the graphs in Fiscal news and macroeconomic volatility, page 2593. My result looks like this so far.
I have two problems though to get the same result:
The subplots should all have the same width and height.
The max and min of the y axis of the subplots should correspond to the y values of the graph, i.e. if the graph is only in the negative space, then the maximum value of the y axis should be around 0 as well.
My code (this is a simplified version) looks like this so far:
figure
set(gcf, 'PaperUnits', 'centimeters');
set(gcf, 'PaperType', 'A4');
for s=1:12
subplot(6,2,s)
plot((1:30),M_.endo_names(s,:),'b','LineWidth',1);
hold on;
plot([0,30],[0,0],'k-','LineWidth',0.2);
hold off;
title(deblank(M_.endo_names(s,:)));
end
Although I have found threads that answer question 1, I was not able to apply their solutions to my problem. They proposed to use
subplot('Position',[left bottom width height])
which never looks like what I need.
I am happy to hear about your solutions! Thank you
axis handle should be helpful. Try this code:
figure
set(gcf, 'PaperUnits', 'centimeters');
set(gcf, 'PaperType', 'A4');
for s=1:12
subplot(6,2,s)
plot((1:30),M_.endo_names(s,:),'b','LineWidth',1),axis([0 30 min(M_.endo_names(s,:)) max(M_.endo_names(s,:))])
hold on;
plot([0,30],[0,0],'k-','LineWidth',0.2);
hold off;
title(deblank(M_.endo_names(s,:)));
end
This will give you graphs with y-axes values ranging from the minimum value to the maximum value of the variables

Matching axes scales

I have 3D data plotted using the 'plot3' function. I would like to constrain the Y and Z axes such that they are equal in scale. The X axis should be automatically scaled as usual.
I know from here that I can make the X axis be the only one to be automatically scaled by using the command:
axis 'auto x';
However, this causes the Y and Z axes to be plotted from 0 to 1 only; my data often exceeds this in all axes. What I'm looking for is a plot which contains all the data in a single view, but with the smallest of the Y or Z axes scaled down so that the Y and Z axes are equivalent in scale.
Try daspect.
plot3(5*rand(10,1),10*rand(10,1),rand(10,1))
tmpAspect=daspect();
daspect(tmpAspect([1 2 2]))
daspect() returns the current aspect ratio as produced by axis 'auto'.
daspect(tmpAspect([1 2 2])) then enforces that y and z have the same scale.
How about
axis equal
or even
axis tight
axis equal
both after the plot has been drawn.
Is this what you mean?
Type help axis at the Matlab command prompt for more capabilities of the axis function.