Axes in MATLAB®
How to adjust axes properties in MATLAB®, axes titles, styling and coloring axes and grid lines, ticks, tick labels and more.
Position Multiple Axes in Figure
Position two Axes
objects in a figure and add a plot to each one.
Specify the position of the first Axes
object so that it has a lower left corner at the point (0.1 0.1) with a width and height of 0.7. Specify the position of the second Axes
object so that it has a lower left corner at the point (0.65 0.65) with a width and height of 0.28. By default, the values are normalized to the figure. Return the Axes
objects as ax1
and ax2
.
figure
ax1 = axes('Position',[0.1 0.1 0.7 0.7]);
ax2 = axes('Position',[0.65 0.65 0.28 0.28]);
Add a plot to each Axes
object. Specify the axes by passing it as the first input argument to the graphics function. Most graphics functions reset some axes properties, such as the tick values and labels. However, they do not reset the axes position.
figure
ax1 = axes('Position',[0.1 0.1 0.7 0.7]);
ax2 = axes('Position',[0.65 0.65 0.28 0.28]);
contour(ax1,peaks(20))
surf(ax2,peaks(20))
fig2plotly(gcf);
Unrecognized field name "Fill". We had trouble parsing the contour object. This trace might not render properly.
Make Axes the Current Axes
Create two overlayed Axes
objects. Then, specify the current axes and add a plot.
First create two Axes
objects and specify the positions. Display the box outline around each axes. Return the Axes
objects as ax1
and ax2
.
figure
ax1 = axes('Position',[0.1 0.1 .6 .6],'Box','on');
ax2 = axes('Position',[.35 .35 .6 .6],'Box','on');
Make ax1
the current axes. This action brings the axes to the front of the display and makes it the target for subsequent graphics functions. Add a line plot to the axes.
figure
ax1 = axes('Position',[0.1 0.1 .6 .6],'Box','on');
ax2 = axes('Position',[.35 .35 .6 .6],'Box','on');
axes(ax1)
x = linspace(0,10);
y = sin(x);
plot(x,y)
fig2plotly(gcf);
Tick Placement, Color, and Style
trace1 = struct(...
'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
'y', [8, 7, 6, 5, 4, 3, 2, 1, 0], ...
'type', 'scatter');
trace2 = struct(...
'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
'y', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
'type', 'scatter');
data = {trace1, trace2};
layout = struct(...
'xaxis', struct(...
'autotick', false, ...
'ticks', 'outside', ...
'tick0', 0, ...
'dtick', 0.25, ...
'ticklen', 8, ...
'tickwidth', 4, ...
'tickcolor', '#000'), ...
'yaxis', struct(...
'autotick', false, ...
'ticks', 'outside', ...
'tick0', 0, ...
'dtick', 0.25, ...
'ticklen', 8, ...
'tickwidth', 4, ...
'tickcolor', '#000'));
plotly(data, struct('layout', layout));
Reversed Axes
data = {...
struct(...
'x', [1, 2], ...
'y', [1, 2], ...
'type', 'scatter')...
};
layout = struct('xaxis', struct('autorange', 'reversed'));
plotly(data, struct('layout', layout));
Logarithmic Axes
trace1 = struct(...
'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
'y', [8, 7, 6, 5, 4, 3, 2, 1, 0], ...
'type', 'scatter');
trace2 = struct(...
'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
'y', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
'type', 'scatter');
data = {trace1, trace2};
layout = struct(...
'xaxis', struct(...
'type', 'log', ...
'autorange', true), ...
'yaxis', struct(...
'type', 'log', ...
'autorange', true));
plotly(data, struct('layout', layout));
Nonnegative
, tozero
, and normal
Rangemode
data = {...
struct(...
'x', [2, 4, 6], ...
'y', [-3, 0, 3], ...
'type', 'scatter')...
};
layout = struct(...
'showlegend', false, ...
'xaxis', struct(...
'rangemode', 'tozero', ...
'autorange', true), ...
'yaxis', struct(...
'rangemode', 'nonnegative', ...
'autorange', true));
plotly(data, struct('layout', layout));
Manually Scaling Axes
trace1 = struct(...
'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
'y', [8, 7, 6, 5, 4, 3, 2, 1, 0], ...
'type', 'scatter');
trace2 = struct(...
'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
'y', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
'type', 'scatter');
data = {trace1, trace2};
layout = struct(...
'xaxis', struct('range', [2, 5]), ...
'yaxis', struct('range', [2, 5]));
plotly(data, struct('layout', layout));
Styling and Coloring Axes and the Zero-Line
trace1 = struct(...
'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
'y', [8, 7, 6, 5, 4, 3, 2, 1, 0], ...
'type', 'scatter');
trace2 = struct(...
'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
'y', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
'type', 'scatter');
data = {trace1, trace2};
layout = struct(...
'xaxis', struct(...
'showgrid', true, ...
'zeroline', true, ...
'showline', true, ...
'mirror', 'ticks', ...
'gridcolor', '#bdbdbd', ...
'gridwidth', 2, ...
'zerolinecolor', '#969696', ...
'zerolinewidth', 4, ...
'linecolor', '#636363', ...
'linewidth', 6), ...
'yaxis', struct(...
'showgrid', true, ...
'zeroline', true, ...
'showline', true, ...
'mirror', 'ticks', ...
'gridcolor', '#bdbdbd', ...
'gridwidth', 2, ...
'zerolinecolor', '#969696', ...
'zerolinewidth', 4, ...
'linecolor', '#636363', ...
'linewidth', 6));
plotly(data, struct('layout', layout));
Set and Style Axes Title Labels and Ticks
trace1 = struct(...
'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
'y', [8, 7, 6, 5, 4, 3, 2, 1, 0], ...
'type', 'scatter');
trace2 = struct(...
'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
'y', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
'type', 'scatter');
data = {trace1, trace2};
layout = struct(...
'xaxis', struct(...
'title', 'AXIS TITLE', ...
'titlefont', struct(...
'family', 'Arial, sans-serif', ...
'size', 18, ...
'color', 'lightgrey'), ...
'showticklabels', true, ...
'tickangle', 45, ...
'tickfont', struct(...
'family', 'Old Standard TT, serif', ...
'size', 14, ...
'color', 'black'), ...
'exponentformat', 'e', ...
'showexponent', 'All'), ...
'yaxis', struct(...
'title', 'AXIS TITLE', ...
'titlefont', struct(...
'family', 'Arial, sans-serif', ...
'size', 18, ...
'color', 'lightgrey'), ...
'showticklabels', true, ...
'tickangle', 45, ...
'tickfont', struct(...
'family', 'Old Standard TT, serif', ...
'size', 14, ...
'color', 'black'), ...
'exponentformat', 'e', ...
'showexponent', 'All'));
plotly(data, struct('layout', layout));
Axes Equal
fig = figure;
t = 0:pi/20:2*pi;
x = sin(t);
y = cos(t);
plot(x,y);
title('axis equal');
grid on;
axis equal tight;
fig2plotly(fig);
Toggling Axes Lines, Ticks, Labels, and Autorange
trace1 = struct(...
'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
'y', [8, 7, 6, 5, 4, 3, 2, 1, 0], ...
'type', 'scatter');
trace2 = struct(...
'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
'y', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
'type', 'scatter');
data = {trace1, trace2};
layout = struct(...
'xaxis', struct(...
'autorange', true, ...
'showgrid', false, ...
'zeroline', false, ...
'showline', false, ...
'autotick', true, ...
'ticks', '', ...
'showticklabels', false), ...
'yaxis', struct(...
'autorange', true, ...
'showgrid', false, ...
'zeroline', false, ...
'showline', false, ...
'autotick', true, ...
'ticks', '', ...
'showticklabels', false));
plotly(data, struct('layout', layout));
Plotting Secondary Y Axis
A = 1000;
a = 0.005;
b = 0.005;
t = 0:900;
z1 = A*exp(-a*t);
z2 = sin(b*t);
[ax,p1,p2] = plotyy(t,z1,t,z2,'semilogy','plot');
ylabel(ax(1),'Semilog Axis')
ylabel(ax(2),'Linear Axis')
fig2plotly(gcf);
Axis Limits with xlim
and ylim
functions
fig = figure;
x = linspace(0,5,1000);
y = sin(100*x)./exp(x);
plot(x,y);
xlim([0 1]);
ylim([-2 2]);
fig2plotly(fig,'strip', false);
Maintaining Aspect Ratio
t = 0:pi/20:2*pi;
x = sin(t);
y = 2*cos(t);
fig = figure;
subplot(2,2,1);
plot(x,y);
title('axis normal');
grid on;
axis normal;
subplot(2,2,2);
plot(x,y);
title('axis square');
grid on;
axis square;
subplot(2,2,3);
plot(x,y);
title('axis equal');
grid on;
axis equal;
subplot(2,2,4);
plot(x,y);
title('axis equal tight');
grid on;
axis equal tight;
fig2plotly(fig,'strip', false);