Matlab动态图例/图例“保持"喜欢行为 [英] Matlab dynamic legend / legend "hold on" like behavior
问题描述
只想添加更多数据就可以删除图例.就像传说中的坚持"
示例:
plotData
=绘图数据数组,例如plotData(i)= plot(...
N
= plotData的大小.
代码:
i = 1:Nstr = sprintf('我的情节y%d',i);%legendData(:,i)= [plotData;str];%#ok< SAGROW>%[〜,〜,〜,current_entries] =图例;%legend([[current_entries [plotData; str]]));这里没有成功%此命令将删除前一个命令.图例(plotData,str);结尾图例([plotX1,plotX2],'x 1','x 2');
我想我可以从循环中存储图例信息,并以某种方式将其添加到最后一行,例如:
legend(DATAFROMLOOP ?? [plotX1,plotX2],'x 1','x 2');
这是一个可能的解决方案,但我不知道该怎么做.
您要设置
您可以轻松地将其合并到循环中:
%在一个循环中创建10个图N = 10;%预分配图形对象hplots = gobject(N,1);对于k = 1:Nhplot(k)= plot(rand(10,1),'DisplayName',sprintf('My plot y%d',k));结尾图例(hplot);
Just want to add more data do a legend without erasing it. Like a legend "hold on"
Sample:
plotData
= array of plot data, like plotData(i) = plot(...
N
= size of plotData.
Code:
for i = 1:N
str = sprintf('My plot y %d', i);
%legendData(:,i) = [plotData; str]; %#ok<SAGROW>
%[~,~,~,current_entries] = legend;
%legend([current_entries [plotData; str]]); no sucess here
% This command will erase the previous one.
legend(plotData,str);
end
legend([plotX1,plotX2],'x 1','x 2');
I think I can store the legend info from the loop and add it some way to the final line, something like:
legend(DATAFROMLOOP?? [plotX1,plotX2],'x 1','x 2');
This is a possible solution, but I don't know how to do it.
You want to set the DisplayName
property of your plot objects and then call legend
once when you are done plotting everything. legend
will automatically retrieve the strings from the DisplayName
property to populate the legend.
hplot1 = plot(rand(10,1), 'DisplayName', 'plot1');
hplot2 = plot(rand(10,1), 'DisplayName', 'plot2');
legend([hplot1, hplot2]);
You can easily incorporate this into a loop:
% Create 10 plots within a loop
N = 10;
% Pre-allocate graphics objects
hplots = gobject(N, 1);
for k = 1:N
hplot(k) = plot(rand(10, 1), 'DisplayName', sprintf('My plot y %d', k));
end
legend(hplot);
这篇关于Matlab动态图例/图例“保持"喜欢行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!