【问题标题】:Combining Plots in Mathematica is not giving the expected result在 Mathematica 中组合图没有给出预期的结果
【发布时间】:2011-11-22 00:26:48
【问题描述】:

我正在尝试将Plot[] 上绘制的 3 个函数和ParametricPlot[] 上绘制的 1 个函数结合起来。我的方程式如下:

plota = Plot[{-2 x, -2 Sqrt[x], -2 x^(3/5)}, {x, 0, 1}, PlotLegend -> {"-2 x", "-2 \!\(\*SqrtBox[\(x\)]\)", "-2 \!\(\*SuperscriptBox[\(x\), \(3/5\)]\)"}]
plotb = ParametricPlot[{2.4056 (u - Sin[u]), 2.4056 (Cos[u] - 1)}, {u,0, 1.40138}, PlotLegend -> {"Problem 3"}]
Show[plota, plotb]

这是它给出的图像:

【问题讨论】:

  • 这是正确的地方。您的 math.se 问题在错误的位置。请删除那个。你会在这里得到答案。
  • @yoda:感谢您积极回答我的一个问题。 :)

标签: wolfram-mathematica plot


【解决方案1】:

正如 yoda 所说,PlotLegends 很糟糕。但是,如果您不介意手动设置绘图样式并稍后重复它们,ShowLegend 可以提供帮助。

 plota = Plot[{-2 x, -2 Sqrt[x], -2 x^(3/5)}, {x, 0, 1}, 
              PlotStyle -> {{Red}, {Blue}, {Orange}}];
 plotb = ParametricPlot[{2.4056 (u - Sin[u]), 2.4056 (Cos[u] - 1)}, {u, 0, 1.40138}, 
                        PlotStyle -> {{Black}}];

现在

ShowLegend[Show[plota, plotb], 
          {{{Graphics[{Red, Line[{{0, 0}, {1, 0}}]}], Label1},          
            {Graphics[{Blue, Line[{{0, 0}, {1, 0}}]}], Label2},
            {Graphics[{Orange, Line[{{0, 0}, {1, 0}}]}], Label3}, 
            {Graphics[{Black, Line[{{0, 0}, {1, 0}}]}], Label4}},
           LegendSize -> {0.5, 0.5}, LegendPosition -> {0.5, -0.2}}]

这会给你这个:

如果你经常处理这个问题,你也可以编写一些简单的函数来减少麻烦。

【讨论】:

  • 哦,哇。我刚刚编辑了我的答案以添加完全相同的内容,我看到您在我编辑前整整 10 分钟发布了。我在编辑器里乱搞的时候没有收到通知,因为我已经回复了帖子。有一个衷心的支持! :)
【解决方案2】:

嗯,错误的根本原因是PlotLegends 包,这是一个可怕的、有缺陷的包。删除它,Show 正确组合它们:

plota = Plot[{-2 x, -2 Sqrt[x], -2 x^(3/5)}, {x, 0, 1}]
plotb = ParametricPlot[{2.4056 (u - Sin[u]), 2.4056 (Cos[u] - 1)}, {u,
    0, 1.40138}]
Show[plota, plotb]

您可以查看 Simon 的解决方案 here,了解在不使用 PlotLegends 的情况下标记不同曲线的想法。 James 的This answer 还展示了为什么PlotLegends 享有盛誉...


您仍然可以使用PlotLegends 包来挽救一些东西。这是一个使用 ShowLegends 的示例,您可以根据自己的喜好进行修改

colors = {Red, Green, Blue, Pink};
legends = {-2 x, -2 Sqrt[x], -2 x^(3/5), "Problem 3"};

plota = Plot[{-2 x, -2 Sqrt[x], -2 x^(3/5)}, {x, 0, 1}, 
   PlotStyle -> colors[[1 ;; 3]]];
plotb = ParametricPlot[{2.4056 (u - Sin[u]), 2.4056 (Cos[u] - 1)}, {u,
     0, 1.40138}, PlotStyle -> colors[[4]]];
ShowLegend[
 Show[plota, 
  plotb], {Table[{Graphics[{colors[[i]], Thick, 
      Line[{{0, 0}, {1, 0}}]}], legends[[i]]}, {i, 4}], 
  LegendPosition -> {0.4, -0.15}, LegendSpacing -> 0, 
  LegendShadow -> None, LegendSize -> 0.6}]

【讨论】:

  • 我不能使用他正在使用的fns[x_] := 符号,因为我有一个参数方程。你能告诉我如何添加标签吗?另外,我在我的 LaTeX 文档中导入了一个图像,但它太大了,所以我将它缩放了 0.5,这让它看起来很糟糕。我怎样才能让它更小而且看起来还不错?
  • 你知道传奇的任何好的替代包吗? (不像 LevelScheme 那样完全重新设计图形系统。只是简单的传说。)
  • @Szabolcs 遗憾的是,我不...因此,我倾向于避免标记我的曲线(最多 3 个/图),而是在图形标题文本中标记它们。
【解决方案3】:

正如其他答案指出的那样,罪魁祸首是PlotLegend。因此,有时能够推出自己的情节图例很有用:

plotStyle = {Red, Green, Blue};
labls = {"a", "b", "Let's go"};
f[i_, s_] := {Graphics[{plotStyle[[i]], Line[{{0, 0}, {1, 0}}]}, 
    ImageSize -> {15, 10}], Style[labls[[i]], s]};

Plot[{Sin[x], Sin[2 x], Sin[3 x]}, {x, 0, 2 Pi}, 
 PlotStyle -> plotStyle,
 Epilog ->
  Inset[Framed[Style@Column[{Grid[Table[f[i, 15], {i, 1, 3}]]}]],
   Offset[{-2, -2}, Scaled[{1, 1}]], {Right, Top}],
 PlotRangePadding -> 1
 ]

【讨论】:

  • 嘿,belisarius,我刚刚意识到,您投票最多的答案比前 12 名历史 SO 用户中任何一个投票最多的答案都多。 o_o
  • @Mr.是的。我因贿赂用户而失去了财富:D。这也是投票最多的非 CW 答案。
  • 那个答案仍然是 reddit 材料。所有贝利撒留需要做的(或者也许他已经这样做了)就是定期发布一个指向 reddit 的链接并将其命名为 “rand() 是否比 rand()*rand() 更随机?天啊!!MIND = BLOWN!! !” 并在接下来的 5 天里观看投票:P
  • @yoda 我只访问过一次 reddit,我不确定该网站的机制。你能为我做那件事吗? :D
猜你喜欢
  • 1970-01-01
  • 2015-01-12
  • 2020-05-29
  • 1970-01-01
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-09
相关资源
最近更新 更多