简体   繁体   中英

Plotting 2 or more functions in the same graph

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

另一种可能更有效的方法是使用Or同时计算两者

plot_implicit(Or(Eq(gradient_1, 0), Eq(gradient_2, 0)))

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" ie Series instance, but rather "collections of a number of curves", ie 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.

其他方式:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM