【发布时间】:2011-11-05 11:38:34
【问题描述】:
如何分别标记这些行:
Plot[{{5 + 2 x}, {6 + x}}, {x, 0, 10}]
【问题讨论】:
标签: wolfram-mathematica plot label
如何分别标记这些行:
Plot[{{5 + 2 x}, {6 + x}}, {x, 0, 10}]
【问题讨论】:
标签: wolfram-mathematica plot label
有一些不错的代码可以让您在an answer 到How to annotate multiple datasets in ListPlots 中动态地执行此操作。
技术说明Labeling Curves in Plots 中还定义了一个LabelPlot 命令
当然,如果你没有太多的图片要制作,
那么使用Epilog手动添加标签就不难了,例如
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}]]
事实上,您可以使用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]};
【讨论】:
Dynamic 表达式更改为Dynamic[pos,(pos=MapIndexed[{##}/.{{x_, y_}, {i_}}:>{x,fns[x][[i]]}&,#])&]。
您可以通过加载 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 的大多数函数那样提供开箱即用的干净体验。您将需要摆弄一些选项来获得您想要的东西。但是,在您确实需要图例的绘图和图表中,这可能会很方便。另请参阅 this answer 和 this question 的 cmets。
【讨论】:
PlotLegends 低于标准,我之前使用过LevelScheme 的能级图部分来创建图例。它不是自动化的,也不是简单的,但它运作良好。
Mathematica 9 现在提供了包含图例的简单方法。
Plot[{{5 + 2 x}, {6 + x}}, {x, 0, 10}, PlotLegends -> "Expressions"]
【讨论】: