How to add extra text to plot legend?
198 views (last 30 days)
Show older comments
Giuseppe
on 11 Mar 2022
Answered: Ron Fredericks
on 28 Jan 2025 at 0:03
Hi, guys
Let us suppose that I have the following simple code:
clc; clear all; close all;
x = linspace(0, 2*pi, 100);
figure
hold on;grid on; box on;
plot(x,sin(x));
plot(x, cos(x));
plot(x, tan(x));
axis([0 2*pi -4 4]);
legend('sin', 'cos', 'tan');
My goal is to add some extra text in the legend box as shown in the following image example (image modified with gimp):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/922799/image.png)
Can you help me to codify this task?
2 Comments
Accepted Answer
Kiran Felix Robert
on 15 Mar 2022
Hi Guiseppe,
Please refer the legend properties documentation and the following MATLAB Answer post for legend positioning.
0 Comments
More Answers (1)
Ron Fredericks
on 28 Jan 2025 at 0:03
You can append extra text to a legend using a hidden marker. You can also optionally add an extra line seperating the text from the standard legend objects using newline.
Code:
clc; clear all; close all;
x = linspace(0, 2*pi, 100);
figure
hold on;grid on; box on;
plot(x,sin(x));
plot(x, cos(x));
plot(x, tan(x));
axis([0 2*pi -4 4]);
% Code to append text to legend
plot(0, 0, "o", 'color', 'none', 'MarkerSize', 10);
legend('sin', 'cos', 'tan', [newline 'Extra Text Here']);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1824381/image.jpeg)
0 Comments
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!