Wolfram mathematica 如何在mathematica中自动生成函数名?
当我绘制多个函数,如exp、2^x、3^x时,是否可以为每个函数生成一个标签 我现在的代码:Wolfram mathematica 如何在mathematica中自动生成函数名?,wolfram-mathematica,Wolfram Mathematica,当我绘制多个函数,如exp、2^x、3^x时,是否可以为每个函数生成一个标签 我现在的代码: Plot[{Exp[x], 2^x, 3^x}, {x, -5, 2}, AspectRatio -> Automatic, PlotStyle -> {Red, Green, Blue}] 我的意思是在这种情况下生成3个标签来告诉用户它是什么功能 例如: 如何生成此项?一种方法是使用PlotLegends (我不太喜欢它,但这是一种简单的方法来做你想做的事) 自动, PlotSty
Plot[{Exp[x], 2^x, 3^x}, {x, -5, 2}, AspectRatio -> Automatic, PlotStyle -> {Red, Green, Blue}]
我的意思是在这种情况下生成3个标签来告诉用户它是什么功能
例如:
如何生成此项?一种方法是使用
PlotLegends
(我不太喜欢它,但这是一种简单的方法来做你想做的事)
自动,
PlotStyle->{红、绿、蓝},PlotLegend->图例]
有关详细信息,请参见“帮助”以自定义图例。以上使用默认值
当鼠标指针指向功能图时,显示标签的
工具提示的替代方法:
Plot[Tooltip@{Exp[x], 2^x, 3^x}, {x, -5, 2}, AspectRatio -> Automatic,
PlotStyle -> {Red, Green, Blue}]
我不知道为同一个问题添加另一个不同答案的规则是什么。但这是另一种不同的方法。如果我要把这个加到我的第一个答案中,我可以做到
可以使用文本命令手动添加文本标签。我觉得它看起来更好。这里有一个方法:
Clear[x];
funs = {Exp[x], 2^x, 3^x};
funNames = Style[#, 12] & /@ funs;
(*the x-axis plot range used *)
from = -5; to = 2;
(* generate the coordinates at the end of the plot lines*)
pos = Map[{to, #} &, funs /. x -> to];
(*generate the text labels *)
text = Map[Text[#[[1]], #[[2]], {-1, 0}] &, Thread[{funNames, pos}]];
打印最终结果(在打印范围中添加一点填充,以便
添加的标签(完全可见)
更新(1)
萨姆在下面要求一个更简单的方法。我现在不确定。但是,使该方法更易于使用的一种方法是生成一个函数,然后只需调用该函数一次即可生成文本标签。您可以将这个函数放在您一直使用的所有其他函数的位置,然后调用它
这里有一点:首先编写函数
(*version 1.1*)
myLegend[funs_List, (*list of functions to plot*)
x_, (*the independent variable*)
from_?(NumericQ[#] && Im[#] == 0 &),(*the x-axis starting plot range*)
to_?(NumericQ[#] && Im[#] == 0 &) (*the x-axis ending plot range*)
] := Module[{funNames, pos, text, labelOffset = -1.3},
(*make label names*)
funNames = Style[#, 12] & /@ funs;
(*generated the coordinates at the end of the plot lines*)
pos = Map[{to, #} &, funs /. x -> to];
(*generate the Text calls*)
text = Map[Text[#[[1]], #[[2]], {labelOffset, 0}] &,
Thread[{funNames, pos}]]
];
现在只要调用上面的任何时候你想打印标签。这将只是额外的1-2行代码。像这样:
Clear[x]
from = -5; to = 2;
funs = {Exp[x], 2^x, 3^x};
Plot[funs, {x, from, to}, PlotRangePadding -> {1, 1},
PlotStyle -> {Red, Green, Blue}, PlotRange -> All,
Epilog -> myLegend[funs, x, from, to]]
以下是几个例子:
您可以根据需要对其进行修改。也许可以这样做:使用绘图中的工具提示
生成带有工具提示的图形
对象。然后重写工具提示,将所需文本放置在所需位置:
Plot[
Tooltip@{Exp[x], 2^x, 3^x}, {x, -5, 2},
AspectRatio -> Automatic,
PlotStyle -> {Red, Green, Blue},
PlotRange -> All,
PlotRangePadding -> 1.1] /.
{
Tooltip[{_, color_, line_}, tip_]
:>
{Text[Style[tip, 14], {.25, 0} + line[[1, -1]]], color, line}
}
虽然它使用图形[]而不是绘图,但可能会给您一些想法。它的可能副本看起来很不错,但对我来说有点太复杂了。有什么简单的方法吗?谢谢~这是对符号图形对象中替换功能的一次非常漂亮和聪明的使用。我们通常也这样做我们所有的后处理。
Clear[x]
from = -5; to = 2;
funs = {Exp[x], 2^x, 3^x};
Plot[funs, {x, from, to}, PlotRangePadding -> {1, 1},
PlotStyle -> {Red, Green, Blue}, PlotRange -> All,
Epilog -> myLegend[funs, x, from, to]]
Plot[
Tooltip@{Exp[x], 2^x, 3^x}, {x, -5, 2},
AspectRatio -> Automatic,
PlotStyle -> {Red, Green, Blue},
PlotRange -> All,
PlotRangePadding -> 1.1] /.
{
Tooltip[{_, color_, line_}, tip_]
:>
{Text[Style[tip, 14], {.25, 0} + line[[1, -1]]], color, line}
}