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 Django模板的使用方法(图文)
Nov 04 Python
python端口扫描系统实现方法
Nov 19 Python
Python实现控制台进度条功能
Jan 04 Python
一个月入门Python爬虫学习,轻松爬取大规模数据
Jan 03 Python
windows下python安装pip图文教程
May 25 Python
Python基于sklearn库的分类算法简单应用示例
Jul 09 Python
Django csrf 验证问题的实现
Oct 09 Python
对Python发送带header的http请求方法详解
Jan 02 Python
python基于C/S模式实现聊天室功能
Jan 09 Python
对Python实现累加函数的方法详解
Jan 23 Python
Pytorch中.new()的作用详解
Feb 18 Python
Python 图片添加美颜效果
Apr 28 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函数array_merge用法一例(合并同类数组)
2013/02/03 PHP
单点登录 Ucenter示例分析
2013/10/29 PHP
php打开远程文件的方法和风险及解决方法
2013/11/12 PHP
smarty自定义函数htmlcheckboxes用法实例
2015/01/22 PHP
YiiFramework入门知识点总结(图文教程)
2015/12/28 PHP
PHP操作mysql数据库分表的方法
2016/06/09 PHP
php基于自定义函数记录log日志方法
2017/07/21 PHP
自己编写的类似JS的trim方法
2013/10/09 Javascript
Jquery Uploadify上传带进度条的简单实例
2014/02/12 Javascript
JQuery实现超链接鼠标提示效果的方法
2015/06/10 Javascript
JS实现的颜色实时渐变效果完整实例
2016/03/25 Javascript
基于Bootstrap和jQuery构建前端分页工具实例代码
2016/11/23 Javascript
jQuery实现的简单排序功能示例【冒泡排序】
2017/01/13 Javascript
JavaScript基于Dom操作实现查找、修改HTML元素的内容及属性的方法
2017/01/20 Javascript
AngularJS双向数据绑定原理之$watch、$apply和$digest的应用
2018/01/30 Javascript
vue-cli扩展多模块打包的示例代码
2018/04/09 Javascript
[05:39]2014DOTA2西雅图国际邀请赛 淘汰赛7月14日TOPPLAY
2014/07/14 DOTA
[51:43]OG vs LGD 2018国际邀请赛淘汰赛BO3 第五场 8.26
2018/08/30 DOTA
[45:34]完美世界DOTA2联赛PWL S3 Rebirth vs CPG 第一场 12.18
2020/12/19 DOTA
Python中的Matplotlib模块入门教程
2015/04/15 Python
使用C#配合ArcGIS Engine进行地理信息系统开发
2016/02/19 Python
PyQt5打开文件对话框QFileDialog实例代码
2018/02/07 Python
Python随机生成身份证号码及校验功能
2018/12/04 Python
Python matplotlib通过plt.scatter画空心圆标记出特定的点方法
2018/12/13 Python
tensorflow将图片保存为tfrecord和tfrecord的读取方式
2020/02/17 Python
美国最便宜的旅游网站:CheapTickets
2017/07/09 全球购物
光电信息专业应届生求职信
2013/10/07 职场文书
大学自我鉴定
2013/12/20 职场文书
运动会广播稿300字
2014/01/10 职场文书
校企合作协议书
2014/04/16 职场文书
中等生评语大全
2014/05/04 职场文书
应届生自荐信
2014/06/30 职场文书
检察院起诉意见书
2015/05/20 职场文书
交通安全教育心得体会
2016/01/15 职场文书
Golang生成Excel文档的方法步骤
2021/06/09 Golang
教你怎么用Python实现GIF动图的提取及合成
2021/06/15 Python