MATLAB: I am using contour with a color bar. The axis are not equal sizes.

axiscolorercontourfequal axisMATLABplotrange

%plotting the contour
%setting color range
%allowing colorbar to be manipulated
cbar = colorbar('peer', gca);
%setting colorbar label
cbar.Label.String = 'Mach Number';
%setting axii labels
xlabel('r [cm]')
ylabel('r [cm]')
%putting a scatter plot onto the contour
hold on
set(gca,'FontSize', fontsize);
st_fig_range = caxis;
%setting colorbar ranges
caxis([st_fig_range(1) st_fig_range(1)+1]);
%attempting to make axis square
axis square
%saving figure
sprintf('/ContourM%.0fz%.0f', machnumber,j) ],'-depsc','-loose')
So here is my code, hopefully these comments make sense. This is embedded in some research code I have been working on for several years so it may not be the cleanest, but it generally works really well. I am by no means an expert in Matlab, but I am happy with the codes efficiency (or lack there of). Mostly, the 'axis square' command isnt working.
Due to the colorbar, the axis arent completely square. The plot is slightly taller than it is long. I need to automate this because of how many figures I am producing. Is there a method, brute force or otherwise, that would make this work?
I would love some help! Thanks!

Best Answer

  • Hi Erik,
    If axis square and axis equal results are not what you expect, you can manually set the axis sizes in a figure you can use the set function with the axes handle. The following code demonstrates it.
    x = linspace(-2*pi,2*pi);
    y = linspace(0,4*pi);
    [X,Y] = meshgrid(x,y);
    Z = sin(X) + cos(Y);
    cbar = colorbar(gca);
    Ax = gca;
    set(Ax,'Units','pixels','Position',[10 10 200 200])
    You can also refer to this answer regarding the units.
    Kiran Felix Robert