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入门篇之文件

    Python入门篇之文件

    文件是我们储存信息的地方,我们经常要对文件进行读、写、删除等的操作,在Python中,我们可用Python提供的函数和方法方便地操作文件。文件可以通过调用open或file来打开,open通常比file更通用,因为file几乎都是为面向对象程序设计量身打造
    2014-10-10
  • 用python如何绘制表格不同颜色的excel

    用python如何绘制表格不同颜色的excel

    做数据分析的时候,用到了对Excel中的数据进行显示处理,能更直观的了解数据,所以下面这篇文章主要给大家介绍了关于利用python如何绘制表格不同颜色excel的相关资料,需要的朋友可以参考下
    2021-11-11
  • python flask开发的简单基金查询工具

    python flask开发的简单基金查询工具

    基于python flask开发的简单基金查询工具,支持大盘指数实时情况查看,总持仓实际涨幅、预估涨幅等功能,感兴趣的朋友可以下载该项目来查看使用
    2021-06-06
  • 如何利用Pandas查询选取数据

    如何利用Pandas查询选取数据

    在数据分析的过程中通常要对数据进行清洗与处理,而其中比较重要和常见的操作就有对数据进行筛选与查询,下面这篇文章主要给大家介绍了关于如何利用Pandas查询选取数据的相关资料,需要的朋友可以参考下
    2022-07-07
  • 在python中实现导入一个需要传参的模块

    在python中实现导入一个需要传参的模块

    这篇文章主要介绍了在python中实现导入一个需要传参的模块,具有很好的参考价值,希望可以给大家一个参考,以后在遇到这种的情况的时候,知道如何应对
    2021-05-05
  • python中的decode()与encode()深入理解

    python中的decode()与encode()深入理解

    这篇文章主要介绍了python中的decode()与encode()函数详解,本文通过实例代码给大家讲解的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-12-12
  • Python使用Matplotlib实现雨点图动画效果的方法

    Python使用Matplotlib实现雨点图动画效果的方法

    这篇文章主要介绍了Python使用Matplotlib实现雨点图动画效果的方法,结合实例形式分析了win10安装ffmpeg及animation函数的使用相关操作技巧,需要的朋友可以参考下
    2017-12-12
  • Python从入门到精通之多线程使用详解

    Python从入门到精通之多线程使用详解

    这篇文章主要介绍了Python中的多线程使用,包括创建线程、线程同步、线程间通信以及线程池等基本概念和技巧,文中的示例代码讲解详细,需要的可以参考一下
    2023-07-07
  • 解决Pycharm双击图标启动不了的问题(JetBrains全家桶通用)

    解决Pycharm双击图标启动不了的问题(JetBrains全家桶通用)

    这篇文章主要介绍了Pycharm双击图标启动不了(JetBrains全家桶通用),本文给大家分享问题及解决方法,需要的朋友可以参考下
    2020-08-08
  • Python实现批量修改指定目录下图片的大小

    Python实现批量修改指定目录下图片的大小

    批量修改指定目录下图片大小通常是在需要对大量图片进行统一处理的情况下使用的,本文主要为大家详细介绍了如何利用Python实现批量修改图片大小,需要的可以参考下
    2023-10-10

最新评论