python+matplotlib演示電偶極子實(shí)例代碼
使用matplotlib.tri.CubicTriInterpolator.演示變化率計(jì)算:

完整實(shí)例:
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()
總結(jié)
以上就是本文關(guān)于python+matplotlib演示電偶極子實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
基于Python實(shí)現(xiàn)文件的壓縮與解壓縮
在日常工作中,除了會(huì)涉及到使用Python處理文本文件,有時(shí)候還會(huì)涉及對(duì)壓縮文件的處理。本文為大家總結(jié)了利用Python可以實(shí)現(xiàn)的幾種文件壓縮與解壓縮實(shí)現(xiàn)代碼,需要的可以參考一下2022-03-03
python實(shí)現(xiàn)截取屏幕保存文件,刪除N天前截圖的例子
今天小編就為大家分享一篇python實(shí)現(xiàn)截取屏幕保存文件,刪除N天前截圖的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
python 爬蟲(chóng)基本使用——統(tǒng)計(jì)杭電oj題目正確率并排序
這篇文章主要介紹了python 爬蟲(chóng)基本的基本使用,主要利用了Urllib和BeautifulSoup4這兩個(gè)庫(kù),配以簡(jiǎn)單的實(shí)例幫助大家理解,感興趣的朋友可以了解下2020-10-10
Tensorflow2.10實(shí)現(xiàn)圖像分割任務(wù)示例詳解
這篇文章主要為大家介紹了Tensorflow2.10實(shí)現(xiàn)圖像分割任務(wù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
python在windows調(diào)用svn-pysvn的實(shí)現(xiàn)
本文主要介紹了python在windows調(diào)用svn-pysvn的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Python實(shí)現(xiàn)將Excel轉(zhuǎn)換成為image的方法
今天小編就為大家分享一篇Python實(shí)現(xiàn)將Excel轉(zhuǎn)換成為image的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
python 利用pyttsx3文字轉(zhuǎn)語(yǔ)音過(guò)程詳解
這篇文章主要介紹了python 利用pyttsx3文字轉(zhuǎn)語(yǔ)音過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09

