在Mathematica中绘制水平和垂直线

kar*_*bad 23 wolfram-mathematica

在Mathematica中,如何在给定数字处绘制水平线?如何在给定数字处绘制垂直线?

Pil*_*lsy 28

如果您实际使用的是Plot(或ListPlot等),最简单的解决方案是使用GridLines选项,该选项允许您指定要绘制线条的xy值.例如:

Plot[Sin[x], {x, 0, 2 \[Pi]},
  GridLines -> {{0, \[Pi]/2, \[Pi], 3 \[Pi]/2, 2 \[Pi]},
                {-1, -Sqrt[3]/2, -1/2, 0, 1/2, Sqrt[3]/2, 1}}]
Run Code Online (Sandbox Code Playgroud)

很多行

编辑添加:

当然,如果您只想在单个给定数字处绘制一条线,此解决方案就可以工作.例如,如果你想从dreeve的答案中重现第二个例子:

Plot[Sin[x], {x, 0, 2 Pi},
 GridLines -> {{4}, {}}]
Run Code Online (Sandbox Code Playgroud)

一条线


dre*_*ves 16

对于水平线的情况,使用Plot最简单的技巧是只包括其他常量函数:

Plot[{Sin[x], .75}, {x, 0, 2Pi}]
Run Code Online (Sandbox Code Playgroud)

对于垂直线,有个Epilog选项PlotListPlot:

Plot[Sin[x], {x, 0, 2Pi}, Epilog->Line[{{4,-100}, {4,100}}]]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但可能最好的是GridLinesPillsy的回答中给出的选项.


Hig*_*ark 10

一种方法是在Line图形中添加图形基元:

p1 = Plot[Sin[x], {x, -2*Pi,2*Pi}];
l1 = Graphics@Line[{{-2Pi,.75},{2Pi,.75}}]; (* horizontal line at y==.75 *)
Show[p1,l1]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

另一种方法是摆弄GridLines.