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()

总结

以上就是本文关于python+matplotlib演示电偶极子实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

您可能感兴趣的文章:

  • python+matplotlib实现礼盒柱状图实例代码
  • Python+matplotlib实现填充螺旋实例
  • python+matplotlib实现鼠标移动三角形高亮及索引显示
  • Python+matplotlib+numpy实现在不同平面的二维条形图
  • Python+matplotlib+numpy绘制精美的条形统计图
  • python+matplotlib绘制简单的海豚(顶点和节点的操作)
  • Python使用Matplotlib实现Logos设计代码
  • Python使用matplotlib填充图形指定区域代码示例
时间: 2018-01-09

Python+matplotlib+numpy实现在不同平面的二维条形图

在不同平面上绘制二维条形图. 本实例制作了一个3d图,其中有二维条形图投射到平面y=0,y=1,等. 演示结果: 完整代码: from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np # Fixing random state for reproducibility np.random.seed(19680801) fig = plt.figure() ax = fig.a

Python+matplotlib+numpy绘制精美的条形统计图

本文实例主要向大家分享了一个Python+matplotlib+numpy绘制精美的条形统计图的代码,效果展示如下: 完整代码如下: import matplotlib.pyplot as plt from numpy import arange from numpy.random import rand def gbar(ax, x, y, width=0.5, bottom=0): X = [[.6, .6], [.7, .7]] for left, top in zip(x, y): ri

Python使用Matplotlib实现Logos设计代码

本文主要展示了使用matplotlib设计logo的示例及完整代码,首先看下其演示结果: Python代码如下: import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.cm as cm mpl.rcParams['xtick.labelsize'] = 10 mpl.rcParams['ytick.labelsize'] = 12 mpl.rcParams['ax

python+matplotlib实现礼盒柱状图实例代码

演示结果: 完整代码: import matplotlib.pyplot as plt import numpy as np from matplotlib.image import BboxImage from matplotlib._png import read_png import matplotlib.colors from matplotlib.cbook import get_sample_data class RibbonBox(object): original_image =

python+matplotlib实现鼠标移动三角形高亮及索引显示

Trifinder事件实例 实例展示Trifinder对象对的使用.当鼠标移动到一个被分割的三角形上,这个三角形高亮显示,并且它的标签在图标题显示. 展示下演示结果: 完整代码: import matplotlib.pyplot as plt from matplotlib.tri import Triangulation from matplotlib.patches import Polygon import numpy as np def update_polygon(tri): if t

python+matplotlib绘制简单的海豚(顶点和节点的操作)

海豚 本文例子主要展示了如何使用补丁.路径和转换类绘制和操作给定的顶点和节点的形状. 测试可用. import matplotlib.cm as cm import matplotlib.pyplot as plt from matplotlib.patches import Circle, PathPatch from matplotlib.path import Path from matplotlib.transforms import Affine2D import numpy as n

Python使用matplotlib填充图形指定区域代码示例

本文代码重点在于演示Python扩展库matplotlib.pyplot中fill_between()函数的用法. import numpy as np import matplotlib.pyplot as plt # 生成模拟数据 x = np.arange(0.0, 4.0*np.pi, 0.01) y = np.sin(x) # 绘制正弦曲线 plt.plot(x, y) # 绘制基准水平直线 plt.plot((x.min(),x.max()), (0,0)) # 设置坐标轴标签 pl

Python+matplotlib实现填充螺旋实例

填充螺旋演示结果: 实例代码: import matplotlib.pyplot as plt import numpy as np theta = np.arange(0, 8*np.pi, 0.1) a = 1 b = .2 for dt in np.arange(0, 2*np.pi, np.pi/2.0): x = a*np.cos(theta + dt)*np.exp(b*theta) y = a*np.sin(theta + dt)*np.exp(b*theta) dt = dt +

python+matplotlib绘制饼图散点图实例代码

本文是从matplotlib官网上摘录下来的一个实例,实现的功能是Python+matplotlib绘制自定义饼图作为散点图的标记,具体如下. 首先看下演示效果 实例代码: import numpy as np import matplotlib.pyplot as plt # first define the ratios r1 = 0.2 # 20% r2 = r1 + 0.4 # 40% # define some sizes of the scatter marker sizes = n

python+matplotlib绘制3D条形图实例代码

本文分享的实例主要实现的是Python+matplotlib绘制一个有阴影和没有阴影的3D条形图,具体如下. 首先看看演示效果: 完整代码如下: import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # setup the figure and axes fig = plt.figure(figsize=(8, 3)) ax1 = fig.add_subplot(121

python+matplotlib绘制旋转椭圆实例代码

旋转椭圆 实例代码: import matplotlib.pyplot as plt import numpy as np from matplotlib.patches import Ellipse delta = 45.0 # degrees angles = np.arange(0, 360 + delta, delta) ells = [Ellipse((1, 1), 4, 2, a) for a in angles] a = plt.subplot(111, aspect='equal

Python+matplotlib绘制不同大小和颜色散点图实例

 具有不同标记颜色和大小的散点图演示. 演示结果: 实现代码: import numpy as np import matplotlib.pyplot as plt import matplotlib.cbook as cbook # Load a numpy record array from yahoo csv data with fields date, open, close, # volume, adj_close from the mpl-data/example directory

python+matplotlib实现动态绘制图片实例代码(交互式绘图)

本文研究的主要是python+matplotlib实现动态绘制图片(交互式绘图)的相关内容,具体介绍和实现代码如下所示. 最近在研究动态障碍物避障算法,在Python语言进行算法仿真时需要实时显示障碍物和运动物的当前位置和轨迹,利用Anaconda的Python打包集合,在Spyder中使用Python3.5语言和matplotlib实现路径的动态显示和交互式绘图(和Matlab功能类似). Anaconda是一个用于科学计算的Python发行版,支持 Linux, Mac, Windows系统

matplotlib作图添加表格实例代码

本文所示代码主要是通过Python+matplotlib实现作图,并且在图中添加表格的功能,具体如下. 代码 import matplotlib.pyplot as plt import numpy as np plt.figure() ax = plt.gca() y = np.random.randn(9) col_labels = ['col1','col2','col3'] row_labels = ['row1','row2','row3'] table_vals = [[11,12,