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.
from IPython.display import display, Math
from sympy import *
init_printing()
from sympy import symbols
from sympy.plotting import plot
x = symbols('x')
expr = x*x
plot(expr);
If you don't like the '<sympy.plotting.plot.Plot at 0x7f6d26fb8780> string, add a ';' at the end of the plot command.
plot(expr);
To specify the plotting range, pass a tuple of the form (variable, lower_limit, upper_limit)
from sympy import symbols
from sympy.plotting import plot
x = symbols('x')
expr = x**2
expr_range = (x,-2,3)
plot(expr, expr_range);
Pass in keyword arguments Title
,xlabel
,ylabel
. Latex is supported by these arguments.
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');
Pass keyword argument line_color
.
from sympy import symbols
from sympy.plotting import plot
x = symbols('x')
expr = x**2
plot(expr, expr_range, line_color = 'r');
Use the syntax of plot(expr_1, expr_2, expr_3, range)
expr_1 = x
expr_2 = x**2
expr_3 = x**3
plot(expr_1, expr_2, expr_3, (x, -1, 1));
Use the syntax of
plot(
(expr_1,range_1),
(expr_2,range_2),
...
)
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)
);
Plotting multiple plots with different colors is not straight forward.
show = False
to suppress displaying the plot, save the returned object to a variable.show()
method of the plotting object to display the image.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.
Legend = True
when construct the plotting object.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()
Use plot3d(expr, x_range , y_range)
to draw a 3D surface plot.
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);
plot3d(
(x**2 + y**2, (x, -5, 5), (y, -5, 5)),
(x*y, (x, -3, 3), (y, -3, 3))
);
Use plot_parametric(expr_x, expr_y, range_u)
to draw a single variable parametric function.
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.
Use plot3d_parametric_line(expr_x, expr_y, expr_z, range_u)
to draw a 3D parametric function.
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));
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);
p1 = plot_implicit(Eq(x**2 + y**2-5))
p2 = plot_implicit(
Eq(x**2 + y**2, 3),
(x, -3, 3),
(y, -3, 3)
)
Pass an inequility to plot_implicit
plot_implicit(y > x**2);
To combine several conditions to define the region, and And,Or
logic conjunctions.
plot_implicit(And(y > x, y > -x));
plot_implicit(Or(y > x, y > -x));
Sometimes Sympy doesn't choose the variable for horizontal axis as you expect.
plot_implicit(Eq(y - 1));
In this case, use x_var
to choose the variable for x_axis.
plot_implicit(Eq(y - 1),x_var=x);