Trigradient 演示

使用 matplotlib.tri.CubicTriInterpolator 演示梯度计算。

  1. from matplotlib.tri import (
  2. Triangulation, UniformTriRefiner, CubicTriInterpolator)
  3. import matplotlib.pyplot as plt
  4. import matplotlib.cm as cm
  5. import numpy as np
  6. #-----------------------------------------------------------------------------
  7. # Electrical potential of a dipole
  8. #-----------------------------------------------------------------------------
  9. def dipole_potential(x, y):
  10. """ The electric dipole potential V """
  11. r_sq = x**2 + y**2
  12. theta = np.arctan2(y, x)
  13. z = np.cos(theta)/r_sq
  14. return (np.max(z) - z) / (np.max(z) - np.min(z))
  15. #-----------------------------------------------------------------------------
  16. # Creating a Triangulation
  17. #-----------------------------------------------------------------------------
  18. # First create the x and y coordinates of the points.
  19. n_angles = 30
  20. n_radii = 10
  21. min_radius = 0.2
  22. radii = np.linspace(min_radius, 0.95, n_radii)
  23. angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
  24. angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
  25. angles[:, 1::2] += np.pi / n_angles
  26. x = (radii*np.cos(angles)).flatten()
  27. y = (radii*np.sin(angles)).flatten()
  28. V = dipole_potential(x, y)
  29. # Create the Triangulation; no triangles specified so Delaunay triangulation
  30. # created.
  31. triang = Triangulation(x, y)
  32. # Mask off unwanted triangles.
  33. triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
  34. y[triang.triangles].mean(axis=1))
  35. < min_radius)
  36. #-----------------------------------------------------------------------------
  37. # Refine data - interpolates the electrical potential V
  38. #-----------------------------------------------------------------------------
  39. refiner = UniformTriRefiner(triang)
  40. tri_refi, z_test_refi = refiner.refine_field(V, subdiv=3)
  41. #-----------------------------------------------------------------------------
  42. # Computes the electrical field (Ex, Ey) as gradient of electrical potential
  43. #-----------------------------------------------------------------------------
  44. tci = CubicTriInterpolator(triang, -V)
  45. # Gradient requested here at the mesh nodes but could be anywhere else:
  46. (Ex, Ey) = tci.gradient(triang.x, triang.y)
  47. E_norm = np.sqrt(Ex**2 + Ey**2)
  48. #-----------------------------------------------------------------------------
  49. # Plot the triangulation, the potential iso-contours and the vector field
  50. #-----------------------------------------------------------------------------
  51. fig, ax = plt.subplots()
  52. ax.set_aspect('equal')
  53. # Enforce the margins, and enlarge them to give room for the vectors.
  54. ax.use_sticky_edges = False
  55. ax.margins(0.07)
  56. ax.triplot(triang, color='0.8')
  57. levels = np.arange(0., 1., 0.01)
  58. cmap = cm.get_cmap(name='hot', lut=None)
  59. ax.tricontour(tri_refi, z_test_refi, levels=levels, cmap=cmap,
  60. linewidths=[2.0, 1.0, 1.0, 1.0])
  61. # Plots direction of the electrical vector field
  62. ax.quiver(triang.x, triang.y, Ex/E_norm, Ey/E_norm,
  63. units='xy', scale=10., zorder=3, color='blue',
  64. width=0.007, headwidth=3., headlength=4.)
  65. ax.set_title('Gradient plot: an electrical dipole')
  66. plt.show()

Trigradient 演示

参考

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

  1. import matplotlib
  2. matplotlib.axes.Axes.tricontour
  3. matplotlib.pyplot.tricontour
  4. matplotlib.axes.Axes.triplot
  5. matplotlib.pyplot.triplot
  6. matplotlib.tri
  7. matplotlib.tri.Triangulation
  8. matplotlib.tri.CubicTriInterpolator
  9. matplotlib.tri.CubicTriInterpolator.gradient
  10. matplotlib.tri.UniformTriRefiner
  11. matplotlib.axes.Axes.quiver
  12. matplotlib.pyplot.quiver

下载这个示例