Axes in MATLAB®

How to adjust axes properties in MATLAB®. Seven examples of linear and logarithmic axes, axes titles, and styling and coloring axes and grid lines.


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));

data = {...
  struct(...
    'x', [1, 2], ...
    'y', [1, 2], ...
    'type', 'scatter')...
};

layout = struct('xaxis', struct('autorange', 'reversed'));

plotly(data, struct('layout', layout));

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));

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));

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));
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));
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));
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,'strip', false);
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));
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();
plot(1:100,'color','green');
title('Customizing grid color, spacing and size');
response = fig2plotly();

response.layout.xaxis1.range = [0 100];
response.layout.yaxis1.range = [0 100];

response.layout.xaxis1.gridcolor = 'rgb(159, 197, 232)';
response.layout.yaxis1.gridcolor = 'rgb(159, 197, 232)';

response.layout.xaxis1.zerolinecolor = 'rgb(74, 134, 232)';
response.layout.yaxis1.zerolinecolor = 'rgb(74, 134, 232)';

response.layout.xaxis1.dtick = 25;
response.layout.yaxis1.dtick = 25;

plotly(response.data, struct('layout', response.layout));
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);

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);