How to add extra text to plot legend?

조회 수: 93 (최근 30일)
Giuseppe
Giuseppe 2022년 3월 11일
답변: Ron Fredericks 2025년 1월 28일
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):
Can you help me to codify this task?
  댓글 수: 2
KSSV
KSSV 2022년 3월 11일
You can try legend title.
Giuseppe
Giuseppe 2022년 3월 11일
Thanks! Is it possible to put the title below the legend instead of above?

댓글을 달려면 로그인하십시오.

채택된 답변

Kiran Felix Robert
Kiran Felix Robert 2022년 3월 15일
Hi Guiseppe,
Please refer the legend properties documentation and the following MATLAB Answer post for legend positioning.

추가 답변 (1개)

Ron Fredericks
Ron Fredericks 2025년 1월 28일
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']);

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by