简体   繁体   English

在 Mayavi 中使用 quiver3d() 绘制 3D 矢量场

[英]Plotting 3D vector field with quiver3d() in Mayavi

I'm having a hard time to figure out why the following code isn't working inside the Jupyter Notebook!我很难弄清楚为什么以下代码在 Jupyter Notebook 中不起作用! Any help is really appreciated任何帮助真的很感激

%gui qt
import matplotlib.pyplot as plt
import numpy as np
from sympy import symbols
from mayavi import mlab

x,y,z = symbols('x y z')
def gradient(f):
    return (f.diff(x), f.diff(y),f.diff(z))

f = x*y**2+z**2
g = gradient(f)

xrange = np.linspace(-3,3,15)
yrange = np.linspace(-3,3,15)
zrange = np.linspace(-3,3,15)
X,Y,Z = np.meshgrid(xrange, yrange, zrange)

U = np.zeros((15,15,15))
V = np.zeros((15,15,15))
W = np.zeros((15,15,15))

for i in range(len(xrange)):
    for j in range(len(yrange)):
        for k in range(len(zrange)):
            x1 = X[i,j,k]
            y1 = Y[i,j,k]
            z1 = Z[i,j,k]
            U[i,j,k] = g[0].subs({x:x1, y:y1, z:z1})
            V[i,j,k] = g[1].subs({x:x1, y:y1, z:z1})
            W[i,j,k] = g[2].subs({x:x1, y:y1, z:z1})


mlab.quiver3d(X,Y,Z,U,V,W)

In fact, I don't receive any error message just a black window of Mayavi.事实上,我没有收到任何错误消息,只是 Mayavi 的一个黑色窗口。 I have waited more than 10min but the windows permanents black.我已经等了 10 多分钟,但窗户永久变黑。 I'm running this code on a machine with Ubuntu 19.10.我在装有 Ubuntu 19.10 的机器上运行此代码。 As a matter of completeness, I would like to mention that the below code works normally.为了完整性,我想提一下下面的代码正常工作。

%gui qt
from mayavi import mlab
import numpy as np

def V(x, y, z):
    """ A 3D sinusoidal lattice with a parabolic confinement. """
    return np.cos(10*x) + np.cos(10*y) + np.cos(10*z) + 2*(x**2 + y**2 + z**2)
X, Y, Z = np.mgrid[-2:2:100j, -2:2:100j, -2:2:100j]
mlab.contour3d(X, Y, Z, V)

I could solve the problem by adding我可以通过添加来解决问题

mlab.init_notebook()
mlab.clf()

after importing the mlab module.导入mlab模块后。 In this way the plot appears inside Jupyter Notebook.通过这种方式,情节出现在 Jupyter Notebook 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM