文本框中的彩色长线

Long colored line in textbox

我想在文本框中指示绘图中不同文本颜色的含义。

这里是示例代码:

figure;
bar([1,2,3])
ylim([0 5]); text(1,2,'{\color{blue} apples} {\color{red} pears}');
annotation('textbox',[.2 .6 .3 .3],'String',{'yes','no'},'FitBoxToText','on' );

我要找的只是一个合适的符号,例如长粗线或正方形,以便在 'yes' 和 'no' 前面的文本框中将其用作颜色标记。类似于图例中的彩色线条。我如何在 MATLAB 文本框中实现它?

注:None中的特殊字符来自MATLAB webpage好像对我有用

我提供了一些替代方案,但对我来说,项目符号似乎适合您提到的 link 中列出的特殊字符。检查以下结果:

figure;
bar([1,2,3])
ylim([0 5]); text(1,2,'{\color{blue} apples} {\color{red} pears}');
annotation('textbox',[0.2 0.6 0.3 0.3],'String',{['{\color{blue}','\bullet','} yes'],...
    ['{\color{red}','\bullet','} no']},'FitBoxToText','on');

给出:


如果您是 unicode 的粉丝,您将拥有更多的自由。您可以插入连字符 (-)、破折号 (—)、方块 (■)、项目符号 (•) 中的任何一个,这样的例子不胜枚举。

char(8212) 给出长划线,char(9632) 给出正方形,char(8226) 给出子弹。想用哪个就用哪个。

figure;
bar([1,2,3])
ylim([0 5]); text(1,2,'{\color{blue} apples} {\color{red} pears}');
annotation('textbox',[0.2 0.6 0.3 0.3],'String',{['{\color{blue}',char(8212),'} yes'],...
    ['{\color{red}',char(9632),'} no']},'FitBoxToText','on'); 

给出:


或者您可以操纵 legend 生成所需的结果,如下所示:

figure;
plot(NaN,NaN,'b',NaN,NaN,'r'); hold on;    %The trick
bar([1,2,3])
ylim([0 5]); text(1,2,'{\color{blue} apples} {\color{red} pears}');
legend({'yes','no'},'Location','northwest');

给出: