Matplotlib 2 -绘图、统计、网格、3D

文章目录

  • 绘图类型
  • 成对数据
  • [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)

plot_wireframe(X, Y, Z)

图库由Sphinx-Gallery生成


成对数据

https://matplotlib.org/stable/plot_types/basic/index.html

展示成对数据 \((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)


plot(x, y)

https://matplotlib.org/stable/plot_types/basic/plot.html

绘制 y 相对于 x 的线条和/或标记图。

详情参阅 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()


scatter(x, y)

https://matplotlib.org/stable/plot_types/basic/scatter_plot.html

散点图,展示 y 随 x 的变化关系,支持调整标记点的大小和/或颜色。

详情参阅 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()


bar(x, height)

https://matplotlib.org/stable/plot_types/basic/bar.html

参见 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()


stem(x, y)

https://matplotlib.org/stable/plot_types/basic/stem.html

创建茎干图。

参见 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()


fill_between(x, y1, y2)

https://matplotlib.org/stable/plot_types/basic/fill_between.html

填充两条水平曲线之间的区域。

详情参阅 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()


stackplot(x, y)

https://matplotlib.org/stable/plot_types/basic/stackplot.html

绘制堆叠面积图或流图。

详情参阅 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()


stairs(values)

https://matplotlib.org/stable/plot_types/basic/stairs.html

绘制阶梯状常数函数,可选择以线条或填充区域形式呈现。

当需要在区间\((x_i, x_{i+1}))内绘制\(y)值时,请参考stairs方法。若需在\(x)点处绘制\(y)值,请使用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 : https://matplotlib.org/stable/_downloads/1b052bbfe9e6d6248dc225ff7df9ca18/stairs.ipynb


统计分布

https://matplotlib.org/stable/plot_types/stats/index.html

展示数据集中至少一个变量的分布情况。部分方法还会计算分布特征。



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)


hist(x)

https://matplotlib.org/stable/plot_types/stats/hist_plot.html

计算并绘制直方图。

详情参阅 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()


boxplot(X)

https://matplotlib.org/stable/plot_types/stats/boxplot_plot.html

绘制箱线图。

详情参见 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()


errorbar(x, y, yerr, xerr)

https://matplotlib.org/stable/plot_types/stats/errorbar_plot.html

绘制带有误差条的y对x的线图和/或标记图。

参见 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()


小提琴图绘制指南

https://matplotlib.org/stable/plot_types/stats/violin.html

绘制小提琴图。

API 详情参阅 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

下载 Python 源代码文件:violin.py

