Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How create vertical line using Plot?

3,774 views
Skip to first unread message

davef

unread,
Oct 24, 2009, 2:46:31 AM10/24/09
to
How can I create a vertical line using the Plot command?

I can hack it using "y" = very big number times "x" but is there a
more elegant way to do it?

Max

unread,
Oct 25, 2009, 1:09:20 AM10/25/09
to

The best ways to plot vertical lines on your plots don't involve
functions of "x", or the Plot command; for example, we can use the
Epilog command on Plot to give us a vertical line drawn between some
pair of points:
Plot[f[x],{x,xmin,xmax}, Epilog->Line[{xval,ymin},{xval,ymax}]]
will plot f from xmin to xmax, and have a vertical line at xval (so
long as xval is in (xmin,xmax)) and you should also take care to see
that ymin and ymax fall within your plot area.

We could also use ParametricPlot:
ParametricPlot[{{xval,u},{u,f[u]}}, {u,umin,umax}]
should give a vertical line at some position "xval," and plot "f"
along the x axis. The documentation center is your friend here; I may
have written these down slightly incorrectly, but experimentation and
documentation should clear that up for you.

-Max

Helen Read

unread,
Oct 25, 2009, 1:09:41 AM10/25/09
to

A vertical line is not a function, so Plot won't plot it. You can use
ContourPlot or ParametricPlot to plot a vertical line, or use the
Graphics primitive Line. For example, each of the following will plot
the line x=3.

ContourPlot[x == 3, {x, -5, 5}, {y, -5, 5}]

ParametricPlot[{3, t}, {t, -5, 5}, PlotRange -> 5]

Graphics[Line[{{3, -5}, {3, 5}}], PlotRange -> 5, Axes -> True]


Use Show to combine any of the above with other plots. For instance:

For instance:

plot1 = Plot[x^2, {x, -5, 5}];
plot2 = ParametricPlot[{3, t}, {t, -5, 16}, PlotStyle -> Red];
Show[{plot1, plot2}, PlotRange -> Automatic]

Or use Epilog to add Graphics primitives to a plot. For instance:

Plot[x^2, {x, -5, 5}, Epilog -> {Red, Line[{{3, -5}, {3, 16}}]},
PlotRange -> {-5, 25}]


--
Helen Read
University of Vermont

ADL

unread,
Oct 25, 2009, 1:10:18 AM10/25/09
to
You can use Epilog:

Plot[Sin[x], {x, 0, 1}, Epilog -> Line[{{.5, 0}, {.5, 1}}]]

creates a vertical line.

ADL

Hannes Kessler

unread,
Oct 25, 2009, 1:11:12 AM10/25/09
to
Hello Dave,

here is an example for a plot with red, dashed, vertical lines:

Plot[x^2, {x, -2, 2}, Epilog -> {Red, Dashed, Line[{{-1, -10}, {-1,
10}}], Line[{{1, -10}, {1, 10}}]}]

Best regards,
Hannes Kessler

Nasser M. Abbasi

unread,
Oct 25, 2009, 1:11:55 AM10/25/09
to

"davef" <davidfr...@yahoo.com> wrote in message
news:hbu7s7$7ic$1...@smc.vnet.net...

> How can I create a vertical line using the Plot command?
>
> I can hack it using "y" = very big number times "x" but is there a
> more elegant way to do it?
>

I think more specification would help.

But to create a line in Mathematica do help on the Line[] function.

--Nasser


dr DanW

unread,
Oct 25, 2009, 1:12:17 AM10/25/09
to
Use either the Epilog or Prolog option to Plot. I could explain here,
but it is probably easier for you to look it up in Doc Center.

Daniel

Szabolcs Horvát

unread,
Oct 25, 2009, 1:13:11 AM10/25/09
to

Why don't you use Graphics directly?

Graphics[Line[{{1, 0}, {1, 1}}], Axes -> True]

You didn't say what you need that vertical line for. Perhaps
Mathematica already has built-in functionality for what you want. Look
up Exclusions and GridLines.

Bob Hanlon

unread,
Oct 25, 2009, 1:14:03 AM10/25/09
to

Use Epilog and Line

f[x_] := (x - 1) (x - 2) (x - 3)

mm = x /. Solve[f'[x] == 0, x];

Plot[f[x], {x, 3/4, 13/4},
Epilog -> {Red,
AbsoluteDashing[{5, 5}],
Line[{{#, 0}, {#, f[#]}}] & /@ mm}]


Bob Hanlon

---- davef <davidfr...@yahoo.com> wrote:

=============


How can I create a vertical line using the Plot command?

I can hack it using "y" = very big number times "x" but is there a
more elegant way to do it?


--

Bob Hanlon


Bill Rowe

unread,
Oct 25, 2009, 1:14:38 AM10/25/09
to
On 10/24/09 at 2:41 AM, davidfr...@yahoo.com (davef) wrote:

>How can I create a vertical line using the Plot command?

>I can hack it using "y" = very big number times "x" but is there a
>more elegant way to do it?

Here are two ways:

Plot[x, {x, 0, 1}, GridLines -> {{.5}, None}]

Plot[x, {x, 0, 1}, Epilog -> {Line@{{.5, -.1}, {.5, 1.1}}}]


David Annetts

unread,
Oct 25, 2009, 1:15:33 AM10/25/09
to
Hi Dave,

>
> How can I create a vertical line using the Plot command?
>
> I can hack it using "y" = very big number times "x" but is
> there a more elegant way to do it?

Don't know about more elegant, but I typically do Line[{{xcoord, smallnum},
{xcoord, bignum}}] and use it in Epilog.

D.


David Park

unread,
Oct 25, 2009, 1:15:57 AM10/25/09
to
Use ParametricPlot instead.

ParametricPlot[{1, y}, {y, 0, 1}]

Or use the Line graphics primitive.


David Park
djm...@comcast.net
http://home.comcast.net/~djmpark/

Tomas Garza

unread,
Oct 25, 2009, 1:26:27 AM10/25/09
to
One possibility is using Epilog:

Plot[Sin[x],{x,0,2}, Epilog->Line[{{1,0},{1,10}}]]

- Tomas

> Date: Sat, 24 Oct 2009 02:41:18 -0400
> From: davidfr...@yahoo.com
> Subject: How create vertical line using Plot?
> To: math...@smc.vnet.net


>
> How can I create a vertical line using the Plot command?
>
> I can hack it using "y" = very big number times "x" but is there a
> more elegant way to do it?
>

=


0 new messages