WOLFRAM

Mathematica Q&A: Excluding Points from Plots

Got questions about Mathematica? The Wolfram Blog has answers! Each week, we’ll answer a selected question from users around the web. You can submit your question directly to the Q&A Team.

For our first post in this new series of Mathematica Q&A articles, we’re going to address a very frequently asked question about plotting in Mathematica.

How can I control the appearance of discontinuities in a plot?

The short answer is, use the options Exclusions and ExclusionsStyle! Let’s see how they work.

By default, Plot shows the function 1 ⁄ sin(x) with lines joining its discontinuities:

1 / sin(x)

1 / sin(x) plot

You can use the Exclusions option to exclude points from the plot:

Exclusions

Exclude points from plot

Instead of excluding the points, you can specify a style to apply to them:

Exclusion style

Exclusion style plot

For explicitly piecewise functions like Floor, points are excluded automatically:

Floor function

Floor function plot

(You can use ExclusionsNone to disable this.)

The exclusions options work with many other Mathematica functions, such as Plot3D:

Plot 3D function

Plot 3D

We specified two styles using ExclusionsStyle: the first style is applied to the excluded region, while the second style is applied to its boundary.

You can create some spectacular visualizations with these options. Download the Computable Document Format (CDF) file for this post to see how to generate this one:

Visualizations

And don’t forget you can submit your own questions to the Q&A Team anytime.

Download the CDF file

Comments

Join the discussion

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

!Please enter your name.

!Please enter a valid email address.

12 comments

  1. Mathematica Q&A is a great idea! In this first posting was raised an excellent question, thanks!

    Reply
  2. I second the comment that Q&A is a great idea. I have a question that I hope isn’t too trivial. What would you recommend as the most reasonable Mathematica way to simulate simple BASIC-like plotting of arbitrary points, where statements like Plot x,y “turn on” a particular x,y points in a bitmap or grid? Mathematica makes the complicated stuff so easy that I sometimes feel unsure where to begin with the very simple stuff.

    Reply
  3. If I have numerical solution as interpolation function from NDSolve:
    sol = NDSolve[{x'[t]==5,x[0]==0}, x, {t, 0, 100}]
    And I want to plot the solution in this way:
    Plot[(x[t] – Floor[x[t], 100]) /. sol[[1]], {t, 0, 100}]
    Then Mathematica doesn’t exclude discontinuities automatically. How do I get rid of these joining lines?

    Reply
  4. Wow, I am a idiot of math, and you guys so smart…and I even can’t understand what’s the stand for?

    Reply
  5. Good idea. But should this really be in the blog? Shouldn’t this be in a separate section?

    Reply
  6. I like this a lot. It will also increase the frequency of update. How does one submit proposals for content? Is this a good place to ask questions or do you have a user forum?

    Reply
  7. Good and thank you for posting. I’m also in the camp that’s thinking that a separate section for this type of posting will be in keeping with the organized structure on your website.

    Reply
  8. Re: NDSolve by Josef
    After observing that the discontinuities happen at multiples of 0.2, I chose to do the following exclusions statement, successfully eliminating the discontinuities.
    Exclusions -> Table[0.2 n, {n, 1, 5}]

    Reply
  9. Ref: NDSolve by Josef
    Submitting entire code, from M7 Student.

    sol = NDSolve[{x'[t] == 5, x[0] == 0}, x, {t, 0, 100}];
    y = Evaluate[(x[t] – Floor[ x[t] ]) /. sol ] ;
    Plot[y, {t, 0, 1},
    Exclusions -> Table[0.2 n, {n, 1, 5}]]

    Reply
  10. Nice tutorial. The Mathematica documentation is first class as of version 6, but there is still room for improvement. The examples in this Blog should replace the examples in documentation for the Exclusions option. I hope Wolfram Research will strive to make it easy to find the answer to every FAQ they know of, and put them all in the documentation. The key is to include good examples, and provide links connecting related topics.

    Reply
  11. @Isaac: Well, it was just a trivial example. In fact I have differential equation where I cannot predict discontinuities caused by Floor function. I mean, how to suppress this InterpolationFunction-behaviour in general. Anyway, thank you for your time and answer. :)

    Reply
  12. Hi Mark, I think ListPlot may be the function you are looking for.

    Reply