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 数据清洗之数据合并、转换、过滤、排序
Feb 12 Python
python3.0 模拟用户登录,三次错误锁定的实例
Nov 02 Python
高质量Python代码编写的5个优化技巧
Nov 16 Python
python如何对实例属性进行类型检查
Mar 20 Python
对Python之gzip文件读写的方法详解
Feb 08 Python
深入解析python中的实例方法、类方法和静态方法
Mar 11 Python
Python3实现的简单工资管理系统示例
Mar 12 Python
为什么你还不懂得怎么使用Python协程
May 13 Python
Spring Cloud Feign高级应用实例详解
Dec 10 Python
Python中itertools的用法详解
Feb 07 Python
将 Ubuntu 16 和 18 上的 python 升级到最新 python3.8 的方法教程
Mar 11 Python
tensorflow之读取jpg图像长和宽实例
Jun 18 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 程序授权验证开发思路
2009/07/09 PHP
php5.3后静态绑定用法详解
2016/11/11 PHP
如何修改Laravel中url()函数生成URL的根地址
2017/08/11 PHP
Ext JS Grid在IE6 下宽度的问题解决方法
2009/02/15 Javascript
JS window.opener返回父页面的应用
2009/10/24 Javascript
jQuery侧边栏随窗口滚动实现方法
2013/03/04 Javascript
JS去除右边逗号的简单方法
2013/07/03 Javascript
JavaScript中的包装对象介绍
2015/01/27 Javascript
javascript Array 数组常用方法
2015/04/05 Javascript
javascript生成不重复的随机数
2015/07/17 Javascript
省市区三级联动下拉框菜单javascript版
2015/08/11 Javascript
jQuery幻灯片带缩略图轮播效果代码分享
2015/08/17 Javascript
jQuery实现放大镜效果实例代码
2016/03/17 Javascript
JS在一定时间内跳转页面及各种刷新页面的实现方法
2016/05/26 Javascript
Bootstrap被封装的弹层
2016/07/20 Javascript
jQuery EasyUI结合zTree树形结构制作web页面
2017/09/01 jQuery
node实现基于token的身份验证
2018/04/09 Javascript
解决JQuery的ajax函数执行失败alert函数弹框一闪而过问题
2019/04/10 jQuery
不管你的Python报什么错,用这个模块就能正常运行
2018/09/14 Python
python利用Tesseract识别验证码的方法示例
2019/01/21 Python
python爬取cnvd漏洞库信息的实例
2019/02/14 Python
python实现自动化报表功能(Oracle/plsql/Excel/多线程)
2019/12/02 Python
html5生成柱状图(条形图)效果的实例代码
2016/03/25 HTML / CSS
美国在线精品家居网站:Burke Decor
2017/04/12 全球购物
Crucial英睿达法国官网:内存条及SSD固态硬盘升级
2018/07/13 全球购物
市场营销专业推荐信
2013/11/03 职场文书
《雪儿》教学反思
2014/04/17 职场文书
公司周年庆典策划方案
2014/05/17 职场文书
市场营销专业毕业生求职信
2014/07/21 职场文书
公司离职证明范本
2014/10/17 职场文书
大学生见习报告范文
2014/11/03 职场文书
留学推荐信英文范文
2015/03/26 职场文书
2015年导购员工作总结
2015/04/25 职场文书
拉贝日记观后感
2015/06/05 职场文书
新闻通讯稿模板
2015/07/22 职场文书
python 标准库原理与用法详解之os.path篇
2021/10/24 Python