Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

How do I plot only one component in ContourPlot when specifying a height.

For example,

ContourPlot[
  Sin[Sqrt[x^2 + y^2]] Exp[-x^2 - y^2] == 0,
  {x, -10, 10}, {y, -10, 10}
]

produces concentric circles. How do I plot only the innermost one? That is the component going through (Pi,0).

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

Contour closest to point:

x0={Pi,0}

Normal@ContourPlot[Sin[Sqrt[x^2 + y^2]] Exp[-x^2 - y^2] == 0, {x,-10,10}, {y,-10, 10},
                   PlotPoints -> 100
                  ] /. {x_, y__Line} :> {x, 
                       Line@SortBy[{y}[[;; , 1]], Min[Norm[# - x0] & /@ #] &][[1]]}

enter image description here

If you are aware of ther structure of graphics you can deal with such problems ad hoc:

ContourPlot[Sin[Sqrt[x^2 + y^2]] Exp[-x^2 - y^2] == 0, {x, -10, 10}, {y, -10, 10}, 
            PlotPoints -> 100
           ] /. {col_, y__Line} :> {col, {y}[[-1]]}

That's not necessarily best way but the better network of information on SE the better! :)

So about the structure, you may want to take a look at:

Examine structure of graphics

share|improve this answer
    
How do you know which contour line this will be, other than testing? Is is always the innermost for example? –  Mr.Wizard Jan 31 at 7:10
    
@Mr.Wizard ofc not, best example is Sin[x]==0 where it's hard to tell what is innermost. I suspected it should be first or last one. That's why I called it ad hoc. :) I've provided an explanation why this was posted, if it works, why not ;) maybe someone will learn something. –  Kuba Jan 31 at 7:12
1  
@Mr.Wizard ok, more general solution added. more convenient for not easy to set region functions. –  Kuba Jan 31 at 7:48
    
This seems good - I'll need to play around with it a bit. –  pdmclean Jan 31 at 9:39
    
@pdmclean I'm glad you like it :) It is good to hold on with an accept a day or two, better answers may appear while now others may feel discouraged ;) –  Kuba Jan 31 at 9:43
add comment

You can add a RegionFunction:

ContourPlot[
  Sin[Sqrt[x^2 + y^2]] Exp[-x^2 - y^2] == 0,
  {x, -10, 10}, {y, -10, 10}, 
  PlotPoints -> 50,
  RegionFunction -> Function[{x, y}, 5 < x^2 + y^2 < 15]
]

enter image description here

What the region itself looks like:

RegionPlot[5 < x^2 + y^2 < 15, {x, -10, 10}, {y, -10, 10}]

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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