Show Sidebar Hide Sidebar

Text and Annotations in MATLAB

How to add text labels and annotations to plots in MATLAB.

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

x = -pi:pi/10:pi;
y = sin(x);
figure('Name', 'Sample graph'), plot(x, y, '--rs');
% Label some points
for i=8:size(x,2)-8
text(x(i), y(i), 'Text');
end

% PLOTLY
response = fig2plotly();
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], ...
  'y', [1, 1, 1], ...
  'mode', 'lines+markers+text', ...
  'name', 'Lines, Markers and Text', ...
  'text', { {'Text A', 'Text B', 'Text C'} }, ...
  'textposition', 'top', ...
  'type', 'scatter');
trace2 = struct(...
  'x', [0, 1, 2], ...
  'y', [2, 2, 2], ...
  'mode', 'markers+text', ...
  'name', 'Markers and Text', ...
  'text', { {'Text D', 'Text E', 'Text F'} }, ...
  'textposition', 'bottom', ...
  'type', 'scatter');
trace3 = struct(...
  'x', [0, 1, 2], ...
  'y', [3, 3, 3], ...
  'mode', 'lines+text', ...
  'name', 'Lines and Text', ...
  'text', { {'Text G', 'Text H', 'Text I'} }, ...
  'textposition', 'bottom', ...
  'type', 'scatter');
data = {trace1, trace2, trace3};
layout = struct('showlegend', false);
response = plotly(data, struct('layout', layout, 'filename', 'text-chart-basic', '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

x = 1:10:100
fig = figure;
plot(x, (x+1).^2, 'bo','markers',14)
title('hover over the markers to see the coordinates');
response = fig2plotly(fig, 'strip',false);

data = cell2struct(data,'data',1);
data.mode = 'markers+text'; % Able the data cursor

response2 = plotly(data, response.layout);
plotly_url = response2.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

data = {...
  struct(...
    'x', [0, 1, 2], ...
    'y', [1, 3, 2], ...
    'mode', 'markers', ...
    'text', { {'Text A', 'Text B', 'Text C'} }, ...
    'type', 'scatter')...
};
layout = struct('title', 'Hover over the points to see the text');
response = plotly(data, struct('layout', layout, 'filename', 'hover-chart-basic', '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, 1, 3, 2, 4, 3, 4, 6, 5], ...
  'type', 'scatter');
trace2 = struct(...
  'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
  'y', [0, 4, 5, 1, 2, 2, 3, 4, 2], ...
  'type', 'scatter');
data = {trace1, trace2};
layout = struct(...
    'showlegend', false, ...
    'annotations', { {...
      struct(...
        'x', 2, ...
        'y', 5, ...
        'xref', 'x', ...
        'yref', 'y', ...
        'text', 'Annotation Text', ...
        'showarrow', true, ...
        'arrowhead', 7, ...
        'ax', 0, ...
        'ay', -40)...
    } });
response = plotly(data, struct('layout', layout, 'filename', 'simple-annotation', '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

x = 1:10; y = 1:10; 
fig = figure;
hold on
scatter(x,y);
a = [1:10]'; b = num2str(a); c = cellstr(b);
dx = 0.1; dy = 0.1; % displacement so the text does not overlay the data points
text(x+dx, y+dy, c);

plot([1 1],[2 3])
text(1.2,2.2,'A Line')
xlim([-1 5])
ylim([-1 5])
hold off

resp = fig2plotly(fig, 'strip',false)
plotly_url = resp.url

Insipired from Matlab Docs
% 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, 1, 3, 2, 4, 3, 4, 6, 5], ...
  'type', 'scatter');
trace2 = struct(...
  'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
  'y', [0, 4, 5, 1, 2, 2, 3, 4, 2], ...
  'type', 'scatter');
data = {trace1, trace2};
layout = struct(...
    'showlegend', false, ...
    'annotations', { {...
      struct(...
        'x', 2, ...
        'y', 5, ...
        'xref', 'x', ...
        'yref', 'y', ...
        'text', 'Annotation Text', ...
        'showarrow', true, ...
        'arrowhead', 7, ...
        'ax', 0, ...
        'ay', -40), ...
      struct(...
        'x', 4, ...
        'y', 4, ...
        'xref', 'x', ...
        'yref', 'y', ...
        'text', 'Annotation Text 2', ...
        'showarrow', true, ...
        'arrowhead', 7, ...
        'ax', 0, ...
        'ay', -40)...
    } });
response = plotly(data, struct('layout', layout, 'filename', 'multiple-annotation', '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, 1, 1], ...
  'mode', 'lines+markers+text', ...
  'name', 'Lines, Markers and Text', ...
  'text', { {'Text A', 'Text B', 'Text C'} }, ...
  'textposition', 'top right', ...
  'textfont', struct(...
    'family', 'sans serif', ...
    'size', 18, ...
    'color', '#1f77b4'), ...
  'type', 'scatter');
trace2 = struct(...
  'x', [0, 1, 2], ...
  'y', [2, 2, 2], ...
  'mode', 'lines+markers+text', ...
  'name', 'Lines and Text', ...
  'text', { {'Text G', 'Text H', 'Text I'} }, ...
  'textposition', 'bottom', ...
  'textfont', struct(...
    'family', 'sans serif', ...
    'size', 18, ...
    'color', '#ff7f0e'), ...
  'type', 'scatter');
data = {trace1, trace2};
layout = struct('showlegend', false);
response = plotly(data, struct('layout', layout, 'filename', 'text-chart-styling', '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, 1, 3, 2, 4, 3, 4, 6, 5], ...
  'type', 'scatter');
trace2 = struct(...
  'x', [0, 1, 2, 3, 4, 5, 6, 7, 8], ...
  'y', [0, 4, 5, 1, 2, 2, 3, 4, 2], ...
  'type', 'scatter');
data = {trace1, trace2};
layout = struct(...
    'showlegend', false, ...
    'annotations', { {...
      struct(...
        'x', 2, ...
        'y', 5, ...
        'xref', 'x', ...
        'yref', 'y', ...
        'text', 'max=5', ...
        'showarrow', true, ...
        'font', struct(...
          'family', 'Courier New, monospace', ...
          'size', 16, ...
          'color', '#ffffff'), ...
        'align', 'center', ...
        'arrowhead', 2, ...
        'arrowsize', 1, ...
        'arrowwidth', 2, ...
        'arrowcolor', '#636363', ...
        'ax', 20, ...
        'ay', -30, ...
        'bordercolor', '#c7c7c7', ...
        'borderwidth', 2, ...
        'borderpad', 4, ...
        'bgcolor', '#ff7f0e', ...
        'opacity', 0.8)...
    } });
response = plotly(data, struct('layout', layout, 'filename', 'style-annotation', 'fileopt', 'overwrite'));
plot_url = response.url
Still need help?
Contact Us

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