Black Lives Matter. Please consider donating to Black Girls Code today.

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.


% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

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;

resp = fig2plotly(fig,'strip',false);
plotly_url = resp.url;
% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

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'));
response = plotly(data, struct('layout', layout, 'filename', 'axes-ticks', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

data = {...
  struct(...
    'x', [1, 2], ...
    'y', [1, 2], ...
    'type', 'scatter')...
};
layout = struct('xaxis', struct('autorange', 'reversed'));
response = plotly(data, struct('layout', layout, 'filename', 'axes-reversed', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

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));
response = plotly(data, struct('layout', layout, 'filename', 'axes-range-type', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

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));
response = plotly(data, struct('layout', layout, 'filename', 'axes-range-mode', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

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]));
response = plotly(data, struct('layout', layout, 'filename', 'axes-range-manual', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

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));
response = plotly(data, struct('layout', layout, 'filename', 'axes-lines', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

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'));
response = plotly(data, struct('layout', layout, 'filename', 'axes-labels', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

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));
response = plotly(data, struct('layout', layout, 'filename', 'axes-booleans', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api


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;

resp = fig2plotly(fig,'strip',false);
plotly_url = resp.url;
Inspired from Matlab Docs
% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

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') % label left y-axis
ylabel(ax(2),'Linear Axis') % label right y-axis

response = fig2plotly;
plotly_url = response.url;
Inspired from Matlab Docs
% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

fig = figure;
x = linspace(0,5,1000);
y = sin(100*x)./exp(x);
plot(x,y);
xlim([0 1]);
ylim([-2 2]);

% Setting Strip as false preserves Matlab Styles
response = fig2plotly(fig,'strip',false);
plotly_url = response.url;
Inspired from Matlab Docs
% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

plot(1:100,'FaceColor','green');
title('Customizing grid color, spacing and size');
response = fig2plotly;

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

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

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

% Grid Spacing
response.layout.xaxis1.dtick = 25;
response.layout.yaxis1.dtick = 25;

response2 = plotly(response.data, struct('layout',response.layout));
plotly_url = response2.url;

MATLAB is a registered trademark of The MathWorks, Inc.