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写的PHPMyAdmin暴力破解工具代码
Aug 06 Python
python实现bucket排序算法实例分析
May 04 Python
python创建进程fork用法
Jun 04 Python
python获取当前运行函数名称的方法实例代码
Apr 06 Python
Python利用multiprocessing实现最简单的分布式作业调度系统实例
Nov 14 Python
python编写分类决策树的代码
Dec 21 Python
Python生成器以及应用实例解析
Feb 08 Python
python如何为创建大量实例节省内存
Mar 20 Python
如何优雅地改进Django中的模板碎片缓存详解
Jul 04 Python
pandas ix &amp;iloc &amp;loc的区别
Jan 10 Python
用Python配平化学方程式的方法
Jul 20 Python
详解python安装matplotlib库三种失败情况
Jul 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
谏山创故乡大分县日田市水坝将设立《进击的巨人》立艾伦、三笠以及阿尔敏的铜像!
2020/03/06 日漫
php 不使用js实现页面跳转
2014/02/11 PHP
Thinkphp中volist标签mod控制一定记录的换行BUG解决方法
2014/11/04 PHP
php中通过eval实现字符串格式的计算公式
2017/03/18 PHP
php制作圆形用户头像的实例_自定义封装类源代码
2017/09/18 PHP
Js保留小数点的4种效果实现代码分享
2014/04/12 Javascript
jquery动态加载js/css文件方法(自写小函数)
2014/10/11 Javascript
js获取当前时间显示在页面上并每秒刷新
2014/12/24 Javascript
JavaScript检查子字符串是否在字符串中的方法
2016/02/03 Javascript
jQuery基于json与cookie实现购物车的方法
2016/04/15 Javascript
React创建组件的三种方式及其区别
2017/01/12 Javascript
Vue.js与 ASP.NET Core 服务端渲染功能整合
2017/11/16 Javascript
jQuery图片查看插件Magnify开发详解
2017/12/25 jQuery
JS前端知识点 运算符优先级,URL编码与解码,String,Math,arguments操作整理总结
2019/06/27 Javascript
vscode vue 文件模板的配置方法
2019/07/23 Javascript
[10:07]2014DOTA2国际邀请赛 实拍选手现场观战DK对阵Titan
2014/07/12 DOTA
python中set常用操作汇总
2016/06/30 Python
wx.CheckBox创建复选框控件并响应鼠标点击事件
2018/04/25 Python
python 以16进制打印输出的方法
2018/07/09 Python
TensorFlow2.1.0最新版本安装详细教程
2020/04/08 Python
python matplotlib:plt.scatter() 大小和颜色参数详解
2020/04/14 Python
解决python中import文件夹下面py文件报错问题
2020/06/01 Python
德国旅游网站:weg.de
2018/06/03 全球购物
聚网科技C++面试笔试题
2015/09/01 面试题
客服部工作职责范本
2014/02/14 职场文书
公司廉洁自律承诺书
2014/03/27 职场文书
快餐公司创业计划书
2014/04/29 职场文书
ktv筹备计划书
2014/05/03 职场文书
银行爱岗敬业演讲稿
2014/05/05 职场文书
环境整治工作方案
2014/05/18 职场文书
分公司总经理岗位职责
2014/07/30 职场文书
2014教师“四风问题”对照检查材料思想汇报
2014/09/16 职场文书
工伤事故赔偿协议书(标准)
2014/09/29 职场文书
教师纪律作风整顿心得体会
2016/01/23 职场文书
详解Redis瘦身指南
2021/05/26 Redis
Mysql事务索引知识汇总
2022/03/17 MySQL