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统计列表中的重复项出现的次数的方法
Aug 18 Python
提升Python程序运行效率的6个方法
Mar 31 Python
Python设计模式之中介模式简单示例
Jan 09 Python
Python机器学习k-近邻算法(K Nearest Neighbor)实例详解
Jun 25 Python
python使用BeautifulSoup与正则表达式爬取时光网不同地区top100电影并对比
Apr 15 Python
详解Python的循环结构知识点
May 20 Python
在flask中使用python-dotenv+flask-cli自定义命令(推荐)
Jan 05 Python
解决Tensorflow sess.run导致的内存溢出问题
Feb 05 Python
python字符串替换re.sub()实例解析
Feb 09 Python
python实现替换word中的关键文字(使用通配符)
Feb 13 Python
通过代码实例解析Pytest运行流程
Aug 20 Python
Python实现文本文件拆分写入到多个文本文件的方法
Apr 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
js最简单的拖拽效果实现代码
2010/09/24 Javascript
javascript中的void运算符语法及使用介绍
2013/03/10 Javascript
javascript获取ckeditor编辑器的值(实现代码)
2013/11/18 Javascript
Javascript 按位取反运算符 (~)
2014/02/04 Javascript
使用iframe window的scroll方法控制iframe页面滚动
2014/03/05 Javascript
JavaScript中伪协议 javascript:使用探讨
2014/07/18 Javascript
javascript瀑布流布局实现方法详解
2016/02/17 Javascript
jQuery增加和删除表格项目及实现表格项目排序的方法
2016/05/30 Javascript
window.open打开窗口被拦截的快速解决方法
2016/08/04 Javascript
jQuery+HTML5实现弹出创意搜索框层
2016/12/29 Javascript
利用Node.js对文件进行重命名
2017/03/12 Javascript
JavaScript实现简单生成随机颜色的方法
2017/09/21 Javascript
Angular自定义组件实现数据双向数据绑定的实例
2017/12/11 Javascript
详解JavaScript的内存空间、赋值和深浅拷贝
2019/04/17 Javascript
vue router 通过路由来实现切换头部标题功能
2019/04/24 Javascript
解决Echarts 显示隐藏后宽度高度变小的问题
2020/07/19 Javascript
解决vue使用vant轮播组件swipe + flex时文字抖动问题
2021/01/07 Vue.js
Python中__call__用法实例
2014/08/29 Python
python写入xml文件的方法
2015/05/08 Python
python使用super()出现错误解决办法
2017/08/14 Python
深入分析python中整型不会溢出问题
2018/06/18 Python
python2和python3在处理字符串上的区别详解
2019/05/29 Python
python调用函数、类和文件操作简单实例总结
2019/11/29 Python
python能自学吗
2020/06/18 Python
如何利用Python动态模拟太阳系运转
2020/09/04 Python
手摸手教你用canvas实现给图片添加平铺水印的实现
2019/08/20 HTML / CSS
AmazeUI 缩略图的实现示例
2020/08/18 HTML / CSS
索桥的故事教学反思
2014/02/06 职场文书
学校春季防火方案
2014/06/08 职场文书
运动会演讲稿100字
2014/08/25 职场文书
产品陈列协议书(标准版)
2014/09/17 职场文书
2014个人年终工作总结范文
2014/12/15 职场文书
赔偿协议书怎么写
2015/01/28 职场文书
正规借条模板
2015/05/26 职场文书
Oracle11g r2 卸载干净重装的详细教程(亲测有效已重装过)
2021/06/04 Oracle
Java8中接口的新特性使用指南
2021/11/01 Java/Android