动态图例(每次递归更新) [英] Dynamic Legend (Updates in every recursion)
问题描述
我得到了for i=1:15
.在内部,我生成了一个变量d=1:0.01:10
,它是x'x轴,基于此,我创建了一个连续函数F(d),该函数具有2个唯一的螺距和偏航变量.然后,在每个使用cmap = hsv(15);
的递归中,使用不同的颜色对此进行绘制.就是这样:
I got a for i=1:15
. Inside I generate a variable d=1:0.01:10
, which is the x'x axis and based on this, I create a continuous function F(d) which has 2 unique variables pitch and yaw. I then plot this using different colors in every recursion using cmap = hsv(15);
. So then it is:
d=1:0.01:10;
cmap = hsv(15);
for i=1:15
pitch = unidrnd(10);
yaw = unidrnd(10);
for j=1:length(d)
F(j) = d(j)*3*pitch*yaw; %// some long calculation here
end
p1 = plot(d,F,'Linewidth', 1.0);
title ('blah blah')
set(p1, 'Color', cmap(i,:));
hold on;
legend (['pitch,yaw:', num2str(pitch) num2str(yaw)])
end
hold off;
此代码在每次递归中都更新了唯一的俯仰,偏航值(它们之间没有空格,因此有点恼人),但未能:
This code updates the unique pitch, yaw values in every recursion (without space between them so it is kind irritating) but fails to:
- 应用适当的颜色(在图中可见).
- 保留上一次迭代的颜色和
pitch,yaw
的值.
- Apply the proper color, visible in the figure.
- Hold the color from the previous iteration and the values of
pitch,yaw
.
推荐答案
半文档解决方案
通过动态图例"可以在循环中向图例添加行,如undocumentedmatlab.com上所述.
想法是将legend
命令替换为:
legend('-DynamicLegend');
然后使用DisplayName
参数更新plot
命令:
Then update the plot
command with a DisplayName
parameter:
plot(d,F,'Linewidth',1.0,'DisplayName',sprintf('pitch,yaw: %d,%d',pitch,yaw));
plot(d,F,'Linewidth',1.0,'DisplayName',sprintf('pitch,yaw: %d,%d',pitch,yaw));
然后将添加到轴的图添加到图例:
Then plots that are added to the axes get added to the legend:
如果半文档记录的功能不是您的最佳选择,请使用DisplayName
技巧,只需打开/关闭legend
.也就是说,代替-DynamicLegend
:
If semi-documented features are not your cup of tea, use the DisplayName
trick and simply toggle the legend
off/on. That is, instead of -DynamicLegend
:
legend('off'); legend('show');
不使用DisplayName
或-DynamicLegend
的不同变体是使用以下命令删除并重新创建图例存储的字符串数组.
A different variation that does not use either DisplayName
or -DynamicLegend
is to delete and recreate the legend with an array of stored strings.
MathWorks推荐的官方解决方案,因此它可以抓住现有图例的线手柄,并使用这些手柄手动更新图例.与上面的动态图例解决方案相比,这非常痛苦:
The official solution recommended by MathWorks it so grab the existing legends` line handles and manually update the legend with those handles. This is pretty painful by comparison to the dynamic legend solution above:
% Get object handles
[LEGH,OBJH,OUTH,OUTM] = legend;
% Add object with new handle and new legend string to legend
legend([OUTH;p1],OUTM{:},sprintf('pitch,yaw: %d,%d',pitch,yaw))
这篇关于动态图例(每次递归更新)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!