2
$\begingroup$

$f: R^2 -> R,$ $f(x,y)=x²-y²$

VectorPlot[{2 x, -2 y}, {x, -3, 3}, {y, -3, 3}] 
ContourPlot[x^2 - y^2, {x, -3, 3}, {y, -3, 3}, ColorFunction -> "DeepSeaColors",
     PlotLegends -> Automatic]

I want both plots (VectorPlot and ContourPlot) shown in one figure. How to approach this ?

$\endgroup$
  • 1
    $\begingroup$ Take a look at Show. $\endgroup$ – Kuba May 19 '15 at 8:07
  • $\begingroup$ have a look at Show $\endgroup$ – glS May 19 '15 at 8:08
  • $\begingroup$ Please, if possible close this as a duplicate since it has surely been asked before. That will assist me in correctly applying the faq tag. $\endgroup$ – Mr.Wizard May 19 '15 at 9:12
2
$\begingroup$

Try this

PIC1 = VectorPlot[{2 x, -2 y}, {x, -3, 3}, {y, -3, 3}];
PIC2 = ContourPlot[x^2 - y^2, {x, -3, 3}, {y, -3, 3}, 
   ColorFunction -> "DeepSeaColors", PlotLegends -> Automatic];
Show[PIC2, PIC1]

Mathematica graphics

|improve this answer|||||
$\endgroup$
2
$\begingroup$

Try the Epilog option

ContourPlot[z, {x, -3, 3}, {y, -3, 3}, 
 Epilog -> 
  First@
   VectorPlot[Evaluate@Grad[z, {x, y}], {x, -3, 3}, {y, -3, 3}]
 ]

Output of commands

|improve this answer|||||
$\endgroup$
1
$\begingroup$

The output of the Plot commands are Graphics objects which are by default shown graphically by the notebook interface -- but you are free to collect it and do other stuff.

Also, since you seem to be new to Mathematica: You can use Grad to compute the gradient.

z = x^2 - y^2;
Show[{
  ContourPlot[z, {x, -3, 3}, {y, -3, 3}],
  VectorPlot[Evaluate@Grad[z, {x, y}], {x, -3, 3}, {y, -3, 3}]
  }]

The Evaluate inside the VectorPlot tells Mathematica to evaluate this expression before starting to plot. Otherwise Mathematica would substitute specific values for x and y first, after which the gradient doesn't make much sense.

Output of commands

|improve this answer|||||
$\endgroup$

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