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) ![三维线框图示例](https://i-blog.csdnimg.cn/img_convert/180ea60289ad32f6d2f9765aa1fd1820.png) *** ** * ** *** [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) 的图表。 *** ** * ** *** ![示例图表](https://i-blog.csdnimg.cn/img_convert/ceca40403b212cd7bf3e799320b515dc.png) *** ** * ** *** [plot(x, y)](https://matplotlib.org/stable/plot_types/basic/plot.html#sphx-glr-plot-types-basic-plot-py) plot(x, y) ![散点图示例](https://i-blog.csdnimg.cn/img_convert/86c221819ed86e3b74af917ae473f1b2.png) *** ** * ** *** [scatter(x, y)](https://matplotlib.org/stable/plot_types/basic/scatter_plot.html#sphx-glr-plot-types-basic-scatter-plot-py) scatter(x, y) ![柱状图示例](https://i-blog.csdnimg.cn/img_convert/ce0ae57c635adad7cd98abc23139746a.png) *** ** * ** *** [bar(x, height)](bar.html#sphx-glr-plot-types-basic-bar-py) bar(x, height) ![茎叶图示例](https://i-blog.csdnimg.cn/img_convert/5738e2651ae1fe6f2e53ac89bc37f0e7.png) *** ** * ** *** [stem(x, y)](stem.html#sphx-glr-plot-types-basic-stem-py) stem(x, y) ![填充区域图示例](https://i-blog.csdnimg.cn/img_convert/11295197bd8d03b1460f175f095a259e.png) *** ** * ** *** [fill_between(x, y1, y2)](fill_between.html#sphx-glr-plot-types-basic-fill-between-py) fill_between(x, y1, y2) ![堆叠图示例](https://i-blog.csdnimg.cn/img_convert/737f7554ed95159c107cbc7dff9ef409.png) *** ** * ** *** [stackplot(x, y)](stackplot.html#sphx-glr-plot-types-basic-stackplot-py) stackplot(x, y) ![阶梯图示例](https://i-blog.csdnimg.cn/img_convert/e9413d3cff9bdae860458bc6260daa15.png) *** ** * ** *** [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") 文档。 *** ** * ** *** ![plot](https://i-blog.csdnimg.cn/img_convert/08b19cce489e9b3088398e27111d2367.png) *** ** * ** *** ```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")。 *** ** * ** *** ![散点图示例](https://i-blog.csdnimg.cn/img_convert/1592c2123799f1561e0395f096d39661.png) *** ** * ** *** ```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")。 *** ** * ** *** ![bar](https://i-blog.csdnimg.cn/img_convert/2f906b61b19cb65d4f51102bdda9dc37.png) *** ** * ** *** ```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")。 *** ** * ** *** ![茎干图示例](https://i-blog.csdnimg.cn/img_convert/342732b8dee1781e7126b4a8b6a4c804.png) *** ** * ** *** ```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")。 *** ** * ** *** ![fill between](https://i-blog.csdnimg.cn/img_convert/35f22fb00c14355e89854c9e9982f118.png) *** ** * ** *** ```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") *** ** * ** *** ![stackplot](https://i-blog.csdnimg.cn/img_convert/8190c3106b1c0d32b0fa8bf90c92f40a.png) *** ** * ** *** ```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")方法。 *** ** * ** *** ![stairs](https://i-blog.csdnimg.cn/img_convert/fd2fcd0e97ea4577934e57e8bac2039f.png) *** ** * ** *** ```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` : *** ** * ** *** ## 统计分布 展示数据集中至少一个变量的分布情况。部分方法还会计算分布特征。 *** ** * ** *** ![直方图示例](https://i-blog.csdnimg.cn/img_convert/32e0e68a158d6783952fcb35ccf6e04d.png) *** ** * ** *** [hist(x)](hist_plot.html#sphx-glr-plot-types-stats-hist-plot-py) hist(x) ![箱线图示例](https://i-blog.csdnimg.cn/img_convert/971f5e5507f5bb07afa8c8bcf9b6879e.png) *** ** * ** *** [boxplot(X)](boxplot_plot.html#sphx-glr-plot-types-stats-boxplot-plot-py) boxplot(X) ![误差条图示例](https://i-blog.csdnimg.cn/img_convert/fe31e6cc13cb1da4efc9c808d7ca2c16.png) *** ** * ** *** [errorbar(x, y, yerr, xerr)](errorbar_plot.html#sphx-glr-plot-types-stats-errorbar-plot-py) errorbar(x, y, yerr, xerr) ![小提琴图示例](https://i-blog.csdnimg.cn/img_convert/6e0d7e1841248e3d020fb3a58a8f711a.png) *** ** * ** *** [violinplot(D)](violin.html#sphx-glr-plot-types-stats-violin-py) violinplot(D) ![事件图示例](https://i-blog.csdnimg.cn/img_convert/dadabaad554b8c557b63af398e22de06.png) *** ** * ** *** [eventplot(D)](eventplot.html#sphx-glr-plot-types-stats-eventplot-py) eventplot(D) ![二维直方图示例](https://i-blog.csdnimg.cn/img_convert/bda0f012339c7d85f3936095384c3803.png) *** ** * ** *** [hist2d(x, y)](hist2d.html#sphx-glr-plot-types-stats-hist2d-py) hist2d(x, y) ![六边形分箱图示例](https://i-blog.csdnimg.cn/img_convert/2105c0eedd94903037db703a28b3ca9c.png) *** ** * ** *** [hexbin(x, y, C)](hexbin.html#sphx-glr-plot-types-stats-hexbin-py) hexbin(x, y, C) ![饼图示例](https://i-blog.csdnimg.cn/img_convert/ced392aa9f77789cadde9e94367a99a2.png) *** ** * ** *** [pie(x)](pie.html#sphx-glr-plot-types-stats-pie-py) pie(x) ![经验累积分布图示例](https://i-blog.csdnimg.cn/img_convert/6be7a093664460d67af178ba6cdc57f6.png) *** ** * ** *** [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")。 *** ** * ** *** ![直方图示例](https://i-blog.csdnimg.cn/img_convert/0e8c5ab36935910af95fda5dd0788ed2.png) *** ** * ** *** ```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")。 *** ** * ** *** ![箱线图示例](https://i-blog.csdnimg.cn/img_convert/5c3adf29d5c57732bc9f9c9365ca275b.png) *** ** * ** *** ```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")。 *** ** * ** *** ![误差条图示](https://i-blog.csdnimg.cn/img_convert/d8fd95a48ae7cc3f317a68e681207e30.png) *** ** * ** *** ```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")。 *** ** * ** *** ![violin](https://i-blog.csdnimg.cn/img_convert/c3f2e61146abd3bee6724e61bfd9133e.png) *** ** * ** *** ```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")。 *** ** * ** *** ![eventplot](https://i-blog.csdnimg.cn/img_convert/472bb81155346207dfd0b08a45a97d8a.png) *** ** * ** *** ```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") 文档。 *** ** * ** *** ![hist2d](https://i-blog.csdnimg.cn/img_convert/66f7d94df12a753c46fb9818e40f5088.png) *** ** * ** *** ```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") 文档。 *** ** * ** *** ![hexbin](https://i-blog.csdnimg.cn/img_convert/edf8e2273237fa239bb4df5fd7e8e13c.png) *** ** * ** *** ```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") 函数文档。 *** ** * ** *** ![pie](https://i-blog.csdnimg.cn/img_convert/275baa055ebf40289c6ef52402476394.png) *** ** * ** *** ```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")。 *** ** * ** *** ![ecdf](https://i-blog.csdnimg.cn/img_convert/5e826c97d4534454e258be8766b7318a.png) *** ** * ** *** ```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})。 *** ** * ** *** ![示例图片](https://i-blog.csdnimg.cn/img_convert/2c2fec7a2ab32d836140dd921852b9a0.png) *** ** * ** *** [imshow(Z)](imshow.html#sphx-glr-plot-types-arrays-imshow-py) imshow(Z) ![示例图片](https://i-blog.csdnimg.cn/img_convert/33fe0dce489a6787aaa0eb88b4e04eb9.png) *** ** * ** *** [pcolormesh(X, Y, Z)](pcolormesh.html#sphx-glr-plot-types-arrays-pcolormesh-py) pcolormesh(X, Y, Z) ![示例图片](https://i-blog.csdnimg.cn/img_convert/0f77956351634f1441499d176849c34a.png) *** ** * ** *** [contour(X, Y, Z)](contour.html#sphx-glr-plot-types-arrays-contour-py) contour(X, Y, Z) ![示例图片](https://i-blog.csdnimg.cn/img_convert/cab6c962c2b100547496786e228c04e5.png) *** ** * ** *** [contourf(X, Y, Z)](contourf.html#sphx-glr-plot-types-arrays-contourf-py) contourf(X, Y, Z) ![示例图片](https://i-blog.csdnimg.cn/img_convert/7078434a2ddc29db0d1950fbd8ab38ad.png) *** ** * ** *** [barbs(X, Y, U, V)](barbs.html#sphx-glr-plot-types-arrays-barbs-py) barbs(X, Y, U, V) ![示例图片](https://i-blog.csdnimg.cn/img_convert/1a4ddfbb42c1e9a419b0bf63197e4866.png) *** ** * ** *** [quiver(X, Y, U, V)](quiver.html#sphx-glr-plot-types-arrays-quiver-py) quiver(X, Y, U, V) ![示例图片](https://i-blog.csdnimg.cn/img_convert/874addb1ff89ed9eae491cda60fb0868.png) *** ** * ** *** [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")。 *** ** * ** *** ![imshow](https://i-blog.csdnimg.cn/img_convert/72408c9da70573294c702c5bacc5f989.png) *** ** * ** *** ```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向量不需要等距分布(实际上可以是倾斜的)。 *** ** * ** *** ![pcolormesh](https://i-blog.csdnimg.cn/img_convert/1b1b314357af9a40429e287f8a1b5bcc.png) *** ** * ** *** ```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") 文档。 *** ** * ** *** ![contour](https://i-blog.csdnimg.cn/img_convert/1fcb2ed7483097e5024b32eee3114518.png) *** ** * ** *** ```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") 函数文档。 *** ** * ** *** ![contourf](https://i-blog.csdnimg.cn/img_convert/b49062a8df514cd77f42598f69b3079c.png) *** ** * ** *** ```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")。 *** ** * ** *** ![风羽图示例](https://i-blog.csdnimg.cn/img_convert/cbd1922c9f5aa6c1e7d2214a59ab3aad.png) *** ** * ** *** ```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") 函数文档。 *** ** * ** *** ![箭头场示意图](https://i-blog.csdnimg.cn/img_convert/d0b4fa9cf5139770be15e8fa28c7458e.png) *** ** * ** *** ```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")。 *** ** * ** *** ![streamplot](https://i-blog.csdnimg.cn/img_convert/94ed376f1a0d2f783eb7a79ab195942c.png) *** ** * ** *** ```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)的图表。 *** ** * ** *** ![非规则网格等值线图示例](https://i-blog.csdnimg.cn/img_convert/a8d1d1aedfbb82f480d13de4715b2078.png) *** ** * ** *** [tricontour(x, y, z)](tricontour.html#sphx-glr-plot-types-unstructured-tricontour-py) tricontour(x, y, z) ![非规则网格填充等值线图示例](https://i-blog.csdnimg.cn/img_convert/08dbce61a8c83b590202a065ff51191a.png) *** ** * ** *** [tricontourf(x, y, z)](tricontourf.html#sphx-glr-plot-types-unstructured-tricontourf-py) tricontourf(x, y, z) ![非规则网格颜色映射图示例](https://i-blog.csdnimg.cn/img_convert/1cecab728860cd980f8d1c4608509ddc.png) *** ** * ** *** [tripcolor(x, y, z)](tripcolor.html#sphx-glr-plot-types-unstructured-tripcolor-py) tripcolor(x, y, z) ![非规则网格三角剖分图示例](https://i-blog.csdnimg.cn/img_convert/d4a195e4f23afd2faad839efebffaef8.png) *** ** * ** *** [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")。 *** ** * ** *** ![tricontour](https://i-blog.csdnimg.cn/img_convert/9e772478184b556afa6841d2bb90dd5b.png) *** ** * ** *** ```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")。 *** ** * ** *** ![tricontourf](https://i-blog.csdnimg.cn/img_convert/626753f19d5b5db3488d6c2a405a0052.png) *** ** * ** *** ```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")。 *** ** * ** *** ![tripcolor](https://i-blog.csdnimg.cn/img_convert/e41ef0981b0b96b6db79737fc874c3e8.png) *** ** * ** *** ```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")。 *** ** * ** *** ![三角网格图](https://i-blog.csdnimg.cn/img_convert/a148e465f0a742a166e31abc0b4ca639.png) *** ** * ** *** ```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})。 *** ** * ** *** ![三维柱状图示例](https://i-blog.csdnimg.cn/img_convert/b82d84d58a1d9ef83b3fe361157978f0.png) *** ** * ** *** [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) *** ** * ** *** ![三维填充图示例](https://i-blog.csdnimg.cn/img_convert/abf1693052d04127a16704ee30d99e77.png) *** ** * ** *** [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) *** ** * ** *** ![三维线图示例](https://i-blog.csdnimg.cn/img_convert/f228e7012af7aecba1e6c21eede9118e.png) *** ** * ** *** [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) *** ** * ** *** ![三维箭头图示例](https://i-blog.csdnimg.cn/img_convert/d18557165a87e1fd2d57000bee83876e.png) *** ** * ** *** [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) *** ** * ** *** ![三维散点图示例](https://i-blog.csdnimg.cn/img_convert/23e373780950a33a6b7effe29e035635.png) *** ** * ** *** [scatter(xs, ys, zs)](scatter3d_simple.html#sphx-glr-plot-types-3d-scatter3d-simple-py) scatter(xs, ys, zs) *** ** * ** *** ![三维杆状图示例](https://i-blog.csdnimg.cn/img_convert/4d08742fcef37b850b28996a7f8ee4db.png) *** ** * ** *** [stem(x, y, z)](stem3d.html#sphx-glr-plot-types-3d-stem3d-py) stem(x, y, z) *** ** * ** *** ![三维曲面图示例](https://i-blog.csdnimg.cn/img_convert/6843dc369bf4a8591f9ed077dc4dffd4.png) *** ** * ** *** [plot_surface(X, Y, Z)](surface3d_simple.html#sphx-glr-plot-types-3d-surface3d-simple-py) plot_surface(X, Y, Z) *** ** * ** *** ![三维三角曲面图示例](https://i-blog.csdnimg.cn/img_convert/1e222d2ee03131e58c22c51b37d47485.png) *** ** * ** *** [plot_trisurf(x, y, z)](trisurf3d_simple.html#sphx-glr-plot-types-3d-trisurf3d-simple-py) plot_trisurf(x, y, z) *** ** * ** *** ![三维体素图示例](https://i-blog.csdnimg.cn/img_convert/bd9dae0792d989c30ecd740289be226c.png) *** ** * ** *** \[voxels(\[x, y, z\], filled)\](voxels_simple.html#sphx-glr-plot-types-3d-voxels-simple-py) voxels(\[x, y, z\], filled) *** ** * ** *** ![三维线框图示例](https://i-blog.csdnimg.cn/img_convert/180ea60289ad32f6d2f9765aa1fd1820.png) *** ** * ** *** [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")。 *** ** * ** *** ![bar3d简单示例](https://i-blog.csdnimg.cn/img_convert/e6c81e5eb47a5e4c2bee08d66546b102.png) *** ** * ** *** ```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")。 *** ** * ** *** ![fill between3d simple](https://i-blog.csdnimg.cn/img_convert/ab5e5f51effc2dbdf600a54413feedee.png) *** ** * ** *** ```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")。 *** ** * ** *** ![plot3d简单示例](https://i-blog.csdnimg.cn/img_convert/ae1cd2e30efa41cb0a72a2421f6f62de.png) *** ** * ** *** ```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") 文档。 *** ** * ** *** ![quiver3d简单示例](https://i-blog.csdnimg.cn/img_convert/0be822aac0535de55fcb9b3db45c1583.png) *** ** * ** *** ```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") 文档。 *** ** * ** *** ![scatter3d简单示例](https://i-blog.csdnimg.cn/img_convert/0a8a0380fce7580a3e58517895337275.png) *** ** * ** *** ```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")。 *** ** * ** *** ![stem3d](https://i-blog.csdnimg.cn/img_convert/9fa06b309bf1e34bdcc75554b912fa3e.png) *** ** * ** *** ```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")。 *** ** * ** *** ![surface3d简单示例](https://i-blog.csdnimg.cn/img_convert/bf2ba4db28289a7f17d3ab024ba5108e.png) *** ** * ** *** ```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")。 *** ** * ** *** ![trisurf3d简单示例](https://i-blog.csdnimg.cn/img_convert/1ff62bf338d6995f4b460c7c52637722.png) *** ** * ** *** ```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") 文档。 *** ** * ** *** ![voxels simple](https://i-blog.csdnimg.cn/img_convert/5eafe68ad37c907a008f6b6616b9a5af.png) *** ** * ** *** ```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")。 *** ** * ** *** ![wire3d简单示例](https://i-blog.csdnimg.cn/img_convert/cdd71a81b3c5fa129c563e1cffafdf9f.png) *** ** * ** *** ```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(三)

相关推荐
胡耀超7 小时前
7、Matplotlib、Seaborn、Plotly数据可视化与探索性分析(探索性数据分析(EDA)方法论)
python·信息可视化·plotly·数据挖掘·数据分析·matplotlib·seaborn
咔咔一顿操作7 小时前
【CSS 3D 交互】打造沉浸式 3D 照片墙:结合 JS 实现拖拽交互
前端·javascript·css·3d·交互·css3
摩羯座-1856903059417 小时前
Python数据可视化基础:使用Matplotlib绘制图表
大数据·python·信息可视化·matplotlib
咔咔一顿操作20 小时前
【CSS 3D 实战】从零实现旋转立方体:理解 3D 空间的核心原理
前端·css·3d·css3
研梦非凡20 小时前
CVPR 2025|基于视觉语言模型的零样本3D视觉定位
人工智能·深度学习·计算机视觉·3d·ai·语言模型·自然语言处理
咔咔一顿操作1 天前
第七章 Cesium 3D 粒子烟花效果案例解析:从原理到完整代码
人工智能·3d·信息可视化·cesium
多恩Stone1 天前
【3DV 进阶-2】Hunyuan3D2.1 训练代码详细理解下-数据读取流程
人工智能·python·算法·3d·aigc
心一信息1 天前
让 3D 动画在浏览器中“活”起来!
3d
云飞云共享云桌面1 天前
工厂办公环境如何实现一台服务器多人共享办公
运维·服务器·网络·数据库·3d