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的框架下的web app的详细教程
Apr 30 Python
改进Django中的表单的简单方法
Jul 17 Python
python smtplib模块自动收发邮件功能(一)
May 22 Python
Python 处理图片像素点的实例
Jan 08 Python
python学生管理系统开发
Jan 30 Python
PyQt5 QTable插入图片并动态更新的实例
Jun 18 Python
VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的方法详解
Jul 01 Python
Python中typing模块与类型注解的使用方法
Aug 05 Python
python3调用windows dos命令的例子
Aug 14 Python
tensorflow 分类损失函数使用小记
Feb 18 Python
Python如何对齐字符串
Jul 30 Python
python基于exchange函数发送邮件过程详解
Nov 06 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中常用的转义函数
2014/02/28 PHP
ThinkPHP空模块和空操作详解
2014/06/30 PHP
php去除数组中重复数据
2014/11/18 PHP
PHP基于swoole多进程操作示例
2019/08/12 PHP
php模式设计之观察者模式应用实例分析
2019/09/25 PHP
js继承 Base类的源码解析
2008/12/30 Javascript
JavaScript中的eval()函数详解
2013/08/22 Javascript
Internet Explorer 11 浏览器介绍:别叫我IE
2014/09/28 Javascript
NodeJS实现阿里大鱼短信通知发送
2016/01/17 NodeJs
仅一个form表单 js实现注册信息依次填写提交功能
2016/06/12 Javascript
jQuery mobile的header和footer在点击屏幕的时候消失的解决办法
2016/07/01 Javascript
js手动播放图片实现图片轮播效果
2016/09/17 Javascript
利用js+css+html实现固定table的列头不动
2016/12/08 Javascript
vue构建单页面应用实战
2017/04/10 Javascript
JavaScript显式数据类型转换详解
2019/03/18 Javascript
前端天气插件tpwidget使用方法详解
2019/06/24 Javascript
jquery.pager.js分页实现详解
2019/07/29 jQuery
Nuxt.js的路由跳转操作(页面跳转nuxt-link)
2020/11/06 Javascript
解决python 输出是省略号的问题
2018/04/19 Python
解决Tensorflow使用pip安装后没有model目录的问题
2018/06/13 Python
python用pandas数据加载、存储与文件格式的实例
2018/12/07 Python
Python 运行.py文件和交互式运行代码的区别详解
2019/07/02 Python
Django 批量插入数据的实现方法
2020/01/12 Python
通过一张图教会你CSS3倒影的实现
2017/09/26 HTML / CSS
英国Zoro工具:手动工具,电动工具和个人防护用品
2016/11/02 全球购物
Lululemon加拿大官网:加拿大知名体育服装零售商
2019/04/12 全球购物
Rentalcars.com中国:世界上最大的在线汽车租赁服务
2019/08/22 全球购物
Elizabeth Gage官网:英国最好的珠宝设计之一
2020/09/26 全球购物
应聘教师推荐信
2013/10/31 职场文书
军训自我鉴定
2014/01/22 职场文书
请假条标准格式规范
2014/04/10 职场文书
师范学院毕业生求职信
2014/06/24 职场文书
一年级语文上册复习计划
2015/01/17 职场文书
办公室主任岗位职责范本
2015/03/31 职场文书
给朋友的道歉短信
2015/05/12 职场文书
导游词之宁夏贺兰山岩画
2019/11/08 职场文书