添加图例Matlab图 [英] add legend matlab plot
问题描述
我有一个matlab函数myfunction(var1,var2)
,该函数为我提供了带有图例的图.我想在一张图中使用此函数生成更多图.到目前为止,这是有效的,即一张图中的2个图具有:
I have a matlab function myfunction(var1,var2)
which gives me a plot with a legend. I would like to generate more plots with this function in one graph. This works so far i.e. for 2 plots in one graph with:
myfunction(a,b)
hold on
myfunction(c,d)
hold off
这里的问题是为每个图生成的图例具有:
The problem here is that the legend which is generated for each plot with:
legend(sprintf('%s%s',var1,' on ',var2))
legend('boxoff')
仅出现在myfunction
的最后一个实例中(在本例中是myfunction(c,d)
,在图例中只有一行显示'c on d',但我想用' a on b'和'c on d')
is only appearing for the last instance of myfunction
(in this case with myfunction(c,d)
, there would be only one line of legend where it says 'c on d', but I would like to have two lines with 'a on b' and 'c on d')
那么如何在不覆盖图例的情况下将其添加到图例中呢?
So how can I add something to the legend without overwriting it?
推荐答案
Another possibility is to use the dynamic legend (not so-well documented) feature of MATLAB. In your case, instead of passing the legend captions to the legend
function, you'll need to use them as input arguments to the plot
function:
plot(x, y, 'DisplayName', 'caption');
,然后使用以下语法添加图例:
and then add the legend using the following syntax:
lh = legend('-DynamicLegend');
set(lh, 'Box', 'off');
但是,如果您有大量图形要尽快绘制在同一图上,则我不建议采用这种方法,例如在一个循环中.在这些情况下,动态图例可能会严重影响性能.
However, I wouldn't suggest this approach if you have a large number of graphs to be rendered on the same plot as fast as possible, e.g. in a loop. In these cases the dynamic legend might cause a significant performance hit.
这篇关于添加图例Matlab图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!