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之编写类之四再论继承
Oct 11 Python
Python实现霍夫圆和椭圆变换代码详解
Jan 12 Python
Python+OpenCv制作证件图片生成器的操作方法
Aug 21 Python
python实现一个点绕另一个点旋转后的坐标
Dec 04 Python
python3正则模块re的使用方法详解
Feb 11 Python
用Python绘制漫步图实例讲解
Feb 26 Python
python不同系统中打开方法
Jun 23 Python
Python函数递归调用实现原理实例解析
Aug 11 Python
详解pytorch tensor和ndarray转换相关总结
Sep 03 Python
Python 高效编程技巧分享
Sep 10 Python
python文件名批量重命名脚本实例代码
Apr 22 Python
python文本处理的方案(结巴分词并去除符号)
May 26 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 CodeBase:将时间显示为&quot;刚刚&quot;&quot;n分钟/小时前&quot;的方法详解
2013/06/06 PHP
curl实现站外采集的方法和技巧
2014/01/31 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(七)
2014/06/23 PHP
ThinkPHP调试模式与日志记录概述
2014/08/22 PHP
extjs grid取到数据而不显示的解决
2008/12/29 Javascript
一个很简单的jquery+xml+ajax的无刷新树结构(无css,后台是c#)
2010/06/02 Javascript
javascript 函数调用的对象和方法
2010/07/01 Javascript
用js实现table单元格高宽调整,兼容合并单元格(兼容IE6、7、8、FF)实例
2013/06/25 Javascript
JS保留两位小数,多位小数的示例代码
2014/01/07 Javascript
JavaScript中number转换成string介绍
2014/12/31 Javascript
JavaScript使用replace函数替换字符串的方法
2015/04/06 Javascript
jQuery+PHP实现可编辑表格字段内容并实时保存
2015/10/09 Javascript
js从外部获取图片的实现方法
2016/08/05 Javascript
BootStrap入门教程(二)之固定的内置样式
2016/09/19 Javascript
JavaScript原生编写《飞机大战坦克》游戏完整实例
2017/01/04 Javascript
Javascript刷新页面的实例
2017/09/23 Javascript
关于layui flow loading占位图的实现方法
2019/09/21 Javascript
详解Python使用simplejson模块解析JSON的方法
2016/03/24 Python
python实现学生信息管理系统
2020/04/05 Python
python 多线程中子线程和主线程相互通信方法
2018/11/09 Python
Django框架序列化与反序列化操作详解
2019/11/01 Python
django日志默认打印request请求信息的方法示例
2020/05/17 Python
如何在VSCode下使用Jupyter的教程详解
2020/07/13 Python
HTML5有哪些新特征
2015/12/01 HTML / CSS
世界领先的豪华床上用品供应商之一:Bedeck Home
2019/03/18 全球购物
草莓网官网:StrawberryNET
2019/08/21 全球购物
绩效专员岗位职责
2013/12/02 职场文书
信息与计算科学专业推荐信
2014/02/23 职场文书
幼儿园健康教育方案
2014/06/14 职场文书
三方协议书
2015/01/27 职场文书
英语邀请函范文
2015/02/02 职场文书
人事文员岗位职责
2015/02/04 职场文书
数学备课组工作总结
2015/08/12 职场文书
放假通知怎么写
2015/08/18 职场文书
MySQL Server层四个日志的实现
2022/03/31 MySQL
Python tensorflow卷积神经Inception V3网络结构
2022/05/06 Python