1
$\begingroup$

I am using a ContourPlot command to plot a function and unless I explicitly define the ContourShading I get the same colour used for most intervals. Is there any way to fix this?

Here's the function that's not doing what I want

ContourPlot[1/x 1.303 E^(1/2 (-0.48 (-5.3+x)^2-(137 y^2)/x^2)),
{x,0,15},{y,-2,2},PlotRange->Full,PlotPoints->10,
AspectRatio->1/3,PlotLegends->Automatic]

which gives

ContourPlot with same colours

whereas if I explicitly state the colour shading I get what I expected

ContourPlot[1/x 1.303 E^(1/2 (-0.48 (-5.3 + x)^2 - (137 y^2)/x^2)),
 {x, 0, 15}, {y, -2, 2}, 
 PlotRange -> Full, PlotPoints -> 10, 
 AspectRatio -> 1/3, PlotLegends -> Automatic, 
 ContourShading -> {White, Red, Green, Blue, Yellow, Purple}]

ContourPlot with each contour a different colour

If I play around with PlotRange (All, Full, Automatic) then it changes whether I get the contours different colours, but the ranges also change. Playing around with the number of PlotPoints also changes whether I get contours as expected.

Is this a glitch or expected behaviour? Is there a way to explicitly tell it to use the default theme? This is what I was expecting (and have for different parameter values), so I'd like everything to match the same theme.

What I want the figures to look like

$\endgroup$
3
  • 6
    $\begingroup$ You have a singularity at {0,0} which is effecting the range used by the color function. If you change the x-range to {x, 0.1, 15} you get something that looks reasonable. $\endgroup$
    – rcollyer
    Jan 12, 2016 at 3:37
  • $\begingroup$ It turns out that all of the colors are actually slightly different, as can be seen if you do Cases[Normal@plot, RGBColor[a__], Infinity]. $\endgroup$
    – march
    Jan 12, 2016 at 3:43
  • $\begingroup$ Thanks - that has fixed things. Still seems strange thought that it's not using the same variety in colours that it is later - the largest interval is still kicking in quite early. $\endgroup$
    – Esme_
    Jan 12, 2016 at 5:35

3 Answers 3

3
$\begingroup$

I think the problem is in the option PlotRange->Full. By removing this option

    ContourPlot[
 1/x*1.303 E^(1/2 (-0.48 (-5.3 + x)^2 - (137 y^2)/x^2)), {x, 0, 
  15}, {y, -2, 2}, PlotPoints -> 10, AspectRatio -> 1/3, 
 PlotLegends -> Automatic]

one immediately gets the different shading:

enter image description here

Alternatively one may specify the color function:

    ContourPlot[
 1/x*1.303 E^(1/2 (-0.48 (-5.3 + x)^2 - (137 y^2)/x^2)), {x, 0, 
  15}, {y, -2, 2}, PlotPoints -> 10, AspectRatio -> 1/3, 
 ColorFunction -> "Rainbow", PlotLegends -> Automatic]

giving the following:

enter image description here

The white area in the center is related to the default PlotRangevalue, by its fixing we get rid of it:

    ContourPlot[
 1/x*1.303 E^(1/2 (-0.48 (-5.3 + x)^2 - (137 y^2)/x^2)), {x, 0, 
  15}, {y, -2, 2}, PlotPoints -> 10, AspectRatio -> 1/3, 
 PlotRange -> {0, 0.3}, PlotLegends -> Automatic, 
 ColorFunction -> "Rainbow"]

enter image description here

but here the heights are resolved in a more rough way.

Hope it helps. Have fun!

$\endgroup$
3
$\begingroup$

As rcollyer states, you can get the plot you need by leaving the origin out of the plotted range.

ContourPlot[
 1/x 1.303 E^(1/2 (-0.48 (-5.3 + x)^2 - (137 y^2)/x^2)), {x, 0.001, 
  15}, {y, -2, 2}, PlotRange -> Full, PlotPoints -> 10, 
 AspectRatio -> 1/3, PlotLegends -> Automatic]

enter image description here

I feel like you should be able to do it using Exclusions but I can't quite figure it out.

$\endgroup$
0
$\begingroup$
ContourPlot[
 1/x 1.303 E^(1/2 (-0.48 (-5.3 + x)^2 - (137 y^2)/x^2)), {x, 0.01, 
  15}, {y, -2, 2}, PlotPoints -> 10, AspectRatio -> 1/3, 
 ColorFunction -> (Blend[{Blue, Cyan, Orange, Green, Yellow}, #1] &), 
 PlotLegends -> Automatic]
$\endgroup$
1
  • $\begingroup$ Can you explain what is different in your approach compared to the previous answers? $\endgroup$
    – MarcoB
    Nov 22, 2023 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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