下载压缩包文件:violin.zip(https://matplotlib.org/stable/_downloads/25e3d8683ff1d01a46d298bac7568bc7/violin.zip\>

图库由 Sphinx-Gallery 生成


eventplot(D)

https://matplotlib.org/stable/plot_types/stats/eventplot.html

在给定位置绘制相同的平行线。

详情参阅 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()


hist2d(x, y)

https://matplotlib.org/stable/plot_types/stats/hist2d.html

绘制二维直方图。

查看 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()


hexbin(x, y, C)

https://matplotlib.org/stable/plot_types/stats/hexbin.html

生成点集x, y的二维六边形分箱图。

详情参阅 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()


pie(x)

https://matplotlib.org/stable/plot_types/stats/pie.html

绘制饼图。

查看 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()


ecdf(x)

https://matplotlib.org/stable/plot_types/stats/ecdf.html

计算并绘制x的经验累积分布函数。

详情参见 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()


网格数据

https://matplotlib.org/stable/plot_types/arrays/index.html

展示数组和图像 \(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)


imshow(Z)

https://matplotlib.org/stable/plot_types/arrays/imshow.html

将数据显示为图像,即在二维规则栅格上呈现。

参见 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()


pcolormesh(X, Y, Z)

https://matplotlib.org/stable/plot_types/arrays/pcolormesh.html

创建一个非规则矩形网格的伪彩色图。

pcolormeshimshow 更灵活,因为其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()


contour(X, Y, Z)

https://matplotlib.org/stable/plot_types/arrays/contour.html

绘制等高线图。

参考 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()


contourf(X, Y, Z)

https://matplotlib.org/stable/plot_types/arrays/contourf.html

绘制填充等高线图。

查看 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()


barbs(X, Y, U, V)

https://matplotlib.org/stable/plot_types/arrays/barbs.html

绘制二维风羽图。

参见 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()


quiver(X, Y, U, V)

https://matplotlib.org/stable/plot_types/arrays/quiver.html

绘制二维箭头场图。

详见 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()


streamplot(X, Y, U, V)

https://matplotlib.org/stable/plot_types/arrays/streamplot.html

绘制向量场的流线图。

详情参阅 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()


非规则网格数据

https://matplotlib.org/stable/plot_types/unstructured/index.html

绘制在非结构化网格上的数据\(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)


tricontour(x, y, z)

https://matplotlib.org/stable/plot_types/unstructured/tricontour.html

在非结构化三角形网格上绘制等高线。

参见 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()


tricontourf(x, y, z)

https://matplotlib.org/stable/plot_types/unstructured/tricontourf.html

在非结构化三角形网格上绘制填充等高线区域。

详情参阅 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()


tripcolor(x, y, z)

https://matplotlib.org/stable/plot_types/unstructured/tripcolor.html

创建非结构化三角形网格的伪彩色图。

参见 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()


triplot(x, y)

https://matplotlib.org/stable/plot_types/unstructured/triplot.html

绘制非结构化三角网格的线条和/或标记。

详情参阅 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()


三维与体积数据可视化

https://matplotlib.org/stable/plot_types/3D/index.html

使用 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)](voxels_simple.html#sphx-glr-plot-types-3d-voxels-simple-py)

voxels(x, y, z, filled)



plot_wireframe(X, Y, Z)

plot_wireframe(X, Y, Z)


bar3d(x, y, z, dx, dy, dz)

https://matplotlib.org/stable/plot_types/3D/bar3d_simple.html

参见 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()


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

https://matplotlib.org/stable/plot_types/3D/fill_between3d_simple.html

参见 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()


plot(xs, ys, zs)

https://matplotlib.org/stable/plot_types/3D/plot3d_simple.html

参见 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()


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

https://matplotlib.org/stable/plot_types/3D/quiver3d_simple.html

参见 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()


scatter(xs, ys, zs)

https://matplotlib.org/stable/plot_types/3D/scatter3d_simple.html

参见 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()


stem(x, y, z)

https://matplotlib.org/stable/plot_types/3D/stem3d.html

参见 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()


plot_surface(X, Y, Z)

https://matplotlib.org/stable/plot_types/3D/surface3d_simple.html

参见 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()


plot_trisurf(x, y, z)

https://matplotlib.org/stable/plot_types/3D/trisurf3d_simple.html

参见 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()


voxels(x, y, z, filled)

https://matplotlib.org/stable/plot_types/3D/voxels_simple.html

参见 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()


plot_wireframe(X, Y, Z)

https://matplotlib.org/stable/plot_types/3D/wire3d_simple.html

参见 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()


2025-09-10(三)

相关推荐
风流 少年8 小时前
数据可视化:matplotlib、pyecharts、panda、seaborn
数据挖掘·数据分析·matplotlib
2601_9564141415 小时前
AI3D模型生成工具怎么比较?2026主流平台对比与实测维度解析
人工智能·3d
fai厅的秃头姐!15 小时前
Matplotlib
matplotlib
蓝速科技21 小时前
蓝速科技视觉 3D 全息仓 AI 数字人一体机深度评测
人工智能·科技·3d
chaoyuanl1 天前
现有游乐设施 XR 数字化升级改造方案
大数据·科技·3d·xr·娱乐
3D小将1 天前
3D格式转换之3MF模型转GLB通用技术
3d·solidworks模型·ug模型·rhino模型·catia模型
韭菜钟1 天前
利用OpenCV进行3d相机手眼标定(眼在手上/眼在手外)
opencv·3d
数智工坊2 天前
视图集合化+轻量Transformer:VSFormer开启多视图3D形状理解新范式
深度学习·3d·transformer
digitalpath2 天前
AutoCAD Plant3D免费版下载安装教程(附安装包)AutoCAD Plant3D 2026 保姆级安装教程
其他·3d
具身新纪元2 天前
ICML 2026|工业3D质检迎来新SOTA:零样本缺陷检测开始拼“多模态硬融合”
3d