在 Mathematica 中组合图未给出预期结果

发布于 2024-12-17 16:57:48 字数 506 浏览 0 评论 0原文

我正在尝试组合在 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]

这是它给出的图像:

Combined Graph

I'm trying to combine 3 functions graphed on a Plot[] and 1 function graphed on a ParametricPlot[]. My equations are as follows:

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]

This is the image it gives:

Combined Graph

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

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

发布评论

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

评论(3

暮光沉寂 2024-12-24 16:57:48

正如 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}}]

将为您提供以下内容:

在此处输入图像描述

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

As yoda said, PlotLegends is terrible. However, if you don't mind setting the plot styles manually and repeating them lateron, ShowLegend can help.

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

And now

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

which will give you this:

enter image description here

You can also write some simple functions to make this a little less cumbersome, if you deal with this problem often.

灯角 2024-12-24 16:57:48

好吧,错误的根本原因是 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 的解决方案 此处获取在不使用标签的情况下标记不同曲线的想法PlotLegends。 James 的这个答案也说明了为什么 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}]

在此处输入图像描述

Well, the root cause of the error is the PlotLegends package, which is a terrible, buggy package. Removing that, Show combines them correctly:

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]

enter image description here

You can see Simon's solution here for ideas to label your different curves without using PlotLegends. This answer by James also demonstrates why PlotLegends has the reputation it has...


You can still salvage something with the PlotLegends package. Here's an example using ShowLegends that you can modify to your tastes

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

enter image description here

云柯 2024-12-24 16:57:48

正如其他答案所指出的,罪魁祸首是 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
 ]

在此处输入图像描述

As the other answers pointed out, the culprit is PlotLegend. So, sometimes is useful to be able to roll your own plot legends:

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
 ]

enter image description here

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