官术网_书友最值得收藏!

Plotting triangulations

Triangulations arise when dealing with spatial locations. Apart from showing distances between points and neighborhood relationships, triangulation plots can be a convenient way to represent maps. matplotlib provides a fair amount of support for triangulations.

How to do it...

As in the preceding examples, the following few lines of code are enough:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as tri

data = np.random.rand(100, 2)

triangles = tri.Triangulation(data[:,0], data[:,1])

plt.triplot(triangles)
plt.show()

Every time the script is run, you will see a different triangulation as the cloud of points that is triangulated is generated randomly.

The preceding script displays the following graph:

How to do it...

How it works...

We import the matplotlib.tri module, which provides helper functions to compute triangulations from points. In this example, for demonstration purpose, we generate a random cloud of points using the following code:

data = np.random.rand(100, 2)

We compute a triangulation and store it in the triangles' variable with the help of the following code:

triangles = tri.Triangulation(data[:,0], data[:,1])

The pyplot.triplot() function simply takes triangles as inputs and displays the triangulation result.

主站蜘蛛池模板: 惠安县| 金门县| 剑阁县| 宜章县| 屏东市| 榆树市| 屏山县| 怀安县| 淮南市| 襄樊市| 西平县| 井研县| 大厂| 鞍山市| 高密市| 当阳市| 布拖县| 卢氏县| 云林县| 莲花县| 丹巴县| 德江县| 全州县| 成武县| 肥东县| 桓仁| 营山县| 铜陵市| 凤翔县| 连云港市| 方正县| 阿图什市| 湛江市| 昭觉县| 黎城县| 金沙县| 宝应县| 论坛| 察雅县| 阿拉善盟| 伊吾县|