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批量修改文件后缀示例代码分享
Dec 24 Python
Python计算三角函数之asin()方法的使用
May 15 Python
Django中的CACHE_BACKEND参数和站点级Cache设置
Jul 23 Python
Python 转义字符详细介绍
Mar 21 Python
详解Python3.6的py文件打包生成exe
Jul 13 Python
Flask框架各种常见装饰器示例
Jul 17 Python
Apache,wsgi,django 程序部署配置方法详解
Jul 01 Python
django迁移数据库错误问题解决
Jul 29 Python
Python2和3字符编码的区别知识点整理
Aug 08 Python
Django Form and ModelForm的区别与使用
Dec 06 Python
python tkinter之顶层菜单、弹出菜单实例
Mar 04 Python
Python 装饰器(decorator)常用的创建方式及解析
Apr 24 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
一个简洁的多级别论坛
2006/10/09 PHP
很让人受教的 提高php代码质量36计
2012/09/05 PHP
十幅图告诉你什么是PHP引用
2015/02/22 PHP
PHP使用Memcache时模拟命名空间及缓存失效问题的解决
2016/02/27 PHP
Laravel Eloquent分表方法并使用模型关联的实现
2019/11/25 PHP
js中使用DOM复制(克隆)指定节点名数据到新的XML文件中的代码
2011/07/27 Javascript
JavaScript高级程序设计 读书笔记之十 本地对象Date日期
2012/02/27 Javascript
原生js和jQuery随意改变div属性style的名称和值
2014/10/22 Javascript
JQuery设置时间段下拉选择实例
2014/12/30 Javascript
JS+CSS实现另类带提示效果的竖向导航菜单
2015/10/15 Javascript
三种带箭头提示框总结实例
2016/06/14 Javascript
js 判断登录界面的账号密码是否为空
2017/02/08 Javascript
jQuery在header中设置请求信息的方法
2017/03/06 Javascript
微信小程序文章详情页面实现代码
2018/09/10 Javascript
在vue中解决提示警告 for循环报错的方法
2018/09/28 Javascript
Vue 动态组件与 v-once 指令的实现
2019/02/12 Javascript
从零搭一个自用的前端脚手架的方法步骤
2019/09/23 Javascript
python实现迭代法求方程组的根过程解析
2019/11/25 Javascript
vue 项目@change多个参数传值多个事件的操作
2021/01/29 Vue.js
python使用Flask框架获取用户IP地址的方法
2015/03/21 Python
在MAC上搭建python数据分析开发环境
2016/01/26 Python
详解python函数传参是传值还是传引用
2018/01/16 Python
pandas基于时间序列的固定时间间隔求均值的方法
2019/07/04 Python
Python celery原理及运行流程解析
2020/06/13 Python
Html5基于canvas实现电子签名并生成PDF文档
2020/12/07 HTML / CSS
西班牙电子产品购物网站:Electronicamente
2018/07/26 全球购物
必须要使用游标的SQL语句有那些
2012/05/07 面试题
数据库设计的包括哪两种,请分别进行说明
2016/07/15 面试题
工商管理应届生求职信
2013/10/07 职场文书
公司门卫工作职责
2014/06/28 职场文书
机关领导查摆四风思想汇报
2014/09/13 职场文书
司法局群众路线教育实践活动整改措施思想汇报
2014/10/13 职场文书
2016年第十九届推普周活动总结
2016/04/06 职场文书
React列表栏及购物车组件使用详解
2021/06/28 Javascript
JavaScript高级程序设计之变量与作用域
2021/11/17 Javascript
SQL Server删除表中的重复数据
2022/05/25 SQL Server