# 3.3 绘图

绘图模块允许您制作二维和三维绘图。目前，这些绘图是使用matplotlib作为后端呈现的。如果没有matplotlib，也可以使用textback绘制二维图。

表\[]列举了本节所用到的plot, plot\_parametric, plot3d, plot3d\_parametric函数的通用参数及其功能描述，在sympy.plotting模块中可以导入这些绘图函数，在实际绘图中，我们可以改变相应参数的取值来绘制不同的图形。如果您使用过Mathematica或者Matlab这些专业数学软件，会发现SymPy中存在大量与Mathematica和Matlab中同名的函数。这不仅仅表现在SymPy的绘图模块上，也表现在SymPy的积分，微分等模块。

表\[]

| 参数            | 描述                        |
| ------------- | ------------------------- |
| title         | 字符串，标题                    |
| xlabel        | 字符串，x轴坐标，                 |
| ylabel        | 字符串，y轴坐标                  |
| legend        | 布尔类型，显示图例                 |
| xscale        | {‘linear’,’log’} ，设置x轴范围， |
| yscale        | {‘linear’,’log’}，设置y轴范围   |
| axis          | 布尔类型，显示坐标轴                |
| axis\_center  | 浮点型二元组或{‘center’, ‘auto’} |
| xlim          | 浮点型，x轴的极限                 |
| yim           | 浮点型，y轴的极限                 |
| aspect\_ratio | 浮点型二元组或{‘auto’}           |
| autoscale:    | 布尔类型                      |
| margin        | 0\~1之间的浮点数，图边距            |

## 3.2.1 二维图形

我们使用SymPy绘制的图形都是在特定的画布上展现的，画布上可以展示一张图也可以同时展示多张图。由于不同图形的范围不同，在同一画布上展示多种图形的时候不同图形的绘图范围也会不同。我们在对SymPy中的绘图函数进行讲解时，按照从单图到多图，从同一画布同范围到同一画布不同范围的方式进行展开。

### 一元函数

使用plot函数可以绘制一元函数代表的二维图形。

Plot()函数的用法如下：

单图

```python
plot(expr, range, **kwargs)
```

一张画布显示同范围多图

```python
plot(expr1, expr2, ..., range, **kwargs)
```

一张画布显示不同范围多图

```python
plot((expr1, range), (expr2, range), ..., **kwargs)
```

例：在统一张画布中绘制函数$$f(x)=x^(-1)$$与$$g(x)=x$$，其中$$x\in(-3,3),y\in(0,10)$$.

```python
[]:from sympy.plotting import plot

[]:p1=plot(x\*\*(-1), (x, -3, 3), ylim=(0, 10), show=False)

p2=plot(x, (x, -3, 3), show=False)

p1.append(p2[0])

p1.show()
```

![C:\Users\Johan\AppData\Local\Microsoft\Windows\INetCache\Content.MSO\11904061.tmp](/files/ks00qGOzizrXL9HYJ4Zw)

在绘制图形的时候，变量p1存储的图形对象$$f(x)=x^-1$$,p2存储$$g(x)=x$$的图形对象。如果将plot()函数中的show参数改为True，则会在创建plot对象的同时，显示plot图像。使用append方法可以将其他图形增加到已有图形对象的画布中去，使用show()方法可以显示图形对象。plot函数和其他绘图函数一样，默认返回一个列表，其中第0号元素存储第一个图形。因此在使用append方法追加p2列表对象存储的图形时，一定要指定图形所在的位置。在上例中$$g(x)=x$$同$$f(x)=x^{-1}$$一样被分别存储在列表p1和p2的第0号位置。

例：在同张画布中绘制函数$$f\_1 (x)=x^(-1),f\_2 (x)=x^(1/2),f\_3 (x)=x,f\_4 (x)=x^2,f\_5 (x)=x^3$$，其中$$x\in(-3,3).f\_i (x)\in(0,10),i=1,2,3,4,5.$$

```python
[]:x=symbols('x')

p1=plot(x\*\*(-1),ylim=(0, 10), show=False)

p2=plot(x\*\*(1/2), show=False)

p3=plot(x, show=False)

p4=plot(x\*\*2, show=False)

p5=plot(x\*\*3, show=False)

p1.append(p2[0])

p1.extend(p3)

p1.extend(p4)

p1.extend(p5)

p1.show()
```

使用extend方法可以将向extend方法中传入的列表对象中的图形元素全部追加到目标图形列表后。

![C:\Users\Johan\AppData\Local\Microsoft\Windows\INetCache\Content.MSO\3BAEC217.tmp](/files/KRxFjCsCuLNicLvHkH51)

例：作出 $$x^{-1}$$, $$\log x$$, $$x$$, $$x \log x$$ , $$x^2$$, $$x^3$$的函数趋势图。并将图形取名为graph of functions。

```python
[]:plot(x\*\*(-1), log(x), x, x\*log(x), x\*\*2, x\*\*3, (x, -3, 3), ylim=(0,
10), lengend=True, title='graph of functions')
```

![C:\Users\Johan\AppData\Local\Microsoft\Windows\INetCache\Content.MSO\9F5F5D9.tmp](/files/JwFFk5jg7peRVGXfGIKo)

\[]:\<sympy.plotting.plot.Plot at 0x141d0400>

### 参数方程

SymPy中的plot\_parametric()函数可以绘制参数方程代表的二维图形。

plot\_parametric()函数的用法如下：

单图

