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

 更新时间:2018年01月12日 10:38:51   投稿:mengwei  
这篇文章主要介绍了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演示电偶极子实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

  • Python BeautifulReport可视化报告代码实例

    Python BeautifulReport可视化报告代码实例

    这篇文章主要介绍了Python BeautifulReport可视化报告代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • python人物视频背景替换实现虚拟空间穿梭

    python人物视频背景替换实现虚拟空间穿梭

    这篇文章主要为大家介绍了python实现人物视频背景替换示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • 在Python程序员面试中被问的最多的10道题

    在Python程序员面试中被问的最多的10道题

    本篇文章我们为大家整理了在Python程序员面试中被问的最多的10道题,我们还给出了最简便的解决办法,一起学习下。
    2017-12-12
  • 纯用NumPy实现神经网络的示例代码

    纯用NumPy实现神经网络的示例代码

    这篇文章主要介绍了纯用NumPy实现神经网络的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-10-10
  • Python selenium文件上传方法汇总

    Python selenium文件上传方法汇总

    这篇文章主要为大家详细介绍了Python selenium文件上传方法,selenium文件上传的所有方法进行整理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • Python使用正则表达式获取网页中所需要的信息

    Python使用正则表达式获取网页中所需要的信息

    这篇文章主要介绍了Python使用正则获取网页中所需要的信息的相关资料,需要的朋友可以参考下
    2018-01-01
  • Python实现合并带有嵌入图片的单元格

    Python实现合并带有嵌入图片的单元格

    这篇文章主要为大家详细介绍了如何使用Python实现合并带有嵌入图片的单元格,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-12-12
  • Python 批量操作设备的实现步骤

    Python 批量操作设备的实现步骤

    本文将结合实例代码,介绍Python 批量操作设备的实现步骤,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
    2021-07-07
  • Django框架封装外部函数示例

    Django框架封装外部函数示例

    这篇文章主要介绍了Django框架封装外部函数,结合Django框架表单登陆功能示例分析了封装外部函数的相关操作步骤与实现技巧,需要的朋友可以参考下
    2019-05-05
  • python 列表删除所有指定元素的方法

    python 列表删除所有指定元素的方法

    下面小编就为大家分享一篇python 列表删除所有指定元素的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04

最新评论