当前位置: 首页 > 编程笔记 >

python+matplotlib演示电偶极子实例代码

东门秦迟
2023-03-14
本文向大家介绍python+matplotlib演示电偶极子实例代码,包括了python+matplotlib演示电偶极子实例代码的使用技巧和注意事项,需要的朋友参考一下

使用matplotlib.tri.CubicTriInterpolator.演示变化率计算:

完整实例:

from matplotlib.tri import (
  Triangulation, UniformTriRefiner, CubicTriInterpolator)
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np


#-----------------------------------------------------------------------------
# Electrical potential of a dipole
#-----------------------------------------------------------------------------
def dipole_potential(x, y):
  """ The electric dipole potential V """
  r_sq = x**2 + y**2
  theta = np.arctan2(y, x)
  z = np.cos(theta)/r_sq
  return (np.max(z) - z) / (np.max(z) - np.min(z))


#-----------------------------------------------------------------------------
# Creating a Triangulation
#-----------------------------------------------------------------------------
# First create the x and y coordinates of the points.
n_angles = 30
n_radii = 10
min_radius = 0.2
radii = np.linspace(min_radius, 0.95, n_radii)

angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] += np.pi / n_angles

x = (radii*np.cos(angles)).flatten()
y = (radii*np.sin(angles)).flatten()
V = dipole_potential(x, y)

# Create the Triangulation; no triangles specified so Delaunay triangulation
# created.
triang = Triangulation(x, y)

# Mask off unwanted triangles.
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
             y[triang.triangles].mean(axis=1))
        < min_radius)

#-----------------------------------------------------------------------------
# Refine data - interpolates the electrical potential V
#-----------------------------------------------------------------------------
refiner = UniformTriRefiner(triang)
tri_refi, z_test_refi = refiner.refine_field(V, subdiv=3)

#-----------------------------------------------------------------------------
# Computes the electrical field (Ex, Ey) as gradient of electrical potential
#-----------------------------------------------------------------------------
tci = CubicTriInterpolator(triang, -V)
# Gradient requested here at the mesh nodes but could be anywhere else:
(Ex, Ey) = tci.gradient(triang.x, triang.y)
E_norm = np.sqrt(Ex**2 + Ey**2)

#-----------------------------------------------------------------------------
# Plot the triangulation, the potential iso-contours and the vector field
#-----------------------------------------------------------------------------
fig, ax = plt.subplots()
ax.set_aspect('equal')
# Enforce the margins, and enlarge them to give room for the vectors.
ax.use_sticky_edges = False
ax.margins(0.07)

ax.triplot(triang, color='0.8')

levels = np.arange(0., 1., 0.01)
cmap = cm.get_cmap(name='hot', lut=None)
ax.tricontour(tri_refi, z_test_refi, levels=levels, cmap=cmap,
       linewidths=[2.0, 1.0, 1.0, 1.0])
# Plots direction of the electrical vector field
ax.quiver(triang.x, triang.y, Ex/E_norm, Ey/E_norm,
     units='xy', scale=10., zorder=3, color='blue',
     width=0.007, headwidth=3., headlength=4.)

ax.set_title('Gradient plot: an electrical dipole')
plt.show()

总结

以上就是本文关于python+matplotlib演示电偶极子实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

 类似资料:
  • 学习目标: 通过一个简单的AJAX调用实例,了解DoitPHP框架在数据交互的具体操作方式。 创建文件: 1、新建Controller文件为:IndexController, 文件路径为:application/controllers/IndexController.php, 内容如下: /** * DoitPHP 演示实例三 * * @author tommy * @copyright

  • 学习目标: 通过这个简单的实例,更好地了解布局视图(layout)、挂件(widget)的运用,更深刻地理解DoitPHP的视图机制。 创建文件: 1、创建IndexController(建议使用DoitPHP Tools),文件路径:application/controllers/IndexController.php。内容如下: /** * DoitPHP 演示实例二 * * @auth

  • 学习目标: 通过一个简单完整的MVC实例,了解控制器文件(Controller)、模型文件(Model)、视图文件(View)的所在的目录路径。更好地理解DoitPHP框架的开发规则。 创建数据表: DROP TABLE IF EXISTS `users`; CREATE TABLE `users` ( `user_id` mediumint(8) unsigned NOT NULL AUTO

  • 本文向大家介绍matplotlib设置legend图例代码示例,包括了matplotlib设置legend图例代码示例的使用技巧和注意事项,需要的朋友参考一下 本文主要是关于matplotlib的一些基本用法。 Demo 结果展示: 总结 以上就是本文关于matplotlib设置legend图例代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言

  • 本文向大家介绍python+matplotlib绘制旋转椭圆实例代码,包括了python+matplotlib绘制旋转椭圆实例代码的使用技巧和注意事项,需要的朋友参考一下 旋转椭圆 实例代码: 总结 以上就是本文关于python+matplotlib绘制旋转椭圆实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

  • 本文向大家介绍python+matplotlib绘制饼图散点图实例代码,包括了python+matplotlib绘制饼图散点图实例代码的使用技巧和注意事项,需要的朋友参考一下 本文是从matplotlib官网上摘录下来的一个实例,实现的功能是Python+matplotlib绘制自定义饼图作为散点图的标记,具体如下。 首先看下演示效果 实例代码: 总结 以上就是本文关于python+matplot