4
$\begingroup$

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

$\endgroup$
5
$\begingroup$

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

| improve this answer | |
$\endgroup$
  • $\begingroup$ How do you know which contour line this will be, other than testing? Is is always the innermost for example? $\endgroup$ – Mr.Wizard Jan 31 '14 at 7:10
  • $\begingroup$ @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. $\endgroup$ – Kuba Jan 31 '14 at 7:12
  • 1
    $\begingroup$ @Mr.Wizard ok, more general solution added. more convenient for not easy to set region functions. $\endgroup$ – Kuba Jan 31 '14 at 7:48
  • $\begingroup$ This seems good - I'll need to play around with it a bit. $\endgroup$ – pdmclean Jan 31 '14 at 9:39
  • $\begingroup$ @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 ;) $\endgroup$ – Kuba Jan 31 '14 at 9:43
5
$\begingroup$

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

| 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.