添加与图没有任何关系的自定义图例 [英] Add custom legend without any relation to the graph

查看:90
本文介绍了添加与图没有任何关系的自定义图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望插入与图无关的图例:

I wish to insert a legend that is not related to the graph whatsoever:

figure;
hold on;
plot(0,0,'or');
plot(0,0,'ob');
plot(0,0,'ok');
leg = legend('red','blue','black');

现在,我希望将其添加到另一个图形中:

Now I wish to add it to another figure:

figure;
t=linspace(0,10,100);
plot(t,sin(t));
%% ADD THE LEGEND OF PLOT ABOVE 

推荐答案

这是我过去解决此问题的方法:

This is how I have solved this problem in the past:

figure
t=linspace(0,10,100);
plot(t,sin(t));
hold on;

h = zeros(3, 1);
h(1) = plot(NaN,NaN,'or');
h(2) = plot(NaN,NaN,'ob');
h(3) = plot(NaN,NaN,'ok');
legend(h, 'red','blue','black');

这将绘制其他点,但是由于坐标位于NaN,因此它们在图本身上将不可见:

This will plot the additional points, but because the coordinates are at NaN they will not be visible on the plot itself:

编辑26/10/2016:我的原始答案导致2016b中的图例条目变为灰色.上面的更新代码有效,但是下面的答案仅适用于2016b之前的版本:

EDIT 26/10/2016: My original answer results in greyed out legend entries in 2016b. The updated code above works, but the answer below is only relevant pre-2016b:

figure
t=linspace(0,10,100);
plot(t,sin(t));
hold on;

h = zeros(3, 1);
h(1) = plot(0,0,'or', 'visible', 'off');
h(2) = plot(0,0,'ob', 'visible', 'off');
h(3) = plot(0,0,'ok', 'visible', 'off');
legend(h, 'red','blue','black');

这将绘制其他点,但它们在图本身上将不可见.

This will plot the additional points, but they will not be visible on the plot itself.

如果元素很多,也可以使用copyobj将图形元素从一个图形复制到另一个图形,然后在显示图例之前使用set(x, 'visible', 'off')隐藏图形,但这取决于最终应用程序.

You can also use copyobj to copy graphics elements from one figure to another if you have a lot of elements, then use set(x, 'visible', 'off') to hide them before showing the legend, but it depends on what your final application is.

这篇关于添加与图没有任何关系的自定义图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