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爬虫框架scrapy实现downloader_middleware设置proxy代理功能示例
Aug 04 Python
Python识别快递条形码及Tesseract-OCR使用详解
Jul 15 Python
python numpy 矩阵堆叠实例
Jan 17 Python
Python3创建Django项目的几种方法(3种)
Jun 03 Python
python:HDF和CSV存储优劣对比分析
Jun 08 Python
PyTorch的torch.cat用法
Jun 28 Python
python3.4中清屏的处理方法
Jul 06 Python
基于Python爬取素材网站音频文件
Oct 21 Python
python中strip(),lstrip(),rstrip()函数的使用讲解
Nov 17 Python
Pycharm Plugins加载失败问题解决方案
Nov 28 Python
python之pygame模块实现飞机大战完整代码
Nov 29 Python
Python爬虫之Selenium设置元素等待的方法
Dec 04 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
Zerg兵种介绍
2020/03/14 星际争霸
Breeze 文章管理系统 v1.0.0正式发布
2006/12/14 PHP
使用php来实现网络服务
2009/09/15 PHP
php中计算程序运行时间的类代码
2012/11/03 PHP
PHP定时任务获取微信access_token的方法
2016/10/10 PHP
Yii2中添加全局函数的方法分析
2017/05/04 PHP
php提高脚本性能的4个技巧
2020/08/18 PHP
formvalidator验证插件中有关ajax验证问题
2013/01/04 Javascript
JS取request值以及自动执行使用示例
2014/02/24 Javascript
jQuery检测鼠标左键和右键点击的方法
2015/03/17 Javascript
javascript控制层显示或隐藏的方法
2015/07/22 Javascript
BootstrapValidator实现注册校验和登录错误提示效果
2017/03/10 Javascript
使用Angular CLI生成路由的方法
2018/03/24 Javascript
详解easyui 切换主题皮肤
2019/04/04 Javascript
vscode 配置vue+vetur+eslint+prettier自动格式化功能
2020/03/23 Javascript
解决vuecli3中img src 的引入问题
2020/08/04 Javascript
HTML中使用python屏蔽一些基本功能的方法
2017/07/07 Python
Python模糊查询本地文件夹去除文件后缀的实例(7行代码)
2017/11/09 Python
Python实现通讯录功能
2018/02/22 Python
漂亮的Django Markdown富文本app插件的实现
2019/01/02 Python
python matplotlib拟合直线的实现
2019/11/19 Python
python 引用传递和值传递详解(实参,形参)
2020/06/05 Python
详解HTML5中的标签
2015/06/19 HTML / CSS
英国最大的女性服装零售商:Dorothy Perkins
2017/03/30 全球购物
shell程序中如何注释
2012/01/28 面试题
介绍一下RMI的基本概念
2016/12/17 面试题
物流管理专业推荐信
2014/09/06 职场文书
超市督导岗位职责
2015/04/10 职场文书
办公室规章制度范本
2015/08/04 职场文书
PyQt5 QThread倒计时功能的实现代码
2021/04/02 Python
图解上海144收音机
2021/04/22 无线电
Python Django 后台管理之后台模型属性详解
2021/04/25 Python
用python批量解压带密码的压缩包
2021/05/31 Python
再谈python_tkinter弹出对话框创建
2022/03/20 Python
安装Windows Server 2012 R2企业版操作系统并设置好相关参数
2022/04/29 Servers
Python保存并浏览用户的历史记录
2022/04/29 Python