如何在mathematica中自动生成函数名?

发布于 2024-12-24 00:56:50 字数 327 浏览 2 评论 0原文

当我绘制多个函数(例如 exp,2^x,3^x)时,是否可以生成每个函数的标签?

我现在的代码:

  Plot[{Exp[x], 2^x, 3^x}, {x, -5, 2}, AspectRatio -> Automatic, PlotStyle -> {Red, Green, Blue}]

我的意思是在本例中生成 3 个标签来告诉用户它是什么功能。

如:

在此处输入图像描述

如何生成这个?

When I draw multiple functions like exp,2^x,3^x, is it possible to generate a label of each function?

My code now:

  Plot[{Exp[x], 2^x, 3^x}, {x, -5, 2}, AspectRatio -> Automatic, PlotStyle -> {Red, Green, Blue}]

What I mean is generate 3 labels in this case to tell the user what function it is.

Such as:

enter image description here

How do you generate this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

中二柚 2024-12-31 00:56:50

也许这可行:在 Plot 中使用 Tooltip 生成带有工具提示的 Graphics 对象。然后重写工具提示,将所需的文本放置在所需的位置:

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}
}

Mathematicagraphics

Perhaps this works: Use Tooltip in Plot to generate a Graphics object with tooltips. Then rewrite the tooltip to place the desired text in the desired location:

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}
}

Mathematica graphics

浪漫之都 2024-12-31 00:56:50

我不确定为同一问题添加另一个不同答案的规则是什么。但这是另一种不同的方法。如果我应该将其添加到我的第一个答案中,我可以这样做。

您可以使用文本命令手动添加文本标签。我认为它看起来更好。这是一种方法:

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}]];

绘制最终结果(在绘制范围中添加一些填充,以便
添加的标签完全可见)

Plot[funs, {x, from, to},
 PlotRangePadding -> {1, 1},
 PlotStyle -> {Red, Green, Blue},
 PlotRange -> All,
 Epilog -> text
 ]

在此处输入图像描述

更新 (1)

Sam 在下面询问一个更简单的方法。我现在不确定。但更容易使用此方法的一种方法是创建一个函数,然后只需调用该函数一次即可生成文本标签。您可以将此函数放在您经常使用的所有其他函数的位置,然后调用它。

这是这样的:首先编写函数

 (*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]]

在此处输入图像描述

以下是一些示例:

在此输入图像描述

您可以根据需要进行修改。

I am not sure what the rules are for adding another, different answer for the same question. But here is another, different way to do it. If I am supposed to add this to my first answer, I can do that.

You can add the text labels, by hand, using Text commands. I think it looks better. Here is one way:

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}]];

Plot the final result (added a little of padding to plot range so that
the labels added are seen completely)

Plot[funs, {x, from, to},
 PlotRangePadding -> {1, 1},
 PlotStyle -> {Red, Green, Blue},
 PlotRange -> All,
 Epilog -> text
 ]

enter image description here

update (1)

Sam asked below for an simpler way. I am not sure now. But one way to make it easier to use this method, is to make a function and then simply call this function once to generate the Text labels. You can put this function where you put all your other functions you use all the time, and just call it.

Here is something: First write the function

 (*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}]]
   ];

And now just call the above any time you want to plot with labels. It will be just 1-2 extra lines of code. like this:

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]]

enter image description here

Here are few examples:

enter image description here

You can modify it as you want.

北陌 2024-12-31 00:56:50

当鼠标指针位于函数图上时使用 Tooltip 显示标签的替代方法:

Plot[Tooltip@{Exp[x], 2^x, 3^x}, {x, -5, 2}, AspectRatio -> Automatic,
                                             PlotStyle -> {Red, Green, Blue}]

Alternative way with Tooltip displaying labels while the mouse pointer is at the function graphs :

Plot[Tooltip@{Exp[x], 2^x, 3^x}, {x, -5, 2}, AspectRatio -> Automatic,
                                             PlotStyle -> {Red, Green, Blue}]
巷子口的你 2024-12-31 00:56:50

一种方法是使用 PlotLegends

(我不太喜欢它,但这是一种简单的方法来实现你想要的)

<< PlotLegends`
Clear[x];
funs = {Exp[x], 2^x, 3^x};
legends = Map[Text@Style[#, "TR", 12] &, funs];
Plot[Evaluate@funs, {x, -5, 2}, AspectRatio -> Automatic, 
 PlotStyle -> {Red, Green, Blue}, PlotLegend -> legends]

在此处输入图像描述

查看帮助以进一步自定义图例。上面使用默认值。

http://reference.wolfram.com/mathematica/PlotLegends/tutorial/PlotLegends。 html

One way is to use PlotLegends

(I do not like it too much, but it is an easy way to do what you want)

<< PlotLegends`
Clear[x];
funs = {Exp[x], 2^x, 3^x};
legends = Map[Text@Style[#, "TR", 12] &, funs];
Plot[Evaluate@funs, {x, -5, 2}, AspectRatio -> Automatic, 
 PlotStyle -> {Red, Green, Blue}, PlotLegend -> legends]

enter image description here

see help to customize the legend more. The above uses defaults.

http://reference.wolfram.com/mathematica/PlotLegends/tutorial/PlotLegends.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文