Wolfram Computation Meets Knowledge

Mathematica Q&A: Combining and Annotating Plots

Got questions about Mathematica? The Wolfram Blog has answers! We’ll regularly answer selected questions from users around the web. You can submit your question directly to the Q&A Team using this form.

Today’s question is from Herbert, a reader of this blog:

How can I plot a function like sin(x) together with a relation like x = π?

You can plot two or more functions together by giving a list as the first argument of a function like Plot:

Plot[{sin[x], (1/x)}, {x, 0, 10}]
Graph illustrating two functions

But a relation like x = π is not a function, since it doesn’t define a single function value for each x.

There are a few different ways to combine a regular plot of a function with a visualization of a relation.

The particular relation x = π is a single vertical line. Any arrangement of horizontal and vertical lines can be produced using the GridLines option:

Plot[sin[x], {x, 0, 10}, GridLines → {{π}, None}]
Graph illustrating GridLine at π

The setting GridLines → {xlist, ylist} draws grid lines at the specified lists of points on the x– and y-axes. You can also use GridLinesStyle to specify colors and styles. Here’s a version with multiple dashed grid lines:

Plot[sin[x], {x, 0, 10}, GridLines → {{(π/4), (9π/4)}, {-(1/√2), (1/√2)}}, GridLinesStyle → Dashed]
Graph illustrating dashed GridLines

Another way to get the vertical line x = π is to explicitly tell Plot to draw the line after it finishes plotting. You do this with the Epilog option:

Plot[sin[x], {x, 0, 10}, Epilog → Line[{{π, -1}, {π, 1}}]]
Graph showing vertical line drawn by the Epilog option

The Epilog option can include any combination of Mathematica‘s graphics directives, such as colors and dashing:

Plot[sin[x], {x, 0, 10}, Epilog → {Thick, Orange, Dashed, Line [{π, -1}, {π, 1}}]}]
Graph illustrating thick orange dashed line at π

Instead of manually drawing the line representing x = π, you can use ContourPlot to visualize the relation:

contour = ContourPlot[x = π, {x, 0, 10}, {y, -1, 1}, ContourStyle → {Thick, Orange, Dashed}]
Graph illustrating a line representing x = π using ContourPlot

Using the function Show, you can combine the contour plot with a regular plot of sin(x):

Show[Plot[Sin[x], {x, 0, 10}], contour]
Graph combining the contour plot with a regular plot of sin(x)

(Note that Show uses the plot size and axes of its first argument—in this case, the regular plot of sin(x).)

The advantage of ContourPlot is that you can readily visualize more complicated relations than x = π. Here’s the relation sin(x) = sin(y):

Show[Plot[Sin[x], {x, 0, 10}, PlotRange → 2], ContourPlot[Sin[x] = Sin[y], {x, 0, 10}, {y, -2, 2}, ContourStyle → {Purple, Dashed}]]
Graph illustrating the relation sin(x) = sin(y) with ContourPlot

You can use Show to combine the graphics produced by any of Mathematica‘s graphics functions. For example, RegionPlot can be used to visualize regions satisfying inequalities. Here’s a plot highlighting the region satisfying both sin(x) < y and x < π/2:

Show[Plot[Sin[x], {x, 0, 10}], RegionPlot[And[Sin[x] < y, x < (π/2)], {x, 0, 10}, {y, -1, 1}]]
Plot highlighting the region satisfying both sin(x) < y and x < (π/2)

Between Show, options like GridLines and Epilog, and all of Mathematica‘s built-in graphics functions, you have a lot of flexibility when creating custom visualizations.

Click here to download this post as a Computable Document Format (CDF) file.

If you have a question you’d like answered in this blog, you can submit it to the Q&A Team using this form. For daily bite-sized Mathematica tips, follow our @MathematicaTip Twitter feed (or follow using RSS).


Join the discussion

!Please enter your comment (at least 5 characters).

!Please enter your name.

!Please enter a valid email address.


  1. I find it easiest to use parametric plot for vertical lines:
    ParametricPlot[{{x, Sin[x]}, {\[Pi], y}}, {x, 0, 10}, {y, -1, 1}]

  2. Thanks. The use of ContourPlot is great ! I havent known that use of more complicated relation.

  3. Thank you for sharing this post! Been wondering how to do this without having to resort to Show[Plot,Graphics[Line]]!

  4. amazing work. i am impressed

  5. Nice!

    I hope the next edition of Mathematica’s Help system will include (links to?) such examples as on this blog and elsewhere – a real harnessing of the Mathematica developer/user ecosystem.

  6. Very well set up. Even advanced mathematicians can bebefit from the lectures.

  7. I see the simplest ways to go to the better understanding of mix elements in mathematica, as gridlines, colours, graphics, areas, dashed lines, the meaning of PI, intervals, styles, regions, and plot of several functions at the same time, etc. Thank You.

  8. y=(1.1)^x+sinx
    -10<=x<=20,-10<=x<=30,-10<=x<=40 ,-10<=x<=50

  9. draw a combining graph

    -10<=x<=20,-10<=x<=30,-10<=x<=40 ,-10<=x<=50