Tripcolor Demo

非结构化三角形网格的伪彩色图。

  1. import matplotlib.pyplot as plt
  2. import matplotlib.tri as tri
  3. import numpy as np

在不指定三角形的情况下创建三角剖分会导致点的Delaunay三角剖分。

  1. # First create the x and y coordinates of the points.
  2. n_angles = 36
  3. n_radii = 8
  4. min_radius = 0.25
  5. radii = np.linspace(min_radius, 0.95, n_radii)
  6. angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
  7. angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
  8. angles[:, 1::2] += np.pi / n_angles
  9. x = (radii * np.cos(angles)).flatten()
  10. y = (radii * np.sin(angles)).flatten()
  11. z = (np.cos(radii) * np.cos(3 * angles)).flatten()
  12. # Create the Triangulation; no triangles so Delaunay triangulation created.
  13. triang = tri.Triangulation(x, y)
  14. # Mask off unwanted triangles.
  15. triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
  16. y[triang.triangles].mean(axis=1))
  17. < min_radius)

tripcolor plot.

  1. fig1, ax1 = plt.subplots()
  2. ax1.set_aspect('equal')
  3. tpc = ax1.tripcolor(triang, z, shading='flat')
  4. fig1.colorbar(tpc)
  5. ax1.set_title('tripcolor of Delaunay triangulation, flat shading')

Tripcolor 演示

说明Gouraud阴影。

  1. fig2, ax2 = plt.subplots()
  2. ax2.set_aspect('equal')
  3. tpc = ax2.tripcolor(triang, z, shading='gouraud')
  4. fig2.colorbar(tpc)
  5. ax2.set_title('tripcolor of Delaunay triangulation, gouraud shading')

Tripcolor 演示2

您可以指定自己的三角剖分而不是执行点的Delaunay三角剖分,其中每个三角形由构成三角形的三个点的索引给出,以顺时针或逆时针方式排序。

  1. xy = np.asarray([
  2. [-0.101, 0.872], [-0.080, 0.883], [-0.069, 0.888], [-0.054, 0.890],
  3. [-0.045, 0.897], [-0.057, 0.895], [-0.073, 0.900], [-0.087, 0.898],
  4. [-0.090, 0.904], [-0.069, 0.907], [-0.069, 0.921], [-0.080, 0.919],
  5. [-0.073, 0.928], [-0.052, 0.930], [-0.048, 0.942], [-0.062, 0.949],
  6. [-0.054, 0.958], [-0.069, 0.954], [-0.087, 0.952], [-0.087, 0.959],
  7. [-0.080, 0.966], [-0.085, 0.973], [-0.087, 0.965], [-0.097, 0.965],
  8. [-0.097, 0.975], [-0.092, 0.984], [-0.101, 0.980], [-0.108, 0.980],
  9. [-0.104, 0.987], [-0.102, 0.993], [-0.115, 1.001], [-0.099, 0.996],
  10. [-0.101, 1.007], [-0.090, 1.010], [-0.087, 1.021], [-0.069, 1.021],
  11. [-0.052, 1.022], [-0.052, 1.017], [-0.069, 1.010], [-0.064, 1.005],
  12. [-0.048, 1.005], [-0.031, 1.005], [-0.031, 0.996], [-0.040, 0.987],
  13. [-0.045, 0.980], [-0.052, 0.975], [-0.040, 0.973], [-0.026, 0.968],
  14. [-0.020, 0.954], [-0.006, 0.947], [ 0.003, 0.935], [ 0.006, 0.926],
  15. [ 0.005, 0.921], [ 0.022, 0.923], [ 0.033, 0.912], [ 0.029, 0.905],
  16. [ 0.017, 0.900], [ 0.012, 0.895], [ 0.027, 0.893], [ 0.019, 0.886],
  17. [ 0.001, 0.883], [-0.012, 0.884], [-0.029, 0.883], [-0.038, 0.879],
  18. [-0.057, 0.881], [-0.062, 0.876], [-0.078, 0.876], [-0.087, 0.872],
  19. [-0.030, 0.907], [-0.007, 0.905], [-0.057, 0.916], [-0.025, 0.933],
  20. [-0.077, 0.990], [-0.059, 0.993]])
  21. x, y = np.rad2deg(xy).T
  22. triangles = np.asarray([
  23. [67, 66, 1], [65, 2, 66], [ 1, 66, 2], [64, 2, 65], [63, 3, 64],
  24. [60, 59, 57], [ 2, 64, 3], [ 3, 63, 4], [ 0, 67, 1], [62, 4, 63],
  25. [57, 59, 56], [59, 58, 56], [61, 60, 69], [57, 69, 60], [ 4, 62, 68],
  26. [ 6, 5, 9], [61, 68, 62], [69, 68, 61], [ 9, 5, 70], [ 6, 8, 7],
  27. [ 4, 70, 5], [ 8, 6, 9], [56, 69, 57], [69, 56, 52], [70, 10, 9],
  28. [54, 53, 55], [56, 55, 53], [68, 70, 4], [52, 56, 53], [11, 10, 12],
  29. [69, 71, 68], [68, 13, 70], [10, 70, 13], [51, 50, 52], [13, 68, 71],
  30. [52, 71, 69], [12, 10, 13], [71, 52, 50], [71, 14, 13], [50, 49, 71],
  31. [49, 48, 71], [14, 16, 15], [14, 71, 48], [17, 19, 18], [17, 20, 19],
  32. [48, 16, 14], [48, 47, 16], [47, 46, 16], [16, 46, 45], [23, 22, 24],
  33. [21, 24, 22], [17, 16, 45], [20, 17, 45], [21, 25, 24], [27, 26, 28],
  34. [20, 72, 21], [25, 21, 72], [45, 72, 20], [25, 28, 26], [44, 73, 45],
  35. [72, 45, 73], [28, 25, 29], [29, 25, 31], [43, 73, 44], [73, 43, 40],
  36. [72, 73, 39], [72, 31, 25], [42, 40, 43], [31, 30, 29], [39, 73, 40],
  37. [42, 41, 40], [72, 33, 31], [32, 31, 33], [39, 38, 72], [33, 72, 38],
  38. [33, 38, 34], [37, 35, 38], [34, 38, 35], [35, 37, 36]])
  39. xmid = x[triangles].mean(axis=1)
  40. ymid = y[triangles].mean(axis=1)
  41. x0 = -5
  42. y0 = 52
  43. zfaces = np.exp(-0.01 * ((xmid - x0) * (xmid - x0) +
  44. (ymid - y0) * (ymid - y0)))

而不是创建Triangulation对象,可以直接将x,y和三角形数组传递给tripcolor。 如果要多次使用相同的三角测量来保存重复计算,最好使用Triangulation对象。 通过使用facecolors kwarg,可以为每个面指定一个颜色值,而不是每个点指定一个颜色值。

  1. fig3, ax3 = plt.subplots()
  2. ax3.set_aspect('equal')
  3. tpc = ax3.tripcolor(x, y, triangles, facecolors=zfaces, edgecolors='k')
  4. fig3.colorbar(tpc)
  5. ax3.set_title('tripcolor of user-specified triangulation')
  6. ax3.set_xlabel('Longitude (degrees)')
  7. ax3.set_ylabel('Latitude (degrees)')
  8. plt.show()

Tripcolor 演示3

参考

此示例中显示了以下函数,方法,类和模块的使用:

  1. import matplotlib
  2. matplotlib.axes.Axes.tripcolor
  3. matplotlib.pyplot.tripcolor
  4. matplotlib.tri
  5. matplotlib.tri.Triangulation

下载这个示例