matplotlib 对称图_基于matplotlib的对称流图

我试着用matplotlib绘制一个球体周围磁场的流线,它确实工作得很好。然而,得到的图像不是对称的,但应该是对称的(我认为)。

这是用于生成图像的代码。请原谅篇幅,但我认为这比仅仅发布一个不起作用的片段要好。另外,它不是很像Python,那是因为我把它从Matlab中转换过来,这比我预期的要简单。在from __future__ import division

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.patches import Circle

def cart2spherical(x, y, z):

r = np.sqrt(x**2 + y**2 + z**2)

phi = np.arctan2(y, x)

theta = np.arccos(z/r)

if r == 0:

theta = 0

return (r, theta, phi)

def S(theta, phi):

S = np.array([[np.sin(theta)*np.cos(phi), np.cos(theta)*np.cos(phi), -np.sin(phi)],

[np.sin(theta)*np.sin(phi), np.cos(theta)*np.sin(phi), np.cos(phi)],

[np.cos(theta), -np.sin(theta), 0]])

return S

def computeB(r, theta, phi, a=1, muR=100, B0=1):

delta = (muR - 1)/(muR + 2)

if r > a:

Bspherical = B0*np.array([np.cos(theta) * (1 + 2*delta*a**3 / r**3),

np.sin(theta) * (delta*a**3 / r**3 - 1),

0])

B = np.dot(S(theta, phi), Bspherical)

else:

B = 3*B0*(muR / (muR + 2)) * np.array([0, 0, 1])

return B

Z, X = np.mgrid[-2.5:2.5:1000j, -2.5:2.5:1000j]

Bx = np.zeros(np.shape(X))

Bz = np.zeros(np.shape(X))

Babs = np.zeros(np.shape(X))

for i in range(len(X)):

for j in range(len(Z)):

r, theta, phi = cart2spherical(X[0, i], 0, Z[j, 0])

B = computeB(r, theta, phi)

Bx[i, j], Bz[i, j] = B[0], B[2]

Babs[i, j] = np.sqrt(B[0]**2 + B[1]**2 + B[2]**2)

fig=plt.figure()

ax=fig.add_subplot(111)

plt.streamplot(X, Z, Bx, Bz, color='k', linewidth=0.8*Babs, density=1.3,

minlength=0.9, arrowstyle='-')

ax.add_patch(Circle((0, 0), radius=1, facecolor='none', linewidth=2))

plt.axis('equal')

plt.axis('off')

fig.savefig('streamlines.pdf', transparent=True, bbox_inches='tight', pad_inches=0)

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值