how to hold legend

45 次查看(过去 30 天)
Elysi Cochin
Elysi Cochin 2013-10-26
how to hold legend... the plot is coming correctly... but the legend is coming only for the last one.... how to hold the legend....
col = hsv(9);
for i = 1:cs
plot(x, y, 'LineWidth', 2, 'color', col(j,:)); hold on
legendInfo{i} = Info{i};
legend(legendInfo, 'Location', 'Best'); hold all
end

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2013-10-26
编辑:Azzi Abdelmalek 2013-10-26
Look at this Example
t=0:0.1:10;
y1=sin(t);
y2=cos(t);
% ------Plot the first data----------
plot(t,y1);
h=legend('word1')
%-------Plot the second data---------
hold on
plot(t,y2);
str=get(h,'string')% get the previous legend
new_leg='word2' % your new legend
h=legend([str new_leg]) % concatenate the new and the previous legend
  2 个评论
Rajani Metri
Rajani Metri 2020-12-5
Sir,
But it is not holding the Marker of those legends. What to do to hold the marker in legend?
Thank you.
Shyam Jani
Shyam Jani 2022-9-1
Hello,
To update the Legend box try this code. The code@Azzi Abdelmalek has written is 80% correct just you need to update few lines (see below)
t=0:0.1:10;
y1=sin(t);
y2=cos(t);
% ------Plot the first data----------
plot(t,y1);
h=legend('word1')
%-------Plot the second data---------
hold on
plot(t,y2);
str=get(h,'string')% get the previous legend
%------These lines you should update--------%
str(end) = [] % delete last element of the cell arrray.
str{end+1} = "word2"; % Adding string
h = legend([str]); % Updating legend box

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by