0
$\begingroup$

I noticed that I can plot several functions at the same time using Table and Plot, like this:

Plot[Table[Sin[k x], {k, 0, 3}], {x, 0, 4}]

Why doesn't this approach work with ContourPlot? I tried something like this:

ContourPlot[Table[x + y == k, {k, 0, 3}], {x, -4, 4}, {y, -4, 4}]

which unfortunately returns an empty plot.

$\endgroup$
1
  • 2
    $\begingroup$ ContourPlot[ Table[x + y == k, {k, 0, 3}] // Evaluate, {x, -4, 4}, {y, -4, 4}] $\endgroup$
    – cvgmt
    Nov 13, 2020 at 9:17

1 Answer 1

3
$\begingroup$

Add Evaluate

ContourPlot[Evaluate[Table[x + y == k, {k, 0, 3}]], {x, -4, 4}, {y, -4, 4}]

enter image description here

$\endgroup$
5
  • $\begingroup$ Amazing. Thank you! Could you please elaborate a bit more why we need Evaluate here, or in which cases in general Evaluate is necessary? (in particular, why it's not necessary with Plot) $\endgroup$
    – ersbygre1
    Nov 13, 2020 at 9:21
  • $\begingroup$ Probably because Table has attribute Hold All and Evaluate forces evaluation $\endgroup$ Nov 13, 2020 at 9:32
  • $\begingroup$ ...and Plot doesn't care about Hold All? $\endgroup$
    – ersbygre1
    Nov 13, 2020 at 9:37
  • 2
    $\begingroup$ @Stephan - Plot also needs an Evaluate for intended behavior. Compare Plot[Table[Sin[k x], {k, 0, 3}], {x, 0, 4}] with Plot[Table[Sin[k x], {k, 0, 3}] // Evaluate, {x, 0, 4}] $\endgroup$
    – Bob Hanlon
    Nov 13, 2020 at 12:28
  • $\begingroup$ I see, thank you! $\endgroup$
    – ersbygre1
    Nov 14, 2020 at 0:29

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.