首页 > 解决方案 > 我们可以将函数“文本”的图例添加到 matlab 吗?

问题描述

我想为我的情节添加图例。因为我想使用标记图“heartsuit”,所以我使用了“text”函数。如果我在我的代码中添加图例函数,它就无法工作。命令窗口显示“警告:绘图为空”。那么,我们可以在“文本”函数中添加图例吗?我搜索了很多来源,我找不到它。

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');

标签: matlablegend

解决方案


这是一个黑客。绘制一个假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 

结果:


推荐阅读