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

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.

主站蜘蛛池模板: 铁岭市| 芜湖市| 澜沧| 都江堰市| 静宁县| 城步| 汤原县| 胶南市| 湖州市| 偃师市| 光泽县| 新昌县| 唐山市| 鲜城| 青铜峡市| 花莲市| 白朗县| 兴义市| 西峡县| 濉溪县| 南丹县| 巨野县| 萨迦县| 弋阳县| 襄汾县| 同仁县| 莱芜市| 新乐市| 巴塘县| 浮山县| 云南省| 绥化市| 霍邱县| 宜川县| 通州市| 丽江市| 承德县| 茶陵县| 安化县| 衡水市| 宜兰县|