文章目录
- 绘图类型
-
- 成对数据
- [plot(x, y)](#plot(x, y))
- [scatter(x, y)](#scatter(x, y))
- [bar(x, height)](#bar(x, height))
- [stem(x, y)](#stem(x, y))
- [fill_between(x, y1, y2)](#fill_between(x, y1, y2))
- [stackplot(x, y)](#stackplot(x, y))
- stairs(values)
- 统计分布
- hist(x)
- boxplot(X)
- [errorbar(x, y, yerr, xerr)](#errorbar(x, y, yerr, xerr))
- 小提琴图绘制指南
- eventplot(D)
- [hist2d(x, y)](#hist2d(x, y))
- [hexbin(x, y, C)](#hexbin(x, y, C))
- pie(x)
- ecdf(x)
- 网格数据
- imshow(Z)
- [pcolormesh(X, Y, Z)](#pcolormesh(X, Y, Z))
- [contour(X, Y, Z)](#contour(X, Y, Z))
- [contourf(X, Y, Z)](#contourf(X, Y, Z))
- [barbs(X, Y, U, V)](#barbs(X, Y, U, V))
- [quiver(X, Y, U, V)](#quiver(X, Y, U, V))
- [streamplot(X, Y, U, V)](#streamplot(X, Y, U, V))
- 非规则网格数据
- [tricontour(x, y, z)](#tricontour(x, y, z))
- [tricontourf(x, y, z)](#tricontourf(x, y, z))
- [tripcolor(x, y, z)](#tripcolor(x, y, z))
- [triplot(x, y)](#triplot(x, y))
- 三维与体积数据可视化
- [bar3d(x, y, z, dx, dy, dz)](#bar3d(x, y, z, dx, dy, dz))
- [fill_between(x1, y1, z1, x2, y2, z2)](#fill_between(x1, y1, z1, x2, y2, z2))
- [plot(xs, ys, zs)](#plot(xs, ys, zs))
- [quiver(X, Y, Z, U, V, W)](#quiver(X, Y, Z, U, V, W))
- [scatter(xs, ys, zs)](#scatter(xs, ys, zs))
- [stem(x, y, z)](#stem(x, y, z))
- [plot_surface(X, Y, Z)](#plot_surface(X, Y, Z))
- [plot_trisurf(x, y, z)](#plot_trisurf(x, y, z))
- [voxels(\[x, y, z], filled)](#voxels([x, y, z], filled))
- [plot_wireframe(X, Y, Z)](#plot_wireframe(X, Y, Z))
绘图类型
https://matplotlib.org/stable/plot_types/index.html
Matplotlib 提供的多种常见绘图命令概览。
更多示例请查看图库,完整教程请访问教程页面。
成对数据
展示成对数据 \((x, y))、表格数据 \((var_0, \cdots, var_n)) 以及函数数据 \(f(x)=y) 的图表。
plot(x, y)
plot(x, y)

scatter(x, y)
scatter(x, y)

bar(x, height)
bar(x, height)

stem(x, y)
stem(x, y)

fill_between(x, y1, y2)
fill_between(x, y1, y2)

stackplot(x, y)
stackplot(x, y)

stairs(values)
stairs(values)
统计分布
展示数据集中至少一个变量的分布情况。部分方法还会计算分布特征。
hist(x)
hist(x)

boxplot(X)
boxplot(X)

errorbar(x, y, yerr, xerr)
errorbar(x, y, yerr, xerr)

violinplot(D)
violinplot(D)

eventplot(D)
eventplot(D)

hist2d(x, y)
hist2d(x, y)

hexbin(x, y, C)
hexbin(x, y, C)

pie(x)
pie(x)

ecdf(x)
ecdf(x)
网格化数据
展示数组和图像 \(Z_{i, j}) 以及场数据 \(U_{i, j}, V_{i, j}) 在规则网格上的可视化效果,以及对应的坐标网格 \(X_{i,j}, Y_{i,j})。
imshow(Z)
imshow(Z)

pcolormesh(X, Y, Z)
pcolormesh(X, Y, Z)

contour(X, Y, Z)
contour(X, Y, Z)

contourf(X, Y, Z)
contourf(X, Y, Z)

barbs(X, Y, U, V)
barbs(X, Y, U, V)

quiver(X, Y, U, V)
quiver(X, Y, U, V)

streamplot(X, Y, U, V)
streamplot(X, Y, U, V)
非规则网格数据
绘制在非结构化网格上的数据\(Z_{x, y})、非结构化坐标网格\((x, y))以及二维函数\(f(x, y) = z)的图表。
tricontour(x, y, z)
tricontour(x, y, z)

tricontourf(x, y, z)
tricontourf(x, y, z)

tripcolor(x, y, z)
tripcolor(x, y, z)

triplot(x, y)
triplot(x, y)
三维与体数据可视化
使用 mpl_toolkits.mplot3d
库绘制三维坐标点 \((x,y,z))、曲面函数 \(f(x,y)=z) 以及体数据 \(V_{x, y, z}) 的可视化图表。
bar3d(x, y, z, dx, dy, dz)
bar3d(x, y, z, dx, dy, dz)

fill_between(x1, y1, z1, x2, y2, z2)
fill_between(x1, y1, z1, x2, y2, z2)

plot(xs, ys, zs)
plot(xs, ys, zs)

quiver(X, Y, Z, U, V, W)
quiver(X, Y, Z, U, V, W)

scatter(xs, ys, zs)
scatter(xs, ys, zs)

stem(x, y, z)
stem(x, y, z)

plot_surface(X, Y, Z)
plot_surface(X, Y, Z)

plot_trisurf(x, y, z)
plot_trisurf(x, y, z)

voxels(\[x, y, z\], filled)\](https://matplotlib.org/stable/plot_types/3D/voxels_simple.html#sphx-glr-plot-types-3d-voxels-simple-py)
voxels(\[x, y, z\], filled)

*** ** * ** ***
[plot_wireframe(X, Y, Z)](https://matplotlib.org/stable/plot_types/3D/wire3d_simple.html#sphx-glr-plot-types-3d-wire3d-simple-py)
plot_wireframe(X, Y, Z)
* \[`下载所有Python源码示例: plot_types_python.zip` :
* \[`下载所有Jupyter笔记本示例: plot_types_jupyter.zip` :
[图库由Sphinx-Gallery生成](https://sphinx-gallery.github.io)
*** ** * ** ***
## 成对数据
展示成对数据 \\((x, y))、表格数据 \\((var_0, \\cdots, var_n)) 以及函数数据 \\(f(x)=y) 的图表。
*** ** * ** ***

*** ** * ** ***
[plot(x, y)](https://matplotlib.org/stable/plot_types/basic/plot.html#sphx-glr-plot-types-basic-plot-py)
plot(x, y)

*** ** * ** ***
[scatter(x, y)](https://matplotlib.org/stable/plot_types/basic/scatter_plot.html#sphx-glr-plot-types-basic-scatter-plot-py)
scatter(x, y)

*** ** * ** ***
[bar(x, height)](bar.html#sphx-glr-plot-types-basic-bar-py)
bar(x, height)

*** ** * ** ***
[stem(x, y)](stem.html#sphx-glr-plot-types-basic-stem-py)
stem(x, y)

*** ** * ** ***
[fill_between(x, y1, y2)](fill_between.html#sphx-glr-plot-types-basic-fill-between-py)
fill_between(x, y1, y2)

*** ** * ** ***
[stackplot(x, y)](stackplot.html#sphx-glr-plot-types-basic-stackplot-py)
stackplot(x, y)

*** ** * ** ***
[stairs(values)](stairs.html#sphx-glr-plot-types-basic-stairs-py)
stairs(values)
*** ** * ** ***
## plot(x, y)
绘制 y 相对于 x 的线条和/或标记图。
详情参阅 [`plot`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.plot.html#matplotlib.axes.Axes.plot "matplotlib.axes.Axes.plot") 文档。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# make data
x= np.linspace(0, 10, 100)
y= 4 + 1 * np.sin(2 * x)
x2 = np.linspace(0, 10, 25)
y2 = 4 + 1 * np.sin(2 * x2)
# plot
fig, ax = plt.subplots()
ax.plot(x2, y2 + 2.5, 'x', markeredgewidth=2)
ax.plot(x, y, linewidth=2.0)
ax.plot(x2, y2 - 2.5, 'o-', linewidth=2)
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `plot.ipynb` :
* 下载 Python 源代码: `plot.py` :
* 下载压缩包: `plot.zip` :
*** ** * ** ***
## scatter(x, y)
散点图,展示 y 随 x 的变化关系,支持调整标记点的大小和/或颜色。
详情参阅 [`scatter`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.scatter.html#matplotlib.axes.Axes.scatter "matplotlib.axes.Axes.scatter")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# make the data
np.random.seed(3)
x= 4 + np.random.normal(0, 2, 24)
y= 4 + np.random.normal(0, 2, len(x))
# size and color:
sizes = np.random.uniform(15, 80, len(x))
colors = np.random.uniform(15, 80, len(x))
# plot
fig, ax = plt.subplots()
ax.scatter(x, y, s=sizes, c=colors, vmin=0, vmax=100)
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `scatter_plot.ipynb` :
* 下载 Python 源代码: `scatter_plot.py` :
* 下载压缩包: `scatter_plot.zip` :
*** ** * ** ***
## bar(x, height)
参见 [`bar`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.bar.html#matplotlib.axes.Axes.bar "matplotlib.axes.Axes.bar")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# make data:
x= 0.5 + np.arange(8)
y = [4.8, 5.5, 3.5, 4.6, 6.5, 6.6, 2.6, 3.0]
# plot
fig, ax = plt.subplots()
ax.bar(x, y, width=1, edgecolor="white", linewidth=0.7)
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `bar.ipynb` :
* 下载 Python 源代码: `bar.py` :
* 下载压缩包: `bar.zip` :
*** ** * ** ***
## stem(x, y)
创建茎干图。
参见 [`stem`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.stem.html#matplotlib.axes.Axes.stem "matplotlib.axes.Axes.stem")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# make data
x= 0.5 + np.arange(8)
y = [4.8, 5.5, 3.5, 4.6, 6.5, 6.6, 2.6, 3.0]
# plot
fig, ax = plt.subplots()
ax.stem(x, y)
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `stem.ipynb` :
* 下载 Python 源代码: `stem.py` :
* 下载压缩包: `stem.zip` :
*** ** * ** ***
## fill_between(x, y1, y2)
填充两条水平曲线之间的区域。
详情参阅 [`fill_between`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.fill_between.html#matplotlib.axes.Axes.fill_between "matplotlib.axes.Axes.fill_between")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# make data
np.random.seed(1)
x= np.linspace(0, 8, 16)
y1= 3 + 4*x/8 + np.random.uniform(0.0, 0.5, len(x))
y2 = 1 + 2*x/8 + np.random.uniform(0.0, 0.5, len(x))
# plot
fig, ax = plt.subplots()
ax.fill_between(x, y1, y2, alpha=.5, linewidth=0)
ax.plot(x, (y1+ y2)/2, linewidth=2)
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `fill_between.ipynb` :
* 下载 Python 源代码: `fill_between.py` :
* 下载压缩包: `fill_between.zip` :
*** ** * ** ***
## stackplot(x, y)
绘制堆叠面积图或流图。
详情参阅 [`stackplot`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.stackplot.html#matplotlib.axes.Axes.stackplot "matplotlib.axes.Axes.stackplot")
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# make data
x= np.arange(0, 10, 2)
ay = [1, 1.25, 2, 2.75, 3]
by = [1, 1, 1, 1, 1]
cy = [2, 1, 2, 1, 2]
y= np.vstack([ay, by, cy])
# plot
fig, ax = plt.subplots()
ax.stackplot(x, y)
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `stackplot.ipynb` :
* 下载 Python 源代码: `stackplot.py` :
* 下载压缩包: `stackplot.zip` :
*** ** * ** ***
## stairs(values)
绘制阶梯状常数函数,可选择以线条或填充区域形式呈现。
当需要在区间\\((x_i, x_{i+1}))内绘制\\(y)值时,请参考[`stairs`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.stairs.html#matplotlib.axes.Axes.stairs "matplotlib.axes.Axes.stairs")方法。若需在\\(x)点处绘制\\(y)值,请使用[`step`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.step.html#matplotlib.axes.Axes.step "matplotlib.axes.Axes.step")方法。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# make data
y = [4.8, 5.5, 3.5, 4.6, 6.5, 6.6, 2.6, 3.0]
# plot
fig, ax = plt.subplots()
ax.stairs(y, linewidth=2.5)
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()
```
*** ** * ** ***
\[`下载 Jupyter 笔记本文件: stairs.ipynb` :
* 下载 Python 源代码: `stairs.py` :
* 下载压缩包: `stairs.zip` :
*** ** * ** ***
## 统计分布
展示数据集中至少一个变量的分布情况。部分方法还会计算分布特征。
*** ** * ** ***

*** ** * ** ***
[hist(x)](hist_plot.html#sphx-glr-plot-types-stats-hist-plot-py)
hist(x)

*** ** * ** ***
[boxplot(X)](boxplot_plot.html#sphx-glr-plot-types-stats-boxplot-plot-py)
boxplot(X)

*** ** * ** ***
[errorbar(x, y, yerr, xerr)](errorbar_plot.html#sphx-glr-plot-types-stats-errorbar-plot-py)
errorbar(x, y, yerr, xerr)

*** ** * ** ***
[violinplot(D)](violin.html#sphx-glr-plot-types-stats-violin-py)
violinplot(D)

*** ** * ** ***
[eventplot(D)](eventplot.html#sphx-glr-plot-types-stats-eventplot-py)
eventplot(D)

*** ** * ** ***
[hist2d(x, y)](hist2d.html#sphx-glr-plot-types-stats-hist2d-py)
hist2d(x, y)

*** ** * ** ***
[hexbin(x, y, C)](hexbin.html#sphx-glr-plot-types-stats-hexbin-py)
hexbin(x, y, C)

*** ** * ** ***
[pie(x)](pie.html#sphx-glr-plot-types-stats-pie-py)
pie(x)

*** ** * ** ***
[ecdf(x)](ecdf.html#sphx-glr-plot-types-stats-ecdf-py)
ecdf(x)
*** ** * ** ***
## hist(x)
计算并绘制直方图。
详情参阅 [`hist`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.hist.html#matplotlib.axes.Axes.hist "matplotlib.axes.Axes.hist")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# make data
np.random.seed(1)
x= 4 + np.random.normal(0, 1.5, 200)
# plot:
fig, ax = plt.subplots()
ax.hist(x, bins=8, linewidth=0.5, edgecolor="white")
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 56), yticks=np.linspace(0, 56, 9))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `hist_plot.ipynb` :
* 下载 Python 源代码: `hist_plot.py` :
* 下载压缩包: `hist_plot.zip` :
*** ** * ** ***
## boxplot(X)
绘制箱线图。
详情参见 [`boxplot`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.boxplot.html#matplotlib.axes.Axes.boxplot "matplotlib.axes.Axes.boxplot")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# make data:
np.random.seed(10)
D = np.random.normal((3, 5, 4), (1.25, 1.00, 1.25), (100, 3))
# plot
fig, ax = plt.subplots()
VP = ax.boxplot(D, positions=[2, 4, 6], widths=1.5, patch_artist=True,
showmeans=False, showfliers=False,
medianprops={"color": "white", "linewidth": 0.5},
boxprops={"facecolor": "C0", "edgecolor": "white",
"linewidth": 0.5},
whiskerprops={"color": "C0", "linewidth": 1.5},
capprops={"color": "C0", "linewidth": 1.5})
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `boxplot_plot.ipynb` :
* 下载 Python 源代码: `boxplot_plot.py` :
* 下载压缩包: `boxplot_plot.zip` :
*** ** * ** ***
## errorbar(x, y, yerr, xerr)
绘制带有误差条的y对x的线图和/或标记图。
参见 [`errorbar`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.errorbar.html#matplotlib.axes.Axes.errorbar "matplotlib.axes.Axes.errorbar")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# make data:
np.random.seed(1)
x = [2, 4, 6]
y = [3.6, 5, 4.2]
yerr = [0.9, 1.2, 0.5]
# plot:
fig, ax = plt.subplots()
ax.errorbar(x, y, yerr, fmt='o', linewidth=2, capsize=6)
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `errorbar_plot.ipynb` :
* 下载 Python 源代码: `errorbar_plot.py` :
* 下载压缩包: `errorbar_plot.zip` :
*** ** * ** ***
## 小提琴图绘制指南
绘制小提琴图。
API 详情参阅 [`violinplot`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.violinplot.html#matplotlib.axes.Axes.violinplot "matplotlib.axes.Axes.violinplot")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# make data:
np.random.seed(10)
D = np.random.normal((3, 5, 4), (0.75, 1.00, 0.75), (200, 3))
# plot:
fig, ax = plt.subplots()
vp = ax.violinplot(D, [2, 4, 6], widths=2,
showmeans=False, showmedians=False, showextrema=False)
# styling: for body in vp['bodies']:
body.set_alpha(0.9)
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()
```
*** ** * ** ***
下载 Jupyter 笔记本文件:[violin.ipynb](https://matplotlib.org/stable/_downloads/96dfb78e5dc87b48afee09aaf6a84cd4/violin.ipynb)
下载 Python 源代码文件:[violin.py](https://matplotlib.org/stable/_downloads/d9b65d60b61233554e1a6123317baad5/violin.py)
下载压缩包文件:\[violin.zip\](https://matplotlib.org/stable/_downloads/25e3d8683ff1d01a46d298bac7568bc7/violin.zip\>
图库由 [Sphinx-Gallery](https://sphinx-gallery.github.io) 生成
*** ** * ** ***
## eventplot(D)
在给定位置绘制相同的平行线。
详情参阅 [`eventplot`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.eventplot.html#matplotlib.axes.Axes.eventplot "matplotlib.axes.Axes.eventplot")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# make data:
np.random.seed(1)
x = [2, 4, 6]
D = np.random.gamma(4, size=(3, 50))
# plot:
fig, ax = plt.subplots()
ax.eventplot(D, orientation="vertical", lineoffsets=x, linewidth=0.75)
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `eventplot.ipynb` :
* 下载 Python 源代码: `eventplot.py` :
* 下载压缩包: `eventplot.zip` :
*** ** * ** ***
## hist2d(x, y)
绘制二维直方图。
查看 [`hist2d`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.hist2d.html#matplotlib.axes.Axes.hist2d "matplotlib.axes.Axes.hist2d") 文档。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery-nogrid')
# make data: correlated + noise
np.random.seed(1)
x= np.random.randn(5000)
y= 1.2 * x+ np.random.randn(5000) / 3
# plot:
fig, ax = plt.subplots()
ax.hist2d(x, y, bins=(np.arange(-3, 3, 0.1), np.arange(-3, 3, 0.1)))
ax.set(xlim=(-2, 2), ylim=(-3, 3))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `hist2d.ipynb` :
* 下载 Python 源代码: `hist2d.py` :
* 下载压缩包: `hist2d.zip` :
*** ** * ** ***
## hexbin(x, y, C)
生成点集x, y的二维六边形分箱图。
详情参阅 [`hexbin`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.hexbin.html#matplotlib.axes.Axes.hexbin "matplotlib.axes.Axes.hexbin") 文档。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery-nogrid')
# make data: correlated + noise
np.random.seed(1)
x= np.random.randn(5000)
y= 1.2 * x+ np.random.randn(5000) / 3
# plot:
fig, ax = plt.subplots()
ax.hexbin(x, y, gridsize=20)
ax.set(xlim=(-2, 2), ylim=(-3, 3))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `hexbin.ipynb` :
* 下载 Python 源代码: `hexbin.py` :
* 下载压缩包: `hexbin.zip` :
*** ** * ** ***
## pie(x)
绘制饼图。
查看 [`pie`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.pie.html#matplotlib.axes.Axes.pie "matplotlib.axes.Axes.pie") 函数文档。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery-nogrid')
# make data
x = [1, 2, 3, 4]
colors = plt.get_cmap('Blues')(np.linspace(0.2, 0.7, len(x)))
# plot
fig, ax = plt.subplots()
ax.pie(x, colors=colors, radius=3, center=(4, 4),
wedgeprops={"linewidth": 1, "edgecolor": "white"}, frame=True)
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `pie.ipynb` :
* 下载 Python 源代码: `pie.py` :
* 下载压缩包: `pie.zip` :
*** ** * ** ***
## ecdf(x)
计算并绘制x的经验累积分布函数。
详情参见 [`ecdf`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.ecdf.html#matplotlib.axes.Axes.ecdf "matplotlib.axes.Axes.ecdf")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# make data
np.random.seed(1)
x= 4 + np.random.normal(0, 1.5, 200)
# plot:
fig, ax = plt.subplots()
ax.ecdf(x)
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `ecdf.ipynb` :
* 下载 Python 源代码: `ecdf.py` :
* 下载压缩包: `ecdf.zip` :
*** ** * ** ***
## 网格数据
展示数组和图像 \\(Z_{i, j}) 以及场量 \\(U_{i, j}, V_{i, j}) 在[规则网格](https://en.wikipedia.org/wiki/Regular_grid)上的绘图,以及对应的坐标网格 \\(X_{i,j}, Y_{i,j})。
*** ** * ** ***

*** ** * ** ***
[imshow(Z)](imshow.html#sphx-glr-plot-types-arrays-imshow-py)
imshow(Z)

*** ** * ** ***
[pcolormesh(X, Y, Z)](pcolormesh.html#sphx-glr-plot-types-arrays-pcolormesh-py)
pcolormesh(X, Y, Z)

*** ** * ** ***
[contour(X, Y, Z)](contour.html#sphx-glr-plot-types-arrays-contour-py)
contour(X, Y, Z)

*** ** * ** ***
[contourf(X, Y, Z)](contourf.html#sphx-glr-plot-types-arrays-contourf-py)
contourf(X, Y, Z)

*** ** * ** ***
[barbs(X, Y, U, V)](barbs.html#sphx-glr-plot-types-arrays-barbs-py)
barbs(X, Y, U, V)

*** ** * ** ***
[quiver(X, Y, U, V)](quiver.html#sphx-glr-plot-types-arrays-quiver-py)
quiver(X, Y, U, V)

*** ** * ** ***
[streamplot(X, Y, U, V)](streamplot.html#sphx-glr-plot-types-arrays-streamplot-py)
streamplot(X, Y, U, V)
*** ** * ** ***
## imshow(Z)
将数据显示为图像,即在二维规则栅格上呈现。
参见 [`imshow`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.imshow.html#matplotlib.axes.Axes.imshow "matplotlib.axes.Axes.imshow")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery-nogrid')
# make data
X, Y = np.meshgrid(np.linspace(-3, 3, 16), np.linspace(-3, 3, 16))
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)
# plot
fig, ax = plt.subplots()
ax.imshow(Z, origin='lower')
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `imshow.ipynb` :
* 下载 Python 源代码: `imshow.py` :
* 下载压缩包: `imshow.zip` :
*** ** * ** ***
## pcolormesh(X, Y, Z)
创建一个非规则矩形网格的伪彩色图。
[`pcolormesh`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.pcolormesh.html#matplotlib.axes.Axes.pcolormesh "matplotlib.axes.Axes.pcolormesh") 比 [`imshow`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.imshow.html#matplotlib.axes.Axes.imshow "matplotlib.axes.Axes.imshow") 更灵活,因为其x和y向量不需要等距分布(实际上可以是倾斜的)。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery-nogrid')
# make data with uneven sampling in x
x = [-3, -2, -1.6, -1.2, -.8, -.5, -.2, .1, .3, .5, .8, 1.1, 1.5, 1.9, 2.3, 3]
X, Y = np.meshgrid(x, np.linspace(-3, 3, 128))
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)
# plot
fig, ax = plt.subplots()
ax.pcolormesh(X, Y, Z, vmin=-0.5, vmax=1.0)
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `pcolormesh.ipynb` :
* 下载 Python 源代码: `pcolormesh.py` :
* 下载压缩包: `pcolormesh.zip` :
*** ** * ** ***
## contour(X, Y, Z)
绘制等高线图。
参考 [`contour`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.contour.html#matplotlib.axes.Axes.contour "matplotlib.axes.Axes.contour") 文档。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery-nogrid')
# make data
X, Y = np.meshgrid(np.linspace(-3, 3, 256), np.linspace(-3, 3, 256))
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)
levels = np.linspace(np.min(Z), np.max(Z), 7)
# plot
fig, ax = plt.subplots()
ax.contour(X, Y, Z, levels=levels)
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `contour.ipynb` :
* 下载 Python 源代码: `contour.py` :
* 下载压缩包: `contour.zip` :
*** ** * ** ***
## contourf(X, Y, Z)
绘制填充等高线图。
查看 [`contourf`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.contourf.html#matplotlib.axes.Axes.contourf "matplotlib.axes.Axes.contourf") 函数文档。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery-nogrid')
# make data
X, Y = np.meshgrid(np.linspace(-3, 3, 256), np.linspace(-3, 3, 256))
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)
levels = np.linspace(Z.min(), Z.max(), 7)
# plot
fig, ax = plt.subplots()
ax.contourf(X, Y, Z, levels=levels)
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `contourf.ipynb` :
* 下载 Python 源代码: `contourf.py` :
* 下载压缩包: `contourf.zip` :
*** ** * ** ***
## barbs(X, Y, U, V)
绘制二维风羽图。
参见 [`barbs`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.barbs.html#matplotlib.axes.Axes.barbs "matplotlib.axes.Axes.barbs")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery-nogrid')
# make data:
X, Y = np.meshgrid([1, 2, 3, 4], [1, 2, 3, 4])
angle = np.pi / 180 * np.array([[15., 30, 35, 45],
[25., 40, 55, 60],
[35., 50, 65, 75],
[45., 60, 75, 90]])
amplitude = np.array([[5, 10, 25, 50],
[10, 15, 30, 60],
[15, 26, 50, 70],
[20, 45, 80, 100]])
U = amplitude * np.sin(angle)
V = amplitude * np.cos(angle)
# plot:
fig, ax = plt.subplots()
ax.barbs(X, Y, U, V, barbcolor='C0', flagcolor='C0', length=7, linewidth=1.5)
ax.set(xlim=(0, 4.5), ylim=(0, 4.5))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `barbs.ipynb` :
* 下载 Python 源代码: `barbs.py` :
* 下载压缩包: `barbs.zip` :
*** ** * ** ***
## quiver(X, Y, U, V)
绘制二维箭头场图。
详见 [`quiver`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.quiver.html#matplotlib.axes.Axes.quiver "matplotlib.axes.Axes.quiver") 函数文档。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery-nogrid')
# make data
x= np.linspace(-4, 4, 6)
y= np.linspace(-4, 4, 6)
X, Y = np.meshgrid(x, y)
U = x+ Y
V = Y - X
# plot
fig, ax = plt.subplots()
ax.quiver(X, Y, U, V, color="C0", angles='xy',
scale_units='xy', scale=5, width=.015)
ax.set(xlim=(-5, 5), ylim=(-5, 5))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `quiver.ipynb` :
* 下载 Python 源代码: `quiver.py` :
* 下载压缩包: `quiver.zip` :
*** ** * ** ***
## streamplot(X, Y, U, V)
绘制向量场的流线图。
详情参阅 [`streamplot`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.streamplot.html#matplotlib.axes.Axes.streamplot "matplotlib.axes.Axes.streamplot")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery-nogrid')
# make a stream function:
X, Y = np.meshgrid(np.linspace(-3, 3, 256), np.linspace(-3, 3, 256))
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)
# make U and V out of the streamfunction:
V = np.diff(Z[1:, :], axis=1)
U = -np.diff(Z[:, 1:], axis=0)
# plot:
fig, ax = plt.subplots()
ax.streamplot(X[1:, 1:], Y[1:, 1:], U, V)
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `streamplot.ipynb` :
* 下载 Python 源代码: `streamplot.py` :
* 下载压缩包: `streamplot.zip` :
*** ** * ** ***
## 非规则网格数据
绘制在[非结构化网格](https://en.wikipedia.org/wiki/Unstructured_grid)上的数据\\(Z_{x, y})、非结构化坐标网格\\((x, y))以及二维函数\\(f(x, y) = z)的图表。
*** ** * ** ***

*** ** * ** ***
[tricontour(x, y, z)](tricontour.html#sphx-glr-plot-types-unstructured-tricontour-py)
tricontour(x, y, z)

*** ** * ** ***
[tricontourf(x, y, z)](tricontourf.html#sphx-glr-plot-types-unstructured-tricontourf-py)
tricontourf(x, y, z)

*** ** * ** ***
[tripcolor(x, y, z)](tripcolor.html#sphx-glr-plot-types-unstructured-tripcolor-py)
tripcolor(x, y, z)

*** ** * ** ***
[triplot(x, y)](triplot.html#sphx-glr-plot-types-unstructured-triplot-py)
triplot(x, y)
*** ** * ** ***
## tricontour(x, y, z)
在非结构化三角形网格上绘制等高线。
参见 [`tricontour`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.tricontour.html#matplotlib.axes.Axes.tricontour "matplotlib.axes.Axes.tricontour")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery-nogrid')
# make data:
np.random.seed(1)
x= np.random.uniform(-3, 3, 256)
y= np.random.uniform(-3, 3, 256)
z = (1 - x/2 + x**5 + y**3) * np.exp(-x**2 - y**2)
levels = np.linspace(z.min(), z.max(), 7)
# plot:
fig, ax = plt.subplots()
ax.plot(x, y, 'o', markersize=2, color='lightgrey')
ax.tricontour(x, y, z, levels=levels)
ax.set(xlim=(-3, 3), ylim=(-3, 3))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `tricontour.ipynb` :
* 下载 Python 源代码: `tricontour.py` :
* 下载压缩包: `tricontour.zip` :
*** ** * ** ***
## tricontourf(x, y, z)
在非结构化三角形网格上绘制填充等高线区域。
详情参阅 [`tricontourf`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.tricontourf.html#matplotlib.axes.Axes.tricontourf "matplotlib.axes.Axes.tricontourf")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery-nogrid')
# make data:
np.random.seed(1)
x= np.random.uniform(-3, 3, 256)
y= np.random.uniform(-3, 3, 256)
z = (1 - x/2 + x**5 + y**3) * np.exp(-x**2 - y**2)
levels = np.linspace(z.min(), z.max(), 7)
# plot:
fig, ax = plt.subplots()
ax.plot(x, y, 'o', markersize=2, color='grey')
ax.tricontourf(x, y, z, levels=levels)
ax.set(xlim=(-3, 3), ylim=(-3, 3))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `tricontourf.ipynb` :
* 下载 Python 源代码: `tricontourf.py` :
* 下载压缩包: `tricontourf.zip` :
*** ** * ** ***
## tripcolor(x, y, z)
创建非结构化三角形网格的伪彩色图。
参见 [`tripcolor`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.tripcolor.html#matplotlib.axes.Axes.tripcolor "matplotlib.axes.Axes.tripcolor")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery-nogrid')
# make data:
np.random.seed(1)
x= np.random.uniform(-3, 3, 256)
y= np.random.uniform(-3, 3, 256)
z = (1 - x/2 + x**5 + y**3) * np.exp(-x**2 - y**2)
# plot:
fig, ax = plt.subplots()
ax.plot(x, y, 'o', markersize=2, color='grey')
ax.tripcolor(x, y, z)
ax.set(xlim=(-3, 3), ylim=(-3, 3))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `tripcolor.ipynb` :
* 下载 Python 源代码: `tripcolor.py` :
* 下载压缩包: `tripcolor.zip` :
*** ** * ** ***
## triplot(x, y)
绘制非结构化三角网格的线条和/或标记。
详情参阅 [`triplot`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.triplot.html#matplotlib.axes.Axes.triplot "matplotlib.axes.Axes.triplot")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery-nogrid')
# make data:
np.random.seed(1)
x= np.random.uniform(-3, 3, 256)
y= np.random.uniform(-3, 3, 256)
z = (1 - x/2 + x**5 + y**3) * np.exp(-x**2 - y**2)
# plot:
fig, ax = plt.subplots()
ax.triplot(x, y)
ax.set(xlim=(-3, 3), ylim=(-3, 3))
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `triplot.ipynb` :
* 下载 Python 源代码: `triplot.py` :
* 下载压缩包: `triplot.zip` :
*** ** * ** ***
## 三维与体积数据可视化
使用 [`mpl_toolkits.mplot3d`](https://matplotlib.org/stable/api/toolkits/mplot3d.html#module-mpl_toolkits.mplot3d "mpl_toolkits.mplot3d") 库绘制三维数据 \\((x,y,z))、曲面数据 \\(f(x,y)=z) 以及体积数据 \\(V_{x, y, z})。
*** ** * ** ***

*** ** * ** ***
[bar3d(x, y, z, dx, dy, dz)](bar3d_simple.html#sphx-glr-plot-types-3d-bar3d-simple-py)
bar3d(x, y, z, dx, dy, dz)
*** ** * ** ***

*** ** * ** ***
[fill_between(x1, y1, z1, x2, y2, z2)](fill_between3d_simple.html#sphx-glr-plot-types-3d-fill-between3d-simple-py)
fill_between(x1, y1, z1, x2, y2, z2)
*** ** * ** ***

*** ** * ** ***
[plot(xs, ys, zs)](https://matplotlib.org/stable/plot_types/basic/plot3d_simple.html#sphx-glr-plot-types-3d-plot3d-simple-py)
plot(xs, ys, zs)
*** ** * ** ***

*** ** * ** ***
[quiver(X, Y, Z, U, V, W)](quiver3d_simple.html#sphx-glr-plot-types-3d-quiver3d-simple-py)
quiver(X, Y, Z, U, V, W)
*** ** * ** ***

*** ** * ** ***
[scatter(xs, ys, zs)](scatter3d_simple.html#sphx-glr-plot-types-3d-scatter3d-simple-py)
scatter(xs, ys, zs)
*** ** * ** ***

*** ** * ** ***
[stem(x, y, z)](stem3d.html#sphx-glr-plot-types-3d-stem3d-py)
stem(x, y, z)
*** ** * ** ***

*** ** * ** ***
[plot_surface(X, Y, Z)](surface3d_simple.html#sphx-glr-plot-types-3d-surface3d-simple-py)
plot_surface(X, Y, Z)
*** ** * ** ***

*** ** * ** ***
[plot_trisurf(x, y, z)](trisurf3d_simple.html#sphx-glr-plot-types-3d-trisurf3d-simple-py)
plot_trisurf(x, y, z)
*** ** * ** ***

*** ** * ** ***
\[voxels(\[x, y, z\], filled)\](voxels_simple.html#sphx-glr-plot-types-3d-voxels-simple-py)
voxels(\[x, y, z\], filled)
*** ** * ** ***

*** ** * ** ***
[plot_wireframe(X, Y, Z)](wire3d_simple.html#sphx-glr-plot-types-3d-wire3d-simple-py)
plot_wireframe(X, Y, Z)
*** ** * ** ***
## bar3d(x, y, z, dx, dy, dz)
参见 [`bar3d`](https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.bar3d.html#mpl_toolkits.mplot3d.axes3d.Axes3D.bar3d "mpl_toolkits.mplot3d.axes3d.Axes3D.bar3d")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# Make data
x = [1, 1, 2, 2]
y = [1, 2, 1, 2]
z = [0, 0, 0, 0]
dx = np.ones_like(x)*0.5
dy = np.ones_like(x)*0.5
dz = [2, 3, 1, 4]
# Plot
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.bar3d(x, y, z, dx, dy, dz)
ax.set(xticklabels=[],
yticklabels=[],
zticklabels=[])
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `bar3d_simple.ipynb` :
* 下载 Python 源代码: `bar3d_simple.py` :
* 下载压缩包: `bar3d_simple.zip` :
*** ** * ** ***
## fill_between(x1, y1, z1, x2, y2, z2)
参见 [`fill_between`](https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.fill_between.html#mpl_toolkits.mplot3d.axes3d.Axes3D.fill_between "mpl_toolkits.mplot3d.axes3d.Axes3D.fill_between")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# Make data for a double helix
n = 50
theta = np.linspace(0, 2*np.pi, n)
x1 = np.cos(theta)
y1= np.sin(theta)
z1 = np.linspace(0, 1, n)
x2 = np.cos(theta + np.pi)
y2 = np.sin(theta + np.pi)
z2 = z1
# Plot
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.fill_between(x1, y1, z1, x2, y2, z2, alpha=0.5)
ax.plot(x1, y1, z1, linewidth=2, color='C0')
ax.plot(x2, y2, z2, linewidth=2, color='C0')
ax.set(xticklabels=[],
yticklabels=[],
zticklabels=[])
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `fill_between3d_simple.ipynb` :
* 下载 Python 源代码: `fill_between3d_simple.py` :
* 下载压缩包: `fill_between3d_simple.zip` :
*** ** * ** ***
## plot(xs, ys, zs)
参见 [`plot`](https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.plot.html#mpl_toolkits.mplot3d.axes3d.Axes3D.plot "mpl_toolkits.mplot3d.axes3d.Axes3D.plot")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# Make data
n = 100
xs = np.linspace(0, 1, n)
ys = np.sin(xs * 6 * np.pi)
zs = np.cos(xs * 6 * np.pi)
# Plot
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.plot(xs, ys, zs)
ax.set(xticklabels=[],
yticklabels=[],
zticklabels=[])
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `plot3d_simple.ipynb` :
* 下载 Python 源代码: `plot3d_simple.py` :
* 下载压缩包: `plot3d_simple.zip` :
*** ** * ** ***
## quiver(X, Y, Z, U, V, W)
参见 [`quiver`](https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.quiver.html#mpl_toolkits.mplot3d.axes3d.Axes3D.quiver "mpl_toolkits.mplot3d.axes3d.Axes3D.quiver") 文档。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# Make data
n = 4
x= np.linspace(-1, 1, n)
y= np.linspace(-1, 1, n)
z = np.linspace(-1, 1, n)
X, Y, Z = np.meshgrid(x, y, z)
U = (x+ Y)/5
V = (Y - X)/5
W = Z*0
# Plot
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.quiver(X, Y, Z, U, V, W)
ax.set(xticklabels=[],
yticklabels=[],
zticklabels=[])
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `quiver3d_simple.ipynb` :
* 下载 Python 源代码: `quiver3d_simple.py` :
* 下载压缩包: `quiver3d_simple.zip` :
*** ** * ** ***
## scatter(xs, ys, zs)
参见 [`scatter`](https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.scatter.html#mpl_toolkits.mplot3d.axes3d.Axes3D.scatter "mpl_toolkits.mplot3d.axes3d.Axes3D.scatter") 文档。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# Make data
np.random.seed(19680801)
n = 100
rng = np.random.default_rng()
xs = rng.uniform(23, 32, n)
ys = rng.uniform(0, 100, n)
zs = rng.uniform(-50, -25, n)
# Plot
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.scatter(xs, ys, zs)
ax.set(xticklabels=[],
yticklabels=[],
zticklabels=[])
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `scatter3d_simple.ipynb` :
* 下载 Python 源代码: `scatter3d_simple.py` :
* 下载压缩包: `scatter3d_simple.zip` :
*** ** * ** ***
## stem(x, y, z)
参见 [`stem`](https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.stem.html#mpl_toolkits.mplot3d.axes3d.Axes3D.stem "mpl_toolkits.mplot3d.axes3d.Axes3D.stem")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# Make data
n = 20
x= np.sin(np.linspace(0, 2*np.pi, n))
y= np.cos(np.linspace(0, 2*np.pi, n))
z = np.linspace(0, 1, n)
# Plot
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.stem(x, y, z)
ax.set(xticklabels=[],
yticklabels=[],
zticklabels=[])
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `stem3d.ipynb` :
* 下载 Python 源代码: `stem3d.py` :
* 下载压缩包: `stem3d.zip` :
*** ** * ** ***
## plot_surface(X, Y, Z)
参见 [`plot_surface`](https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.plot_surface.html#mpl_toolkits.mplot3d.axes3d.Axes3D.plot_surface "mpl_toolkits.mplot3d.axes3d.Axes3D.plot_surface")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
plt.style.use('_mpl-gallery')
# Make data
x= np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
# Plot the surface
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.plot_surface(X, Y, Z, vmin=Z.min() * 2, cmap=cm.Blues)
ax.set(xticklabels=[],
yticklabels=[],
zticklabels=[])
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `surface3d_simple.ipynb` :
* 下载 Python 源代码: `surface3d_simple.py` :
* 下载压缩包: `surface3d_simple.zip` :
*** ** * ** ***
## plot_trisurf(x, y, z)
参见 [`plot_trisurf`](https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.plot_trisurf.html#mpl_toolkits.mplot3d.axes3d.Axes3D.plot_trisurf "mpl_toolkits.mplot3d.axes3d.Axes3D.plot_trisurf")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
plt.style.use('_mpl-gallery')
n_radii = 8
n_angles = 36
# Make radii and angles spaces
radii = np.linspace(0.125, 1.0,n_radii)
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)[..., np.newaxis]
# Convert polar (radii, angles) coords to cartesian (x, y) coords.
x= np.append(0, (radii*np.cos(angles)).flatten())
y= np.append(0, (radii*np.sin(angles)).flatten())
z = np.sin(-x*y)
# Plot
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
ax.plot_trisurf(x, y, z, vmin=z.min() * 2, cmap=cm.Blues)
ax.set(xticklabels=[],
yticklabels=[],
zticklabels=[])
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `trisurf3d_simple.ipynb` :
* 下载 Python 源代码: `trisurf3d_simple.py` :
* 下载压缩包: `trisurf3d_simple.zip` :
*** ** * ** ***
## voxels(\[x, y, z\], filled)
参见 [`voxels`](https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.voxels.html#mpl_toolkits.mplot3d.axes3d.Axes3D.voxels "mpl_toolkits.mplot3d.axes3d.Axes3D.voxels") 文档。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# Prepare some coordinates
x, y, z = np.indices((8, 8, 8))
# Draw cuboids in the top left and bottom right corners
cube1 = (x< 3) & (y< 3) & (z < 3)
cube2 = (x>= 5) & (y>= 5) & (z >= 5)
# Combine the objects into a single boolean array
voxelarray = cube1 | cube2
# Plot
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.voxels(voxelarray, edgecolor='k')
ax.set(xticklabels=[],
yticklabels=[],
zticklabels=[])
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `voxels_simple.ipynb` :
* 下载 Python 源代码: `voxels_simple.py` :
* 下载压缩包: `voxels_simple.zip` :
*** ** * ** ***
## plot_wireframe(X, Y, Z)
参见 [`plot_wireframe`](https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.plot_wireframe.html#mpl_toolkits.mplot3d.axes3d.Axes3D.plot_wireframe "mpl_toolkits.mplot3d.axes3d.Axes3D.plot_wireframe")。
*** ** * ** ***

*** ** * ** ***
```python
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
plt.style.use('_mpl-gallery')
# Make data
X, Y, Z = axes3d.get_test_data(0.05)
# Plot
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
ax.set(xticklabels=[],
yticklabels=[],
zticklabels=[])
plt.show()
```
*** ** * ** ***
* 下载 Jupyter 笔记本: `wire3d_simple.ipynb` :
* 下载 Python 源代码: `wire3d_simple.py` :
* 下载压缩包: `wire3d_simple.zip` :
*** ** * ** ***
2025-09-10(三)