matlab - 我们可以将函数“文本”的图例添加到 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');
解决方案
这是一个黑客。绘制一个假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
结果:
推荐阅读
- python - 缩短 Sphinx 文档中的交叉引用
- ionic-framework - 来自 getBoundingClientRect() 的坐标在 Ionic 3 应用程序上已关闭
- c - 如何交换单位矩阵的第一行和最后一行?
- google-chrome - 在 Google Apps 脚本中捕获 #REF 错误
- javascript - 尝试将新属性添加到 redux 状态时,javascript map.set 不起作用
- elasticsearch - Kibana 在非索引字段上显示聚合,此映射是否有效?
- bash - unix/macos - 如何查找所有文件并使用不同的扩展名复制
- python - 如何修复 numpy 数组中的“IndexError:仅整数”?
- bash - Rsync在传输前计算文件数?
- python-3.x - Python如何从服务器转发消息