如何在 Mathematica 中标记不同的曲线?

发布于 2024-12-01 22:34:52 字数 151 浏览 1 评论 0原文

如何分别标记每一行:

Plot[{{5 + 2 x}, {6 + x}}, {x, 0, 10}]

在此处输入图像描述

How can I label each of these lines separately :

Plot[{{5 + 2 x}, {6 + x}}, {x, 0, 10}]

enter image description here

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

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

发布评论

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

评论(3

待"谢繁草 2024-12-08 22:34:52

有一些不错的代码可以让您在答案中动态执行此操作如何注释多个数据集在 ListPlots 中。

技术说明中还定义了一个 LabelPlot 命令 在绘图中标记曲线

当然,如果你没有太多的图像要制作,
那么使用 Epilog< 手动添加标签并不难/a>,例如

fns[x_] := {5 + 2 x, 6 + x}; 
len := Length[fns[x]];

Plot[Evaluate[fns[x]], {x, 0, 10}, 
 Epilog -> Table[Inset[
    Framed[DisplayForm[fns[x][[i]]], RoundingRadius -> 5], 
    {5, fns[5][[i]]}, Background -> White], {i, len}]]

outputa

事实上,您可以使用 Locators 执行类似的操作,从而允许你将标签移动到您想要的任何位置:

DynamicModule[{pos = Table[{1, fns[1][[i]]}, {i, len}]},
 LocatorPane[Dynamic[pos], Plot[Evaluate[fns[x]], {x, 0, 10}],
  Appearance -> Table[Framed[Text@TraditionalForm[fns[x][[i]]], 
                  RoundingRadius -> 5, Background -> White], {i, len}]]]

在上面,我使定位器采用标签的形式,尽管也可以保留像上面那样的 Epilog 并具有控制位置的不可见定位器。
定位器也可以被约束(使用Dynamic的第二个参数)到适当的曲线......但这并不是真正必要的。

作为上述代码的示例,其中带有手动移动标签的函数:

fns[x_] := {Log[x], Exp[x], Sin[x], Cos[x]};

four function

There's some nice code that allows you to do this dynamically in an answer to How to annotate multiple datasets in ListPlots.

There's also a LabelPlot command defined in the Technical Note Labeling Curves in Plots

Of course, if you don't have too many images to make,
then it's not hard to manually add the labels in using Epilog, for example

fns[x_] := {5 + 2 x, 6 + x}; 
len := Length[fns[x]];

Plot[Evaluate[fns[x]], {x, 0, 10}, 
 Epilog -> Table[Inset[
    Framed[DisplayForm[fns[x][[i]]], RoundingRadius -> 5], 
    {5, fns[5][[i]]}, Background -> White], {i, len}]]

outputa

In fact, you can do something similar with Locators that allows you to move the labels wherever you want:

DynamicModule[{pos = Table[{1, fns[1][[i]]}, {i, len}]},
 LocatorPane[Dynamic[pos], Plot[Evaluate[fns[x]], {x, 0, 10}],
  Appearance -> Table[Framed[Text@TraditionalForm[fns[x][[i]]], 
                  RoundingRadius -> 5, Background -> White], {i, len}]]]

In the above I made the locators take the form of the labels, although it is also possible to keep an Epilog like that above and have invisible locators that control the positions.
The locators could also be constrained (using the 2nd argument of Dynamic) to the appropriate curves... but that's not really necessary.

As an example of the above code with the functions with the labels moved by hand:

fns[x_] := {Log[x], Exp[x], Sin[x], Cos[x]};

four functions

烈酒灼喉 2024-12-08 22:34:52

Mathematica 9 现在提供了包含图例的简单方法。

Plot[{{5 + 2 x}, {6 + x}}, {x, 0, 10}, PlotLegends -> "Expressions"]

Mathematica 9 now provides easy ways to include legends.

Plot[{{5 + 2 x}, {6 + x}}, {x, 0, 10}, PlotLegends -> "Expressions"]
平定天下 2024-12-08 22:34:52

您可以通过加载 PlotLegends 包在图中插​​入图例

<<PlotLegends`;
Plot[{5+2 x,6+x},{x,0,10},
    PlotLegend->{"5+2x","6+x"},LegendShadow->None,
    LegendPosition->{0.3,-0.5},LegendSpacing->-0,LegendSize->0.5]

在此处输入图像描述


但是,让我还注意到我不喜欢这个软件包,主要是因为它非常违反直觉,充满了太多选项,并且不像大多数 Mathematica 功能那样提供开箱即用的干净体验。您将需要摆弄一些选项来获得您想要的东西。然而,在您确实需要图例的绘图和图表中,这会很方便。另请参阅此答案这个问题

You can insert legends in your plot by loading the PlotLegends package

<<PlotLegends`;
Plot[{5+2 x,6+x},{x,0,10},
    PlotLegend->{"5+2x","6+x"},LegendShadow->None,
    LegendPosition->{0.3,-0.5},LegendSpacing->-0,LegendSize->0.5]

enter image description here


However, let me also note my dislike of this package, primarily because it's extremely counterintuitive, laden with too many options and does not provide a clean experience right out of the box like most of Mathematica's functions. You will have some fiddling around to do with the options to get what you want. However, in plots and charts where you do want a legend, this can be handy. Also see the comments to this answer and this question.

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