使用集合进行 Matplotlib 可视化

Beginner

This tutorial is from open-source community. Access the source code

简介

本教程将指导你使用 Matplotlib 集合创建可视化。本教程将向你展示如何使用 LineCollection、PolyCollection 和 RegularPolyCollection。本教程还将向你展示如何使用 LineCollection 和 PolyCollection 的 offsets 和 offset_transform 关键字参数来设置螺旋线的位置。本教程还将向你展示如何使用 RegularPolyCollection 来制作正多边形。

虚拟机使用提示

虚拟机启动完成后,点击左上角切换到“笔记本”标签,以访问 Jupyter Notebook 进行练习。

有时,你可能需要等待几秒钟让 Jupyter Notebook 完成加载。由于 Jupyter Notebook 的限制,操作验证无法自动化。

如果你在学习过程中遇到问题,随时向 Labby 提问。课程结束后提供反馈,我们将立即为你解决问题。

导入必要的库

import matplotlib.pyplot as plt
import numpy as np

from matplotlib import collections, transforms

第一步是导入必要的库。在本教程中,我们将使用 Matplotlib 和 Numpy。

创建螺旋线

nverts = 50
npts = 100

## 制作一些螺旋线
r = np.arange(nverts)
theta = np.linspace(0, 2*np.pi, nverts)
xx = r * np.sin(theta)
yy = r * np.cos(theta)
spiral = np.column_stack([xx, yy])

下一步是使用 Numpy 创建螺旋线。我们将使用正弦和余弦函数来创建螺旋线。

创建偏移量

## 为保证可重复性而固定随机状态
rs = np.random.RandomState(19680801)

## 生成一些偏移量
xyo = rs.randn(npts, 2)

第三步是使用 Numpy 创建偏移量。我们将使用随机函数来创建偏移量。

使用偏移量创建 LineCollection

col = collections.LineCollection(
    [spiral], offsets=xyo, offset_transform=ax1.transData)
trans = fig.dpi_scale_trans + transforms.Affine2D().scale(1.0/72.0)
col.set_transform(trans)
col.set_color(colors)

ax1.add_collection(col, autolim=True)
ax1.autoscale_view()

ax1.set_title('LineCollection using offsets')

第四步是使用偏移量创建一个 LineCollection。我们将使用 LineCollection 来创建带有偏移量的曲线。我们还将使用 offset_transform 来设置曲线的位置。

使用偏移量创建 PolyCollection

col = collections.PolyCollection(
    [spiral], offsets=xyo, offset_transform=ax2.transData)
trans = transforms.Affine2D().scale(fig.dpi/72.0)
col.set_transform(trans)
col.set_color(colors)

ax2.add_collection(col, autolim=True)
ax2.autoscale_view()

ax2.set_title('PolyCollection using offsets')

第五步是使用偏移量创建一个 PolyCollection。我们将使用 PolyCollection 为曲线填充颜色。我们还将使用 offset_transform 来设置曲线的位置。

使用偏移量创建 RegularPolyCollection

col = collections.RegularPolyCollection(
    7, sizes=np.abs(xx) * 10.0, offsets=xyo, offset_transform=ax3.transData)
trans = transforms.Affine2D().scale(fig.dpi / 72.0)
col.set_transform(trans)
col.set_color(colors)

ax3.add_collection(col, autolim=True)
ax3.autoscale_view()

ax3.set_title('RegularPolyCollection using offsets')

第六步是使用偏移量创建一个 RegularPolyCollection。我们将使用 RegularPolyCollection 来创建带有偏移量的正多边形。我们还将使用 offset_transform 来设置多边形的位置。

创建连续的数据偏移量

## 依次模拟一系列海洋流剖面,
## 每次偏移 0.1 米/秒,以便它们形成有时被称为
## “瀑布”图或“交错”图的图形。

nverts = 60
ncurves = 20
offs = (0.1, 0.0)

yy = np.linspace(0, 2*np.pi, nverts)
ym = np.max(yy)
xx = (0.2 + (ym - yy) / ym) ** 2 * np.cos(yy - 0.4) * 0.5
segs = []
for i in range(ncurves):
    xxx = xx + 0.02*rs.randn(nverts)
    curve = np.column_stack([xxx, yy * 100])
    segs.append(curve)

col = collections.LineCollection(segs, offsets=offs)
ax4.add_collection(col, autolim=True)
col.set_color(colors)
ax4.autoscale_view()

ax4.set_title('Successive data offsets')
ax4.set_xlabel('Zonal velocity component (m/s)')
ax4.set_ylabel('Depth (m)')
ax4.set_ylim(ax4.get_ylim()[::-1])

第七步是创建连续的数据偏移量。我们将使用 LineCollection 来创建具有连续偏移量的曲线。

总结

本教程向你展示了如何使用 Matplotlib 集合来创建可视化效果。你已经学习了如何使用 LineCollection、PolyCollection 和 RegularPolyCollection 来创建曲线和多边形。你还学习了如何使用 LineCollection 和 PolyCollection 的 offsets 和 offset_transform 关键字参数来设置螺旋线的位置。