Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting 2 or more functions in the same graph

Tags:

python

plot

sympy

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

like image 383
user2983638 Avatar asked Nov 12 '13 14:11

user2983638


2 Answers

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)))
like image 97
asmeurer Avatar answered Sep 28 '22 19:09

asmeurer


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.

like image 26
Krastanov Avatar answered Sep 28 '22 18:09

Krastanov