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

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.

主站蜘蛛池模板: 赣榆县| 土默特右旗| 定远县| 罗源县| 三门县| 翁源县| 磐安县| 香港| 班戈县| 长顺县| 上林县| 社旗县| 宁安市| 游戏| 赤水市| 江山市| 昌宁县| 天长市| 白银市| 黄龙县| 开封市| 五峰| 枞阳县| 博客| 深圳市| 德庆县| 四子王旗| 延庆县| 黄梅县| 攀枝花市| 卢湾区| 松阳县| 蕲春县| 玉环县| 西丰县| 台前县| 石门县| 平凉市| 任丘市| 社会| 台南县|