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 相关文章推荐
在Django的模板中使用认证数据的方法
Jul 23 Python
python安装与使用redis的方法
Apr 19 Python
详解Django rest_framework实现RESTful API
May 24 Python
Python爬虫PyQuery库基本用法入门教程
Aug 04 Python
Python 获取div标签中的文字实例
Dec 20 Python
python用win32gui遍历窗口并设置窗口位置的方法
Jul 26 Python
python求加权平均值的实例(附纯python写法)
Aug 22 Python
Python实现元素等待代码实例
Nov 11 Python
使用pytorch实现可视化中间层的结果
Dec 30 Python
Python实现结构体代码实例
Feb 10 Python
python继承threading.Thread实现有返回值的子类实例
May 02 Python
Python pip 常用命令汇总
Oct 19 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 文件夹删除、php清除缓存程序
2009/08/25 PHP
探讨PHP中this,self,parent的区别详解
2013/06/08 PHP
Yii实现MySQL多数据库和读写分离实例分析
2014/12/03 PHP
PHP+JS三级菜单联动菜单实现方法
2016/02/24 PHP
thinkPHP删除前弹出确认框的简单实现方法
2016/05/16 PHP
iOS10推送通知开发教程
2016/09/19 PHP
详解PHP中foreach的用法和实例
2016/10/25 PHP
基于CI(CodeIgniter)框架实现购物车功能的方法
2018/04/09 PHP
javascript instanceof 与typeof使用说明
2010/01/11 Javascript
jquery下checked取值问题的解决方法
2012/08/09 Javascript
Javascript与jQuery方法的隐藏与显示
2015/01/19 Javascript
Underscore.js常用方法总结
2015/02/28 Javascript
jQuery中dom元素上绑定的事件详解
2015/04/24 Javascript
学习Javascript面向对象编程之封装
2016/02/23 Javascript
jQuery图片轮播插件——前端开发必看
2016/05/31 Javascript
使用 Javascript 实现浏览器推送提醒功能的示例
2017/11/03 Javascript
javascript中call,apply,callee,caller用法实例分析
2019/07/24 Javascript
VUE.CLI4.0配置多页面入口的实现
2019/11/25 Javascript
学习 Vue.js 遇到的那些坑
2021/02/02 Vue.js
python实现超市扫码仪计费
2018/05/30 Python
Numpy 将二维图像矩阵转换为一维向量的方法
2018/06/05 Python
python 计算数据偏差和峰度的方法
2019/06/29 Python
python制作英语翻译小工具代码实例
2019/09/09 Python
Python编程快速上手——Excel表格创建乘法表案例分析
2020/02/28 Python
Python3以GitHub为例来实现模拟登录和爬取的实例讲解
2020/07/30 Python
使用phonegap进行提示操作的具体方法
2017/03/30 HTML / CSS
印尼在线旅游门户网站:NusaTrip
2019/11/01 全球购物
区域总监的岗位职责
2013/11/21 职场文书
校园报刊亭的创业计划书
2014/01/02 职场文书
临床专业自荐信
2014/06/22 职场文书
酒店服务员岗位职责
2015/02/09 职场文书
2015年学校政教处工作总结
2015/05/26 职场文书
运动会班级口号霸气押韵
2015/12/24 职场文书
《雪地里的小画家》教学反思
2016/02/16 职场文书
2016年主题党日活动总结
2016/04/05 职场文书
浅谈Redis主从复制以及主从复制原理
2021/05/29 Redis