Further information

Further information#

How can you plot a function#

It is possible to plot a function using sympy using the sympy.plot function:

Usage

sympy.plot(expression)

So for example, here is a plot of \(f(x)=x^2 + 3x + 1\):

import sympy as sym

x = sym.Symbol("x")
sym.plot(x ** 2 + 3 * x + 1)
../../../_images/ed7917115c3f3562e2d74e0f0e9956cd6eaf5a24177299592ae939d62c7f005c.png
<sympy.plotting.backends.matplotlibbackend.matplotlib.MatplotlibBackend at 0x7f81de339160>

It is possible to specify the \(x\) and combine it with other plots:

sym.plot(x ** 2 + 3 * x + 1, xlim=(-5, 5))
../../../_images/08c60ae9a8204c6345f433e5d486cea5d9745896c3f8d1bb91e15f549833a2af.png
<sympy.plotting.backends.matplotlibbackend.matplotlib.MatplotlibBackend at 0x7f81d04ccf50>

This plotting solution is good if you want to take a look at a function quickly but it is not recommended. The main python library for plotting is called matplotlib and is covered in Matplotlib.