繁体   English   中英

我们可以将“文字”功能的图例添加到matlab吗?

[英]Can we add legend for function “text” to matlab?

我想为我的剧情添加图例。 因为我要使用标记图“心脏服”,所以使用“文本”功能。 如果我在代码中添加了图例功能,它将无法正常工作。 命令窗口将显示“警告:绘制为空”。 那么,我们可以在“文本”功能中添加图例吗? 我搜索了很多资源,但找不到。

clear all;
clc;
m = '\heartsuit';
x = 0:pi/5:2*pi;
y = sin(x);    
text(x,y,m,'fontname','Arial','color','red','FontSize',18,'HorizontalAlignment','center','VerticalAlignment','middle');
grid on;
xlim([min(x) max(x)])    
ylim([min(y) max(y)])
legend('Solusi Numerik');

这是一个hack。 绘制一个假的NaN点,为其创建图例,隐藏其图例行,然后在字符串中的适当位置的适当位置添加心形西装。 如果需要,调整心形西装和/或细绳的颜色。

hold on;
LgdStr = 'Solusi Numerik';        %Your legend string
hNaN = plot(NaN,NaN);             %Plotting nothing
[~, icons] = legend(hNaN, LgdStr);%Creating a legend to get required space for string
icons(2).Visible = 'off';         %Hiding the fake legend line
icons(1).Position(1) = 0.125;     %Adjusting the starting position of text
icons(1).String = ['\color{red}', m, '   \color{black}',LgdStr];
%Last line includes red-colored heart-suit at reasonable space from black-colored text 

结果:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM