5

I would like to plot 2 curves in the same figure with the following code:

import sympy as syp


x, y = syp.symbols('x, y')

my_function = syp.exp(-(x-2)**2)*syp.exp(-(y-3)**2) + 2*syp.exp(-(x+1)**2)*syp.exp(-(y-1)**2) 

gradient_1 = syp.diff(my_function, x)
gradient_2 = syp.diff(my_function, y)


curve_1 = syp.plot_implicit(syp.Eq(gradient_1, 0))
curve_2 = syp.plot_implicit(syp.Eq(gradient_2, 0))

What I see is only the first plot, while I would like to have both the curves in the same picture, maybe also with a grid if possible. Any ideas?

Note: with matplotlib it's very easy, but I cannot find any specific example for the function syp.plot_implicit

user
  • 5,370
  • 8
  • 47
  • 75
user2983638
  • 903
  • 1
  • 13
  • 20

3 Answers3

7

Another, perhaps more efficient way, would be to compute both at the same time using Or

plot_implicit(Or(Eq(gradient_1, 0), Eq(gradient_2, 0)))
asmeurer
  • 86,894
  • 26
  • 169
  • 240
3

It might work if you do:

>>> curve_1.extend(curve_2)
>>> curve_1.show()

However mixing implicit plots might not be implemented yet.

Be aware that your curve_1 and curve_2 are not what sympy considers "single curves" i.e. Series instance, but rather "collections of a number of curves", i.e. Plot instances.

You can also extract the matplotlib objects from curve_1._backend.fig and other _backend attributes.

In conclusion, there is a nice API to do what you want, but probably the methods behind it are not finished yet.

Krastanov
  • 6,479
  • 3
  • 29
  • 42
  • Yes, it works, that's great! I also found another way by using "pyplot.contour" (see [here](http://stackoverflow.com/questions/2484527/is-it-possible-to-plot-implicit-equations-using-matplotlib) ), but it requires the manual calculation of the gradient, so your method is still the best one. Do you know if it can be plotted with a grid? I don't see this option in the parameters that can be passed to the function... – user2983638 Nov 12 '13 at 15:52
0

Another way:

curve_1.append(curve_2[0]) curve_1.show()

Jeremy Anifacc
  • 1,003
  • 10
  • 7