Wolfram Blog
Andrew Moylan

Mathematica Q&A: Excluding Points from Plots

March 14, 2011 — Andrew Moylan, Technical Communication & Strategy

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

Posted in: Mathematica Q&A
Leave a Comment

13 Comments


Pedro Vagner

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

Posted by Pedro Vagner    March 14, 2011 at 1:00 pm
Mark

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.

Posted by Mark    March 14, 2011 at 1:41 pm
Josef Schmidt

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?

Posted by Josef Schmidt    March 14, 2011 at 2:04 pm
Avitodvd

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

Posted by Avitodvd    March 14, 2011 at 9:21 pm
Mooniac

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

Posted by Mooniac    March 15, 2011 at 9:57 am
Paul McHale

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?

Posted by Paul McHale    March 15, 2011 at 10:21 pm
Isaac Abraham

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.

Posted by Isaac Abraham    March 15, 2011 at 11:06 pm
Isaac Abraham

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

Posted by Isaac Abraham    March 16, 2011 at 12:32 am
Isaac Abraham

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

Posted by Isaac Abraham    March 16, 2011 at 8:27 am
Ted Ersek

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.

Posted by Ted Ersek    March 16, 2011 at 10:07 am
Josef Schmidt

@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. :)

Posted by Josef Schmidt    March 16, 2011 at 5:28 pm
Andrew Moylan

Hi Paul, You can submit questions and ideas for Q&A blog posts using the form at http://blog.wolfram.com/ask-a-question/.

Posted by Andrew Moylan    March 16, 2011 at 8:22 pm
Andrew Moylan

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

Posted by Andrew Moylan    March 16, 2011 at 8:23 pm


Leave a comment

Loading...

Or continue as a guest (your comment will be held for moderation):