0

你能帮我这个代码吗?我正在尝试将力线整合到给定点。不知道哪里错了——剧情没有流线。

数据 - 偶极磁场在这里

我用数据的变化和流线数量的变化尝试了这个例子。

import numpy as np
import matplotlib.pyplot as plt
from numpy import array
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D                 # 3d graph
from mpl_toolkits.mplot3d import proj3d                 # 3d graph
import math
from matplotlib import patches
import code
import yt
from yt import YTArray  # arrays in yt module
from yt.visualization.api import Streamlines  # force lines
import matplotlib.pylab as pl# Choose point in field
X_point = 0.007089085922957821
Y_point = 0.038439192046320805
Z_point = 0# Load data (dictionary)
try:
    import cPickle as pickle
except ImportError:  # python 3.x
    import picklewith open('data.p', 'rb') as fp:
    data = pickle.load(fp)Bx_d = data["Bx"]
By_d = data["By"]
Bz_d = data["Bz"]# 3d array of dipole magnetic field 
print(type(data))
bbox = np.array([[-0.15, 0.15], [0, 0.2], [-0.1, 0.1]]) # box, border
ds = yt.load_uniform_grid(data, Bx_d.shape, length_unit="Mpc", bbox=bbox, nprocs=100) # data, dimensionc = YTArray([X_point, Y_point, Z_point], 'm') # Define c: the center of the box, chosen point
c1 = ds.domain_center
print('c1', c1)
print(type(c1))
print('center',c)
N = 1 # N: the number of streamlines
scale = ds.domain_width[0] # scale: the spatial scale of the streamlines relative to the boxsize,
pos = c# Create streamlines of the 3D vector velocity and integrate them through
# the box defined above
streamlines = Streamlines(ds, pos, 'Bx', 'By', 'Bz', length=None) # length of integration
streamlines.integrate_through_volume()# Create a 3D plot, trace the streamlines through the 3D volume of the plot
fig=pl.figure()
ax = Axes3D(fig)
ax.scatter(X_point, Y_point, Z_point, marker = 'o', s=40, c='green')
print('tisk', streamlines.streamlines)for stream in streamlines.streamlines:
    stream = stream[np.all(stream != 0.0, axis=1)]
    ax.plot3D(stream[:,0], stream[:,1], stream[:,2], alpha=0.1)# Save the plot to disk.
pl.savefig('streamlines.png')
plt.show()

输出: 在此处输入图像描述

4

1 回答 1

0

在不了解数据的更多信息以及 print 调用的输出是什么的情况下,并不完全清楚错误是什么。如果流线具有有意义的值(即stream[:,0]etc 的值在您的范围内Axes3D,它应该会产生结果。

调试选项将从检查单个值开始,然后继续在 2D 中绘制它们(使用每个组件对stream- (0,1)、(1,2) 和 (0,2)),然后检查什么如果您允许Axes3D自动缩放 xyz 轴,则会发生这种情况。您也可以尝试使用该alpha值,以查看线条是否太浅而无法看到。

这产生的示例图像也会有所帮助,以便可以清楚地了解 matplotlib 分配给Axes3D对象的属性。

于 2020-03-29T18:49:59.613 回答