python+matplotlib演示电偶极子实例代码
作者:mengwei 发布时间:2021-07-09 12:52:35
标签:python,matplotlib
使用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


猜你喜欢
- 新手在配置pytorch过程中总会或多或少遇到些问题,同时网上关于pytorch的环境配置琳琅满目,不知道应该按照哪个配置,这里笔者记录一下
- 这个Python脚本是用来对实时文件的内容监控,比如 Error 或者 time out 字段都可以进行自定义;算是我的第一个真正的Pyth
- 1.如果客户端和服务器端的连接需要跨越并通过不可信任的网络,那么就需要使用SSH隧道来加密该连接的通信。2.用set password语句来
- 本文实例讲述了mysql外键的三种关系。分享给大家供大家参考,具体如下:因为有foreign key的约束,使得两张表形成了三种了关系:多对
- 数据类型判断在python(版本3.0以上)使用变量,并进行值比较时。有时候会出现以下错误:TypeError: unorderable t
- AdobeAdobe公司的标识1982年,40多岁的程序员约翰·沃诺克(John Warnock)和查尔斯·杰斯克(Charles Gesc
- 一、Mysql 中索引的创建删除innodb 和 myisam 引擎会自动为主键或者带有 UNIQUE 属性的列建立索引。如果要为其他列建立
- 首先,我们来随便写一个带空格的列表:list1 = ['122','2333','3444'
- 有没有曾经为IE浏览器中长按钮莫名其妙的padding感到困扰?在分析解决方法之前,我们首先来看一下问题所在。在IE中,如果按钮文本比较长,
- 前言:Unittest又名单元测试,主要用于测试自己写的代码的正确性和完备性,也方便自己和他人对代码的维护。在软件开发过程中,测试驱动开发的
- 一、概述机器学习算法在近几年大数据点燃的热火熏陶下已经变得被人所“熟知”,就算不懂得其中各算法理论,叫你喊上一两个著名算法的名字,你也能昂首
- 我们的机器学习任务通常会跟全局图像的问题有关(例如,“图像是否包含一只猫呢?”),所以我们最后一层的神经元应该对整个输入的全局敏感。通过逐渐
- 富文本-图片上传html:<div class="layui-form-item layui-form-text"
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&
- 本文实例讲述了PHP模板引擎Smarty中变量的使用方法。分享给大家供大家参考,具体如下:一、概述:Smarty 是 PHP 众多模板引擎中
- 大致介绍在TypeScript里,接口的作用就是为这些类型命名和为你的代码或第三方代码定义契约。接口例子: function p
- 哲学上有种说法,“运动是绝对的,静止是相对的”。我们在编写各样的效果时,时常会碰到动画。下面的章,将讨论动画的原理以及实现。动画,简而言之就
- 一直一来,我都在想办法看能不能用 col 控制表格的列隐藏,但是无奈,FF对COL支持不够好(其实是以前没有发现COL在FF下要怎么弄)。今
- 何谓socket计算机,顾名思义即是用来做计算。因而也需要输入和输出,输入需要计算的条件,输出计算结果。这些输入输出可以抽象为I/O(inp
- 本文实例讲述了JS实现针对给定时间的倒计时功能。分享给大家供大家参考,具体如下:有时候,网站需要一个倒计时的特效来庆祝某些特别的日子。自己也