Sympy 简明教程
SymPy - Plotting
SymPy 使用 Matplotlib 库作为后端来渲染数学函数的 2D 和 3D 图。确保在当前 Python 安装中可以使用 Matplotlib。如果没有,请使用以下命令安装相同的命令 -
SymPy uses Matplotlib library as a backend to render 2-D and 3-D plots of mathematical functions. Ensure that Matplotlib is available in current Python installation. If not, install the same using following command −
pip install matplotlib
绘图支持在 sympy.plotting 模块中定义。绘图模块中存在以下函数 -
Plotting support is defined in sympy.plotting module. Following functions are present in plotting module −
-
plot − 2D line plots
-
plot3d − 3D line plots
-
plot_parametric − 2D parametric plots
-
plot3d_parametric − 3D parametric plots
plot() 函数返回绘图类的一个实例。一个绘图图形可能有一个或多个 SymPy 表达式。虽然它能够使用 Matplotlib 作为后端,但也可以使用其他后端,如 texplot、pyglet 或 Google charts API。
The plot() function returns an instance of Plot class. A plot figure may have one or more SymPy expressions. Although it is capable of using Matplotlib as backend, other backends such as texplot, pyglet or Google charts API may also be used.
plot(expr, range, kwargs)
其中 expr 是任何有效的 symPy 表达式。如果没有注明,则范围使用默认值 (-10, 10)。
where expr is any valid symPy expression. If not mentioned, range uses default as (-10, 10).
以下示例绘制了 range(-10,10) 中每个值 x2 的值 -
Following example plots values of x2 for each value in range(-10,10) −
>>> from sympy.plotting import plot
>>> from sympy import *
>>> x=Symbol('x')
>>> plot(x**2, line_color='red')

要为相同范围绘制多张图,请在范围元组之前给出多个表达式。
To draw multiple plots for same range, give multiple expressions prior to the range tuple.
>>> plot( sin(x),cos(x), (x, -pi, pi))

您还可以为每个表达式指定单独的范围。
You can also specify separate range for each expression.
plot((expr1, range1), (expr2, range2))
下图绘制了不同范围内的 sin(x) 和 cos(x)。
Following figure plots sin(x) and cos(x) over different ranges.
>>> plot( (sin(x),(x, -2*pi, 2*pi)),(cos(x), (x, -pi, pi)))

以下可选关键字参数可以在 plot() 函数中指定。
Following optional keyword arguments may be specified in plot() function.
-
line_color − specifies color of the plot line.
-
title − a string to be displayed as title
-
xlabel − a string to be displayed as label for X axis
-
ylabel − a string to be displayed as label for y axis
>>> plot( (sin(x),(x, -2*pi, 2*pi)),(cos(x), (x, -pi, pi)), line_color='red', title='SymPy plot example')

plot3d() 函数渲染一个三维图形。
The plot3d() function renders a three dimensional plot.
plot3d(expr, xrange, yrange, kwargs)
以下示例绘制一个三维曲面图形 −
Following example draws a 3D surface plot −
>>> from sympy.plotting import plot3d
>>> x,y=symbols('x y')
>>> plot3d(x*y, (x, -10,10), (y, -10,10))

与 2D 图形类似,三维图形也可以有多个不同范围的图形。
As in 2D plot, a three dimensional plot can also have multiple plots each with different range.
>>> plot3d(x*y, x/y, (x, -5, 5), (y, -5, 5))

plot3d_parametric_line() 函数渲染三维参数线图形。
The plot3d_parametric_line() function renders a 3 dimensional parametric line plot.
>>> from sympy.plotting import plot3d_parametric_line
>>> plot3d_parametric_line(cos(x), sin(x), x, (x, -5, 5))

要绘制参数曲面图形,请使用 plot3d_parametric_surface() 函数。
To draw a parametric surface plot, use plot3d_parametric_surface() function.
plot3d_parametric_surface(xexpr, yexpr, zexpr, rangex, rangey, kwargs)
>>> from sympy.plotting import plot3d_parametric_surface
>>> plot3d_parametric_surface(cos(x+y), sin(x-y), x-y, (x, -5, 5), (y, -5, 5))
