【问题标题】:labeling different plots in the same graph in Mathematica 8在 Mathematica 8 的同一张图中标记不同的图
【发布时间】:2012-09-14 14:26:16
【问题描述】:

我有一个关于在 Mathematica 中标记绘图的问题。我将描述我的问题。

我有这样的功能。

y = 4 x / L + 2

我想绘制 y 与 x 的关系图。还有,我有

L={10,20,30,40}

当我编写如下代码时,

Plot[y, {x, 0, 100}, 
    ImageSize -> Scaled[1.0], PlotLabel ->  Style["y vs X ", FontSize -> 18]]

我在同一张图中有四个不同的图。我想知道如何用相关的 L 值标记每个图。

【问题讨论】:

  • 请阅读手册以掌握语法的基本知识

标签: wolfram-mathematica mathematica-8


【解决方案1】:

根据我之前的帖子here,您可以使用此方法随意标记行。标注后可以发现没有动态内容的情节设置为plainplot

它通过将每一行变成一个自标记按钮来工作。您可以针对不同的标签修改labels

l = {10, 20, 30, 40};
y[x_, s_] := 4 x/s + 2

plot = Plot[Evaluate@Table[y[x, u], {u, l}], {x, 0, 100},
   PlotLabel -> Style["y vs X ", FontSize -> 18]];

pos = Position[plot, _Line];
Array[(line[#] = plot[[Sequence @@ pos[[#]]]]) &, Length@l];
AddLabel[label_] := Module[{},
  AppendTo[plot[[1]], Inset[Framed[label, Background -> White], pt]];
  (* Removing buttons for final plot *)
  plainplot = plot;
  Array[
   (plainplot[[Sequence @@ pos[[#]]]] =
      plainplot[[Sequence @@ Append[pos[[#]], 1]]]) &, Length@l]]
labels = ToString /@ l;
Array[
  (plot[[Sequence @@ pos[[#]]]] =
     Button[line[#], AddLabel[labels[[#]]]]) &, Length@l];
Dynamic[EventHandler[plot,
  "MouseDown" :> (pt = MousePosition["Graphics"])]]

【讨论】:

  • 非常感谢巫师先生。你能告诉我如何将它们标记为“l = 10”,....我想删除这些小盒子。我是 Mathematica 的初学者。非常感谢您在这方面的帮助。
  • @Thakshila - 您可以将 Inset 更改为 Inset[Framed["l = " <> ToString[label], Background -> White, FrameStyle -> None], pt] 或(通过均匀位移移动标签)Inset["l = " <> ToString[label] <> " \n", pt]
  • @ChrisDegnen 当我运行这段代码时,它没有任何标签。你能解释一下原因吗?
【解决方案2】:
l = {10, 20, 30, 40}
y[x_, s_] := 4 x/s + 2
<< PlotLegends`

Plot[Evaluate@Table[y[x, u], {u, l}], {x, 0, 100}, 
 ImageSize -> Scaled[1.0], 
 PlotLabel -> Style["y vs X ", FontSize -> 18], 
 PlotLegend -> ("L = " <> ToString@# & /@ l)]

【讨论】:

  • 谢谢贝利撒留。当我打印这个图时,其他人看不到颜色。这就是问题。
猜你喜欢
  • 2011-11-05
  • 2021-03-29
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 2013-05-23
  • 2019-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多