# Text and Annotations in MATLAB®

How to make Text and Annotations plots in MATLAB® with Plotly.

## Adding Text to Plots with the text Function

x = -pi:pi/10:pi;
y = sin(x);
figure('Name', 'Sample graph'), plot(x, y, '--rs');

for i=8:size(x,2)-8
text(x(i), y(i), 'Text');
end

fig2plotly(gcf);


Similarly, here is an example for line and a scatter plot:

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

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

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

fig2plotly(fig);


## Enable Hover

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

plotly(data, response.layout);

"To be inmplemented soon"


Similarly, here is an example for line and a scatter plot:

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

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


## Styling and Coloring Annotations

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', ...
'arrowsize', 1, ...
'arrowwidth', 2, ...
'arrowcolor', '#636363', ...
'ax', 20, ...
'ay', -30, ...
'bordercolor', '#c7c7c7', ...
'borderwidth', 2, ...
'bgcolor', '#ff7f0e', ...
'opacity', 0.8)...
} });

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


Here is an example with custom text size, plot and text color:

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

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


## Single Annotation

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, ...
'ax', 0, ...
'ay', -40)...
} });

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


## Multiple Annotations

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, ...
'ax', 0, ...
'ay', -40), ...
struct(...
'x', 4, ...
'y', 4, ...
'xref', 'x', ...
'yref', 'y', ...
'text', 'Annotation Text 2', ...
'showarrow', true, ...