Sympy IV - Visualization

JunjieCai

2022-05-22

中文

It is recommended to download the .ipynb file and related resources here. Then you can test the codes in the article interactively while you are reading in Jupyter.

Initialize Enviroment

In [1]:
from IPython.display import display, Math
from sympy import *
init_printing()

Plotting

Single variable function

Basic plotting

In [2]:
from sympy import symbols
from sympy.plotting import plot
x = symbols('x')

expr = x*x

plot(expr);
<Figure size 640x480 with 1 Axes>

Suppresss Str representation

If you don't like the '<sympy.plotting.plot.Plot at 0x7f6d26fb8780> string, add a ';' at the end of the plot command.

In [3]:
plot(expr);

Plot with range

To specify the plotting range, pass a tuple of the form (variable, lower_limit, upper_limit)

In [4]:
from sympy import symbols
from sympy.plotting import plot
x = symbols('x')

expr = x**2
expr_range = (x,-2,3)

plot(expr, expr_range);

Title and Label

Pass in keyword arguments Title,xlabel,ylabel. Latex is supported by these arguments.

In [5]:
from sympy import symbols
from sympy.plotting import plot
x = symbols('x')

expr = x**2
expr_range = (x,-2,3)

title = '$y = {}$'.format(latex(expr))

plot(expr, expr_range, title = title, xlabel = 'x', ylabel = 'y');

Line color

Pass keyword argument line_color.

In [6]:
from sympy import symbols
from sympy.plotting import plot
x = symbols('x')

expr = x**2

plot(expr, expr_range, line_color = 'r');

Multiple plot with same range

Use the syntax of plot(expr_1, expr_2, expr_3, range)

In [7]:
expr_1 = x
expr_2 = x**2
expr_3 = x**3

plot(expr_1, expr_2, expr_3, (x, -1, 1));

Multiple plots with different range

Use the syntax of

plot(
    (expr_1,range_1),
    (expr_2,range_2),
    ...
)
In [8]:
expr_1 = x**2
range_1 = (x,-2,2)

expr_2 = x
range_2 = (x,-1,1)

plot(
    (expr_1,range_1),
    (expr_2,range_2)
);

Multiple plots with differnt colors

Plotting multiple plots with different colors is not straight forward.

  1. Pass show = False to suppress displaying the plot, save the returned object to a variable.
  2. Get the indivisual data series by indexing the plotting object and set line_color seperately.
  3. Call show() method of the plotting object to display the image.
In [9]:
expr_1 = x**2
range_1 = (x,-2,2)

expr_2 = x
range_2 = (x,-1,1)

p = plot(
    (expr_1,range_1),
    (expr_2,range_2),
    show = False
);

p[0].line_color = 'r'
p[1].line_color = 'b'

p.show()

To add a legend to the plot, take additional two steps.

  1. Pass Legend = True when construct the plotting object.
  2. Set label for each data series seperately.
In [10]:
expr_1 = x**2
range_1 = (x,-2,2)

expr_2 = x
range_2 = (x,-1,1)

p = plot(
    (expr_1,range_1),
    (expr_2,range_2),
    show = False,
    legend = True
);

p[0].line_color = 'r'
p[1].line_color = 'b'

p[0].label = 'Line 1'
p[1].label = 'Line 2'

p.show()

3D surface plot

Single plot

Use plot3d(expr, x_range , y_range) to draw a 3D surface plot.

In [11]:
from sympy import symbols
from sympy.plotting import plot3d

x, y = symbols('x y')

expr = x*y
x_range = (x, -5, 5)
y_range = (y, -5, 5)

plot3d(expr, x_range, y_range);

Multiple plot

Use the following syntax to draw multiple 3D surface plot.

plot3d(
    (expr_1, x_range_1 , y_range_1),
    (expr_2, x_range_2 , y_range_2)
)
In [12]:
plot3d(
    (x**2 + y**2, (x, -5, 5), (y, -5, 5)),
    (x*y, (x, -3, 3), (y, -3, 3))
);

Single variable parametric function

Use plot_parametric(expr_x, expr_y, range_u) to draw a single variable parametric function.

In [13]:
from sympy import symbols, cos, sin
from sympy.plotting import plot_parametric

u = symbols('u')
expr_x = cos(u)
expr_y = sin(u)

p = plot_parametric(expr_x, expr_y, (u, -5, 5));

The result is not a perfect circle. Sympy offers the aspect_ratio argment to adjust the ratio, but it doesn't work in sympy 1.0 yet. The community is working on the problem and it may work in the next release.

3D parametric line

Use plot3d_parametric_line(expr_x, expr_y, expr_z, range_u) to draw a 3D parametric function.

In [14]:
from sympy import symbols, cos, sin
from sympy.plotting import plot3d_parametric_line
u = symbols('u')
expr_x = cos(u)
expr_y = sin(u)
expr_z = u

plot3d_parametric_line(expr_x, expr_y, expr_z, (u, -5, 5));

3D parametric surface

Use plot3d_parametric_surface(expr_x, expr_y, expr_z, u_range, v_range) to draw a 3d parametric surface.

In [15]:
from sympy import symbols, cos, sin
from sympy.plotting import plot3d_parametric_surface
u, v = symbols('u v')

expr_x = cos(u + v)
expr_y = sin(u-v)
expr_z = u-v
u_range = (u, -5, 5)
v_range = (v, -5, 5)

plot3d_parametric_surface(expr_x, expr_y, expr_z, u_range, v_range);

Implicit function

Single variable implicit function

Use plot_implicit(equition) to draw implicit function plotting.

In [16]:
p1 = plot_implicit(Eq(x**2 + y**2-5))
/usr/local/lib/python3.7/site-packages/sympy/core/relational.py:470: SymPyDeprecationWarning: 

Eq(expr) with rhs default to 0 has been deprecated since SymPy 1.5.
Use Eq(expr, 0) instead. See
https://github.com/sympy/sympy/issues/16587 for more info.

  deprecated_since_version="1.5"

Multiple variable implicit function

In [17]:
p2 = plot_implicit(
    Eq(x**2 + y**2, 3),
    (x, -3, 3), 
    (y, -3, 3)
)

Implicit inequilities

Pass an inequility to plot_implicit

In [18]:
plot_implicit(y > x**2);

To combine several conditions to define the region, and And,Or logic conjunctions.

In [19]:
plot_implicit(And(y > x, y > -x));
In [20]:
plot_implicit(Or(y > x, y > -x));

Sometimes Sympy doesn't choose the variable for horizontal axis as you expect.

In [21]:
plot_implicit(Eq(y - 1));

In this case, use x_var to choose the variable for x_axis.

In [22]:
plot_implicit(Eq(y - 1),x_var=x);