Show Sidebar Hide Sidebar

Legends in MATLAB

How to modify the legend in MATLAB graphs. Seven examples of how to move, color, and hide the legend.

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

fig = figure;
plot([1 2 3 4 5 6 7 8],[1 2 5 6 3 3 2 5]);
hold on
plot([1 2 3 4 5 6 7 8],[1 6 2 3 4 7 7 8]);
legend('blue trace','orange trace');

%--PLOTLY--%

% Strip MATLAB style by default!
response = fig2plotly(fig, 'filename', 'matlab-legend-inside');
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

fig = figure;
plot([1 2 3 4 5 6 7 8],[1 2 5 6 3 3 2 5]);
hold on
plot([1 2 3 4 5 6 7 8],[1 6 2 3 4 3 7 8]);
legend('blue trace','orange trace','Location','BestOutside');

%--PLOTLY--%

% Strip MATLAB style by default!
response = fig2plotly(fig, 'filename', 'matlab-legend-outside');
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

trace1 = struct(...
  'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
  'y', [0, 3, 6, 4, 5, 2, 3, 5, 4], ...
  'type', 'scatter');
trace2 = struct(...
  'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
  'y', [0, 4, 7, 8, 3, 6, 3, 3, 4], ...
  'type', 'scatter');
data = {trace1, trace2};
layout = struct('showlegend', false);
response = plotly(data, struct('layout', layout, 'filename', 'legend-visibility', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

trace1 = struct(...
  'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
  'y', [0, 3, 6, 4, 5, 2, 3, 5, 4], ...
  'name', 'Blue Trace', ...
  'type', 'scatter');
trace2 = struct(...
  'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
  'y', [0, 4, 7, 8, 3, 6, 3, 3, 4], ...
  'name', 'Orange Trace', ...
  'type', 'scatter');
data = {trace1, trace2};
response = plotly(data, struct('filename', 'legend-labels', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

trace1 = struct(...
  'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
  'y', [0, 3, 6, 4, 5, 2, 3, 5, 4], ...
  'type', 'scatter');
trace2 = struct(...
  'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
  'y', [0, 4, 7, 8, 3, 6, 3, 3, 4], ...
  'type', 'scatter');
data = {trace1, trace2};
layout = struct(...
    'showlegend', true, ...
    'legend', struct(...
      'x', 1, ...
      'y', 1));
response = plotly(data, struct('layout', layout, 'filename', 'legend-inside', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

trace1 = struct(...
  'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
  'y', [0, 3, 6, 4, 5, 2, 3, 5, 4], ...
  'type', 'scatter');
trace2 = struct(...
  'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
  'y', [0, 4, 7, 8, 3, 6, 3, 3, 4], ...
  'type', 'scatter');
data = {trace1, trace2};
layout = struct(...
    'showlegend', true, ...
    'legend', struct(...
      'x', 100, ...
      'y', 1));
response = plotly(data, struct('layout', layout, 'filename', 'legend-outside', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

trace1 = struct(...
  'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
  'y', [0, 3, 6, 4, 5, 2, 3, 5, 4], ...
  'type', 'scatter');
trace2 = struct(...
  'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
  'y', [0, 4, 7, 8, 3, 6, 3, 3, 4], ...
  'type', 'scatter');
data = {trace1, trace2};
layout = struct('legend', struct(...
      'x', 0, ...
      'y', 1, ...
      'traceorder', 'normal', ...
      'font', struct(...
        'family', 'sans-serif', ...
        'size', 12, ...
        'color', '#000'), ...
      'bgcolor', '#E2E2E2', ...
      'bordercolor', '#FFFFFF', ...
      'borderwidth', 2));
response = plotly(data, struct('layout', layout, 'filename', 'legend-style', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

trace1 = struct(...
  'x', [0, 1, 2], ...
  'y', [1, 2, 3], ...
  'name', 'First Trace', ...
  'showlegend', false, ...
  'type', 'scatter');
trace2 = struct(...
  'x', [0, 1, 2, 3], ...
  'y', [8, 4, 2, 0], ...
  'name', 'Second Trace', ...
  'showlegend', true, ...
  'type', 'scatter');
data = {trace1, trace2};
response = plotly(data, struct('filename', 'show-legend', 'fileopt', 'overwrite'));
plot_url = response.url
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.