【问题标题】:Can we add legend for function "text" to matlab?我们可以将函数“文本”的图例添加到 matlab 吗?
【发布时间】:2019-09-07 07:58:34
【问题描述】:

我想为我的情节添加图例。因为我想使用标记图“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');

【问题讨论】:

    标签: matlab legend


    【解决方案1】:

    这是一个技巧。绘制一个假的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 
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-25
      • 2011-09-13
      • 1970-01-01
      • 2021-02-25
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 2019-12-02
      相关资源
      最近更新 更多