9
$\begingroup$

I was working with mathematica and I need to use "Filling", however I notice that it doesn't work when you have "small" numbers, for example if I make the following:

Plot[Evaluate[{x + 1, x + 2}], {x, -1*10^-15,1*10^-15},PlotRange -> All, Filling -> {1 -> {2}}]

The "Filling" doesn't appears.

Question

There is some way to make that the "Filling" appear?

Extra

Since this question appear in a physical context, I know that I can change units in order to make "x" a larger number.(Actually I do this.)

$\endgroup$
  • 2
    $\begingroup$ Try Filling -> {1 -> 2}. $\endgroup$ – Rohit Namjoshi Jun 29 at 0:13
  • $\begingroup$ I tried it, however it "seems" that only work in some cases. Try Plot[Evaluate[{1*10^15 (x) - 1, (1*10^15) x + 1}], {x, -1*10^-15, 1*10^-15}, PlotRange -> All, Filling -> {1 -> 2}] It seems that doesn't work $\endgroup$ – Cruz Jun 29 at 0:34
  • 3
    $\begingroup$ @RohitNamjoshi I'd say that Filling -> {1 -> {2}} would be perfectly correct to fill between the first and second plot rather thatn to the value $2$. It should work. Indeed, if you plot over the {x, -1*^-14, 1*^-14} range, you will see the filling just fine. I think OP is right that this is weird behavior... $\endgroup$ – MarcoB Jun 29 at 1:31
  • 2
    $\begingroup$ It's a numerical issue. Change 10^-15 to 10^-5 and it works as you would expect. Presumably the "filling" part of the plot command is using floating point numbers. $\endgroup$ – bill s Jun 29 at 16:31
  • 1
    $\begingroup$ I was unable to find a way to peer into the internals to look for the problem or a way to patch it. Are you interested in functional work-arounds, like rescaling your plot function and changing the axis labels automatically? $\endgroup$ – Mr.Wizard Jul 2 at 1:14
1
$\begingroup$

There are two perspectives in the question

a) PlotRange -> All b) Filling -> {1 -> {2}}

to a)

It is necessary to put in both intervals explicitly into the options of PlotRange:

PlotRange -> {{-1.*10^-15, 1.*10^-15}, {-1.0, 2.1}}

will present the desired result.

to b)

This appears to me as taken from the example for Filling Fill between curves 1 and 2.

I agree this example works for me too. I am using 12.0.

And I get

Plot[{x + 1, x + 2}, {x, 0, 2 Pi}, Filling -> {1 -> {2}}]

direct transfer into the example input of the given functions

This even can not be narrowed in the interval!!! And the built-in Evaluation does not change anything.

The cause can be in the invalid options since with Filling -> {1 -> 2} the plot became as expected:

Plot[Evaluate[{x + 1, x + 2}], {x, -1.*10^-15, 1.*10^-15}, 
 Filling -> {1 -> 2}, 
 PlotRange -> {{-1.*10^-15, 1.*10^-15}, {-1.0, 2.1}}]

everything as expected on the small interval

Mind the example is listed under the section Filling Limits in the Mathematica documentation for Filling.

The curly brackets are suggested for the use with style options or if there are more curves or sections of the interval with separate fillings in the section Details.

Most probable the reason stems from the granularity of the option in the example. Plot in Mathematica uses an interval division algorithm to represent curve smoothly. This algorithm works brilliantly for example trigonometric functions but fails if the given interval is to narrow and the first derivative of the function to be displayed is to small.

In the given case both are about the same interval length and magnitude of the first derivative of the linear function over the interval for interpolation. Since the interpolation is already exact Mathematica does not further refine the representation and the filling algorithm fails. This process is shown in the documentation for Plot in the section Details and Options.

Since the example is given without commending this is transferred into the knowledge of the Mathematica user. So the question is to be dealt with respect, but very basic knowledge.

This question make $MachinePrecision as the interval length and change rate of the function.

$MachinePrecision

15.9546

The limit is different on each machine:

$MinMachineNumber

2.22507*10^-308

for example on mine.

So setting MaxRecursion bigger than 15 fails in this case. This option limits the efforts of refine the curvature to appear smooth to the human eye.

The option Mesh show the efforts already in the seemingly failed example:

Plot[{SetPrecision[x + 1., MachinePrecision], 
  SetPrecision[x + 2., MachinePrecision]}, {x, 
  SetPrecision[-1.000*10^-15, MachinePrecision], 
  SetPrecision[1.000*10^-15, MachinePrecision]}, 
 Filling -> {1 -> {2}}, Mesh -> All]

failed example with highest Precision

Some variation that works is

ListPlot[{Table[x + 1, {x, -1*10^-15, 1*10^-15, 2*10^-16}], 
  Table[x + 2, {x, -1*10^-15, 1*10^-15, 2*10^-16}]}, Joined -> True, 
 Filling -> {1 -> {2}}, Axes -> True, 
 DataRange -> {-1*10^-15, 1*10^-15}]

not working solution for the plotting tasks type

ListPlot[{Table[x + 1, {x, -1*10^-15, 1*10^-15, 2*10^-16}], 
  Table[x + 2, {x, -1*10^-15, 1*10^-15, 2*10^-16}]}, Joined -> True, 
 Filling -> {1 -> {2}}, Axes -> True]

not working solution for the plotting tasks type but wrong axes ticks

ListPlot[{Table[{x, x + 1}, {x, -1*10^-15, 1*10^-15, 2*10^-16}], 
  Table[{x, x + 2}, {x, -1*10^-15, 1*10^-15, 2*10^-16}]}, 
 Filling -> {1 -> {2}}]

working options

ListPlot[{Table[{x, x + 1}, {x, -1*10^-15, 1*10^-15, 2*10^-16}], 
  Table[{x, x + 2}, {x, -1*10^-15, 1*10^-15, 2*10^-16}]}, 
 Joined -> True, Filling -> {1 -> {2}}]

not working options in this case

Design own plot primitives to workaround or accept what is possible.

gg = Graphics[{Opacity[0.3], LightGray, 
   Rectangle[{-10^-15, 1}, {10^-15, 2}]}, Axes -> True, 
  AspectRatio -> 1/3, 
  PlotRange -> {{-1.*10^-15, 1.*10^-15}, {.0, 2.1}}]

works

Show[Plot[Evaluate[{x + 1, x + 2}], {x, -1*10^-15, 1*10^-15}, 
 PlotRange -> {{-1.*10^-15, 1.*10^-15}, {-1.0, 2.1}}], gg]

workaround for very small intervals and linear functions

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.