Matlab动态图例/图例 "hold on"类似行为

标签 matlab plot matlab-figure

只是想添加更多数据做一个图例而不删除它。就像传说中的“坚持住”

示例:

plotData = 绘图数据数组,例如plotData(i) =plot(...

N = 绘图数据的大小。

代码:

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

我想我可以存储循环中的图例信息并将其添加到最后一行,例如:

legend(DATAFROMLOOP?? [plotX1,plotX2],'x 1','x 2');

这是一个可能的解决方案,但我不知道该怎么做。

最佳答案

您想要设置 DisplayName绘制对象的属性,然后在完成绘制所有内容后调用一次legendlegend 会自动从 DisplayName 属性中检索字符串来填充图例。

hplot1 = plot(rand(10,1), 'DisplayName', 'plot1');
hplot2 = plot(rand(10,1), 'DisplayName', 'plot2');

legend([hplot1, hplot2]);

enter image description here

您可以轻松地将其合并到循环中:

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

enter image description here

关于Matlab动态图例/图例 "hold on"类似行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36203272/

相关文章:

algorithm - 如何根据强度值创建蒙版或检测图像部分?

matlab - 如何在matlab图形的x轴上重复相同的值?

python - 合并/连接具有不同元素的数组

python - matplotlib for 循环显示、保存和重绘所有图

matlab - 拟随机集和多元正态分布matlab

plot - 如何使用 matplotlib 以 mm/cm 为单位指定绘图的边距?

plot - gnuplot 中参数平面的大小

matlab - 如何在 Matlab 中为非当前图形创建轴?

matlab - imshow 显示与 imwrite 不同的输出

c++ - 在 MATLAB 中打开包含混合数据的 .gz 文件