```python
plot\_parametric(expr_x, expr_y, range, \*\*kwargs)
```

一张画布显示同范围多图

```python
plot\_parametric((expr1_x, expr1_y), (expr2_x, expr2_y), range, \*\*kwargs)
```

一张画布显示不同范围多图

```python
plot\_parametric((expr_x, expr_y, range), ..., \*\*kwargs)
```

例：画出参数方程$$ \begin{cases} x & = 2 \sin x - sin 2x \ y & = 2 \cos x - cos 2x \end{cases}

$$$
的图像 `python []:from sympy.plotting import plot_parametric []:plot\_parametric(cos(x), sin(x)) ` ![C:\Users\Johan\AppData\Local\Microsoft\Windows\INetCache\Content.MSO\2E5C2187.tmp](../media/7270b52b96b6713012bd95dc23b9a832.png) `python []:\<sympy.plotting.plot.Plot at 0x1430a588\> ` 例：画出参数方程$$ \begin{cases} x & = \cos t \ y & \sin t \end{cases} $$与
$$$

\begin{cases} x & = t \ y & = \cos t \end{cases}

$$$
`python []:plot\_parametric((cos(x), sin(x)), (x, cos(x))) ` ![C:\Users\Johan\AppData\Local\Microsoft\Windows\INetCache\Content.MSO\3052DC35.tmp](../media/c3d5616249b8982b5b5d69faa8971fc3.png) `python []:\<sympy.plotting.plot.Plot at 0xbb0f6d8\> ` ## 3.3.2 三维图形 ### 二元函数 使用plot3d函数可以绘制二元函数代表的三维图形。 plot3d ()函数的用法如下： `python plot3d(expr, range_x, range_y, \*\*kwargs) ` 一张画布显示同范围多图 `python plot3d(expr1, expr2, range_x, range_y, \*\*kwargs) ` 一张画布显示不同范围多图 `python plot3d((expr1, range_x, range_y), (expr2, range_x, range_y), ..., \*\*kwargs) ` 例：画出函数$$z=x^2 y$$的图像，其中 $$x, y \in (-5, 5) $$ `python []:from sympy.plotting import plot3d []:plot3d(x\*\*2\*y, (x, -5, 5), (y, -5, 5)) ` ![C:\Users\Johan\AppData\Local\Microsoft\Windows\INetCache\Content.MSO\9C87D10B.tmp](../media/482cef54c33b1ac8fc4174fed4ef7476.png) `python []:\<sympy.plotting.plot.Plot at 0x17364eb8\> ` 例：画出函数$$z=x^2+y^2$$与$$z=x^2-y^2$$的组合图像，其中$$ x, y \in (-5, 5) $$ `python []:plot3d(x\*\*2+y\*\*2, x\*\*2-y\*\*2, (y, -5, 5)) ` ![C:\Users\Johan\AppData\Local\Microsoft\Windows\INetCache\Content.MSO\DC4A5A51.tmp](../media/451d310dce0db1ed6fbda1872f500b42.png) `python []:\<sympy.plotting.plot.Plot at 0x17238e10\> ` ### 参数方程 1. 空间曲线 使用plot3d\_parametric\_line函数可以绘制参数方程代表的三维图形。 plot3d\_parametric\_line的用法如下： 单图 `python plot3d_parametric\_line(expr_x, expr_y, expr_z, range_u, range_v, \*\*kwargs) ` 一张画布显示不同范围多图 `python plot3d_parametric\_line((expr_x, expr_y, expr_z, range_u, range_v), ..., \*\*kwargs) ` 例：绘制螺旋线$$ \begin{cases} x & = t \ y & = \frac{t \sin 6t}{3} \ z & = \frac{t \cos 6t}{5} \end{cases}
$$$

```python
[]:from sympy.plotting import plot3d_parametric_line

[]:plot3d_parametric_line(x,sin(6\*x)\*x/3,cos(6\*x)\*x/5,(x,0,6\*pi))
```

![C:\Users\Johan\AppData\Local\Microsoft\Windows\INetCache\Content.MSO\2D800C2D.tmp](/files/qVNCTILc03jQP1lNJOdf)

```python
[]:\<sympy.plotting.plot.Plot at 0x18f410f0\>
```

2. 空间曲面

使用plot3d\_parametric\_surface函数可以绘制参数方程代表的三维图形。

plot3d\_parametric\_surface的用法如下：

单图

```python
plot3d_parametric\_surface(expr_x, expr_y, expr_z, range_u, range_v, \*\*kwargs)
```

一张画布显示不同范围多图

```python
plot3d_parametric\_surface((expr_x, expr_y, expr_z, range_u, range_v), ...,
\*\*kwargs)
```

例：绘制参数方程$$ \begin{cases} x & = \cos (m-n) \ y & = \sin (m-n) \ z & = m - n \end{cases}

$$
的图像。 `python []:from sympy.plotting import plot3d_parametric_surface []:plot3d_parametric_surface(cos(x + y), sin(x - y), x - y, (x, -5, 5), (y, -5, 5)) ` \[]:![C:\Users\Johan\AppData\Local\Microsoft\Windows\INetCache\Content.MSO\AC5AEA35.tmp](../media/8787289a54358dd62fbba28d8e829951.png) `python []:\<sympy.plotting.plot.Plot at 0xbe42550\> `
$$


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://johannesliu.gitbook.io/learning-advanced-mathematics-with-python/3.0-sympy_foundation/3.3-plot.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
