python+matplotlib演示电偶极子实例代码
作者:mengwei 时间:2021-07-09 12:52:35
使用matplotlib.tri.CubicTriInterpolator.演示变化率计算:
完整实例:
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()
来源:https://matplotlib.org/index.html
标签:python,matplotlib
0
投稿
猜你喜欢
Python把对应格式的csv文件转换成字典类型存储脚本的方法
2022-07-08 08:39:06
图文详解vscode配置运行php项目完整版
2023-05-28 22:56:03
一篇文章介绍redux、react-redux、redux-saga总结
2023-08-22 16:56:32
asp+jsp+JavaScript动态实现添加数据行
2023-07-03 05:37:15
python实现爬虫统计学校BBS男女比例(一)
2023-06-24 17:48:28
python中的循环结构问题
2023-05-30 14:13:21
Python中bisect的使用方法
2021-12-03 05:56:12
页面表达常用方式
2010-05-27 12:42:00
asp 实现检测字符串是否为纯字母和数字组合的函数
2009-10-04 20:39:00
超级连接的title提示中如何换行实现多行显示
2008-03-07 15:57:00
Yii2框架实现登陆添加验证码功能示例
2023-11-21 11:36:32
3个常用的JS时间代码
2009-03-22 15:29:00
python中sys模块是做什么用的
2021-04-30 10:04:48
PDO::prepare讲解
2023-06-06 06:15:28
javascript面向对象技术基础(二)
2010-02-07 13:09:00
python连接字符串的方法小结
2023-12-29 16:24:49
XMLHTTPRequest的属性和方法简介
2007-12-18 18:42:00
一文详解Python中复合语句的用法
2021-08-11 23:15:40
Python使用cx_Oracle调用Oracle存储过程的方法示例
2021-01-26 19:37:28
golang三元表达式的使用方法
2023-08-28 14:34:09