Matlab 图中的希腊字母
Greek letter in Matlab plot
我已经在 Matlab 中创建了一个绘图,现在我想使用以下命令添加一个图例:
legend({'Pos1', 'Pos2', 'Pos3', '\alpha Pos4'}, 'Location', 'northeast', 'Interpreter', 'latex', 'fontsize', 22);
legend('boxoff')
问题是\alpha
没有转换成希腊字母。如果我省略大括号 {} 那么它可以工作,但我需要它们,因为我只想标记前四行。
如何获得希腊字母 alpha?
你忘记了 $
legend({'Pos1', 'Pos2', 'Pos3', '$\alpha$ Pos4'}, 'Location', 'northeast', 'Interpreter', 'latex', 'fontsize', 22);
我想扩展 Daniel 的回答并解释一些细节。
没有 {}
会怎样
当元胞数组中 未 指定图例条目时,只有属性 Location
和 Orientation
可用于直接调用 legend
。如果存在其他属性,它们将被解释为图例条目。这意味着 Interpreter
和 TextSize
并且它的值将是图例条目。解决 Adiel 关于为什么 在没有 {}
的情况下显然有效 的评论:实际上并非如此,由于上述原因,它甚至会间接发出警告。
旁注: 根据语法,图例条目必须在属性之前提供。尽管如此,它确实可以按任何顺序工作,但我不建议使用这种未记录的行为。
选择地块
你提到你必须使用 {}
到 select 只有前四行。这是不正确的,因为默认情况下 legend
select 第一个 N 绘图。问题是属性得到了如上所述的解释。对于 select 个特定图,您可以使用图句柄省略第二个图:
legend([ph1,ph3,ph4,ph5], 'Pos1', 'Pos3', 'Pos4', 'Pos5');
使用其他属性
为了能够 直接 在对 legend
的调用中使用其他属性,您可以将图例条目作为元胞数组提供。这将条目与属性的名称-值对分离。例如更改字体大小:
legend({'Pos1', 'Pos2', 'Pos3', 'Pos4'}, 'Fontsize', 22);
另一种可能性是在不使用元胞数组的情况下使用句柄来设置其他属性:
l = legend('Pos1', 'Pos2', 'Pos3', 'Pos4');
set(l, 'Fontsize', 22); % using the set-function
l.FontSize = 22; % object oriented
latex
-解释器
如果将 Interpreter
设置为 latex
则图例条目的所有内容都需要由 latex 编译。这意味着 \alpha
不能在数学环境之外使用。要在 LaTeX 中添加内联数学表达式,您可以用 $
符号将其括起来。所以 $\alpha$
就像丹尼尔的回答中提到的那样工作。使用 tex
-解释器,Matlab 使用 TeX 标记的子集并自动处理支持的特殊字符,因此当您不使用 [=25= 时,就不需要 $...$
]解释器。
建议
- 不要忘记
$
标志。
- 在调用
legend
. 中解决具体情节
- 使用元胞数组并直接设置
legend
调用中的所有属性。
- 使用
...
可以将长行分成几行。
例如像这样:
legend([ph1,ph3,ph4,ph5], ...
{'Pos $\alpha$', 'Pos $\beta$', 'Pos $\gamma$', 'Pos $\delta$'}, ...
'Location', 'northeast', 'Interpreter', 'latex', 'FontSize', 22);
这是示例的完整代码:
figure; hold on;
ph1 = plot(0,-1,'*'); ph2 = plot(0,-2,'*');
ph3 = plot(0,-3,'*'); ph4 = plot(0,-4,'*');
ph5 = plot(0,-5,'*'); ph6 = plot(0,-6,'*');
legend([ph1,ph3,ph4,ph5], ...
{'Pos $\alpha$', 'Pos $\beta$', 'Pos $\gamma$', 'Pos $\delta$'}, ...
'Location', 'northeast', 'Interpreter', 'latex', 'FontSize', 22);
结果为:
我已经在 Matlab 中创建了一个绘图,现在我想使用以下命令添加一个图例:
legend({'Pos1', 'Pos2', 'Pos3', '\alpha Pos4'}, 'Location', 'northeast', 'Interpreter', 'latex', 'fontsize', 22);
legend('boxoff')
问题是\alpha
没有转换成希腊字母。如果我省略大括号 {} 那么它可以工作,但我需要它们,因为我只想标记前四行。
如何获得希腊字母 alpha?
你忘记了 $
legend({'Pos1', 'Pos2', 'Pos3', '$\alpha$ Pos4'}, 'Location', 'northeast', 'Interpreter', 'latex', 'fontsize', 22);
我想扩展 Daniel 的回答并解释一些细节。
没有 {}
会怎样
当元胞数组中 未 指定图例条目时,只有属性 Location
和 Orientation
可用于直接调用 legend
。如果存在其他属性,它们将被解释为图例条目。这意味着 Interpreter
和 TextSize
并且它的值将是图例条目。解决 Adiel 关于为什么 在没有 {}
的情况下显然有效 的评论:实际上并非如此,由于上述原因,它甚至会间接发出警告。
旁注: 根据语法,图例条目必须在属性之前提供。尽管如此,它确实可以按任何顺序工作,但我不建议使用这种未记录的行为。
选择地块
你提到你必须使用 {}
到 select 只有前四行。这是不正确的,因为默认情况下 legend
select 第一个 N 绘图。问题是属性得到了如上所述的解释。对于 select 个特定图,您可以使用图句柄省略第二个图:
legend([ph1,ph3,ph4,ph5], 'Pos1', 'Pos3', 'Pos4', 'Pos5');
使用其他属性
为了能够 直接 在对 legend
的调用中使用其他属性,您可以将图例条目作为元胞数组提供。这将条目与属性的名称-值对分离。例如更改字体大小:
legend({'Pos1', 'Pos2', 'Pos3', 'Pos4'}, 'Fontsize', 22);
另一种可能性是在不使用元胞数组的情况下使用句柄来设置其他属性:
l = legend('Pos1', 'Pos2', 'Pos3', 'Pos4');
set(l, 'Fontsize', 22); % using the set-function
l.FontSize = 22; % object oriented
latex
-解释器
如果将 Interpreter
设置为 latex
则图例条目的所有内容都需要由 latex 编译。这意味着 \alpha
不能在数学环境之外使用。要在 LaTeX 中添加内联数学表达式,您可以用 $
符号将其括起来。所以 $\alpha$
就像丹尼尔的回答中提到的那样工作。使用 tex
-解释器,Matlab 使用 TeX 标记的子集并自动处理支持的特殊字符,因此当您不使用 [=25= 时,就不需要 $...$
]解释器。
建议
- 不要忘记
$
标志。 - 在调用
legend
. 中解决具体情节
- 使用元胞数组并直接设置
legend
调用中的所有属性。 - 使用
...
可以将长行分成几行。
例如像这样:
legend([ph1,ph3,ph4,ph5], ...
{'Pos $\alpha$', 'Pos $\beta$', 'Pos $\gamma$', 'Pos $\delta$'}, ...
'Location', 'northeast', 'Interpreter', 'latex', 'FontSize', 22);
这是示例的完整代码:
figure; hold on;
ph1 = plot(0,-1,'*'); ph2 = plot(0,-2,'*');
ph3 = plot(0,-3,'*'); ph4 = plot(0,-4,'*');
ph5 = plot(0,-5,'*'); ph6 = plot(0,-6,'*');
legend([ph1,ph3,ph4,ph5], ...
{'Pos $\alpha$', 'Pos $\beta$', 'Pos $\gamma$', 'Pos $\delta$'}, ...
'Location', 'northeast', 'Interpreter', 'latex', 'FontSize', 22);
结果为: