【问题标题】:How to generate function name automatically in mathematica?如何在mathematica中自动生成函数名?
【发布时间】:2012-01-02 02:13:05
【问题描述】:

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

我现在的代码:

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

我的意思是在这种情况下生成 3 个标签来告诉用户它是什么功能。

如:

你如何生成这个?

【问题讨论】:

标签: wolfram-mathematica


【解决方案1】:

也许这样可行:在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}
}

【讨论】:

  • 这是对符号图形对象中替换功能的一种非常漂亮和巧妙的使用。我们通常也以这种方式进行所有后期处理。
【解决方案2】:

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

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

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

这里有几个例子:

你可以随意修改。

【讨论】:

  • 看起来很不错,但对我来说有点太复杂了。有什么简单的方法吗?谢谢~
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    一种方法是使用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

    【讨论】:

      猜你喜欢
      • 2011-09-07
      • 2016-09-07
      • 2018-12-04
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多