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


Posted in Python onJanuary 12, 2018

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

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

完整实例:

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爬虫功能
Dec 31 Python
Python简单实现自动删除目录下空文件夹的方法
Aug 29 Python
Python+request+unittest实现接口测试框架集成实例
Mar 16 Python
python3实现基于用户的协同过滤
May 31 Python
python使用matplotlib绘制热图
Nov 07 Python
Django框架模板的使用方法示例
May 25 Python
Python参数类型以及常见的坑详解
Jul 08 Python
使用Python刷淘宝喵币(低阶入门版)
Oct 30 Python
Python如何基于Tesseract实现识别文字功能
Jun 05 Python
解决Keras使用GPU资源耗尽的问题
Jun 22 Python
详细分析Python可变对象和不可变对象
Jul 09 Python
教你利用python实现企业微信发送消息
May 23 Python
Python实现读取及写入csv文件的方法示例
Jan 12 #Python
python+matplotlib绘制旋转椭圆实例代码
Jan 12 #Python
使用C++扩展Python的功能详解
Jan 12 #Python
聊聊Python中的pypy
Jan 12 #Python
Python中实现switch功能实例解析
Jan 11 #Python
Python中getpass模块无回显输入源码解析
Jan 11 #Python
python版微信跳一跳游戏辅助
Jan 11 #Python
You might like
深入理解PHP原理之Session Gc的一个小概率Notice
2011/04/12 PHP
PHP程序开发范例学习之表单 获取文本框的值
2011/08/08 PHP
一段效率很高的for循环语句使用方法
2007/08/13 Javascript
js 函数的执行环境和作用域链的深入解析
2009/11/01 Javascript
JavaScript 语法集锦 脚本之家基础推荐
2009/11/15 Javascript
基于Jquery的动态添加控件并取值的实现代码
2010/09/24 Javascript
IE6下CSS图片缓存问题解决方法
2010/12/09 Javascript
当鼠标移动时出现特效的JQuery代码
2013/11/08 Javascript
jQuery实现设置、移除文本框默认值功能
2015/01/13 Javascript
jquery图片切换实例分析
2015/04/15 Javascript
JS判断当前页面是否在微信浏览器打开的方法
2015/12/08 Javascript
学习jQuey中的return false
2015/12/18 Javascript
详解javascript实现自定义事件
2016/01/19 Javascript
js仿小米官网图片轮播特效
2016/09/29 Javascript
JavaScript中关键字 in 的使用方法详解
2016/10/17 Javascript
JS+CSS3制作炫酷的弹窗效果
2016/11/08 Javascript
在vue中读取本地Json文件的方法
2018/09/06 Javascript
Vue实现移动端左右滑动效果的方法
2018/11/27 Javascript
js变量声明var使用与不使用的区别详解
2019/01/21 Javascript
详解Python当中的字符串和编码
2015/04/25 Python
使用Python进行AES加密和解密的示例代码
2018/02/02 Python
Python中is和==的区别详解
2018/11/15 Python
解决PySide+Python子线程更新UI线程的问题
2019/01/11 Python
python 判断三个数字中的最大值实例代码
2019/07/24 Python
Python  Django 母版和继承解析
2019/08/09 Python
python switch 实现多分支选择功能
2020/12/21 Python
CSS3 3D位移translate效果实例介绍
2016/05/03 HTML / CSS
CSS3改变浏览器滚动条样式
2019/01/04 HTML / CSS
SOA面试题:如何在SOA中实现松耦合
2013/07/21 面试题
土木工程应届生求职信
2013/10/31 职场文书
餐饮业会计岗位职责
2013/12/19 职场文书
商场端午节活动方案
2014/01/29 职场文书
土建施工员岗位职责
2014/07/16 职场文书
单位婚育证明范本
2014/11/21 职场文书
高中政治教师教学反思
2016/02/23 职场文书
Python中的嵌套循环详情
2022/03/23 Python