22
$\begingroup$

Bug introduced in 8 or earlier and persisting through 11.0.1 or later


Consider this example:

dat = Table[Re[Sqrt[1 - x^2 - y^2]], {x, -1.1, 1.1, 0.01}, {y, -1.1, 1.1, 0.01}];

Now pay special attention to the labelling of contours.

Here everything looks good:

ListContourPlot[dat, Contours -> {1., 0.9, 0.8, 0.7, 0.6, 0.5}, ContourLabels -> All]

enter image description here

But look!

ListContourPlot[dat, Contours -> Range[1, 0.5, -0.1], ContourLabels -> All]

enter image description here

The labelling of contours is now wrong: it is inverted. The largest one gets the smallest label.

As far as I can tell, the two ListContourPlot commands are completely equivalent. One has a Range, the second one has what the Range would evaluate to.

Range[1, 0.5, -0.1]
(* {1., 0.9, 0.8, 0.7, 0.6, 0.5} *)

ListContourPlot is not HoldAll.

Attributes[ListContourPlot]
(* {Protected, ReadProtected} *)

I must be missing something obvious, but it's late here and I'm tired and I cannot see what. I'm using M10.4 on OS X.

Update: I solved the mystery: the difference between Range[1, 0.5, -0.1] and {1., 0.9, 0.8, 0.7, 0.6, 0.5} is that the former is a packed array. It seems this is just a plain old bug...

$\endgroup$
  • $\begingroup$ Given your discovery, a workaround -- other than avoiding use of Range -- would be ListContourPlot[dat, Contours -> (# & /@ Range[1, 0.5, -0.1]), ContourLabels -> All] $\endgroup$ – Bob Hanlon Mar 12 '16 at 0:51
  • $\begingroup$ @BobHanlon. Turns out the incredibly silly-looking List@@Range[1, 0.5, -0.1] fixes it too. $\endgroup$ – march Mar 12 '16 at 4:45
  • $\begingroup$ You should add bug Tag to the question. $\endgroup$ – Algohi Mar 12 '16 at 5:16
  • 1
    $\begingroup$ @Algohi I think it's better if you do that yourself. The idea is that it should be someone else than the original poster who adds it. I don't want to show a bad example. $\endgroup$ – Szabolcs Mar 13 '16 at 10:17
14
$\begingroup$

The reason for the difference between the outputs is that Range[1,0.5,-0.1] is a packed array while {1., 0.9, 0.8, 0.7, 0.6, 0.5} is not.

If the Contours specification is a packed array, the output will be incorrect.

Developer`FromPackedArray@Range[...] can be used as a workaround.


When being an option of Interpolation, ListInterpolation, FunctionInterpolation, using packed array as the option value of InterpolationOrder also causes problem:

f = Interpolation[{1, 2, 3, 5, 8, 5}, InterpolationOrder ->{3}]
(* Works as expected *)
f = Interpolation[{1, 2, 3, 5, 8, 5}, InterpolationOrder -> Developer`ToPackedArray@{3}]

enter image description here

$\endgroup$
  • $\begingroup$ that is just bizarre. $\endgroup$ – rcollyer Mar 14 '16 at 14:35
  • $\begingroup$ Also, if the contours are in ascending order, it works fine. $\endgroup$ – rcollyer Mar 14 '16 at 14:45

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.