简体   繁体   English

使用matplotlib streamplot

[英]using matplotlib streamplot

I have problem using matplotlib streamplot. 我在使用matplotlib streamplot时遇到问题。 I want to use a 3d vector field in coordinates (x,y,z) stored in a numpy array, and plot slices of it with streamplot. 我想在存储在numpy数组中的坐标(x,y,z)中使用3d矢量字段,并使用streamplot绘制其切片。

To test it I wanted to use a vector field with arrows pointed up in the z>0 region and pointed down in the z<0 region. 为了测试它,我想使用一个向量字段,其箭头在z> 0区域中向上,在z <0区域中向下。

So I tried this: 所以我尝试了这个:

import numpy as np
import matplotlib.pyplot as plt
from math import * 

max = 100
min = -100


X = np.linspace(min, max, num=100)
Y = np.linspace(min, max, num=100)
Z = np.linspace(min, max, num=100)

N = X.size

#single components in the 3D matrix

Bxa = np.zeros((N, N, N))
Bya = np.zeros((N, N, N))
Bza = np.zeros((N, N, N))


for i, x in enumerate(X):
    for j, y in enumerate(Y):
        for k, z in enumerate(Z):
            Bxa[ i, j, k] = 0.0 #x
            Bya[ i, j, k] = 0.0 #y
            Bza[ i, j, k] = z

#I take a slice close to Y=0
Bx_sec = Bxa[:,N/2,:]
By_sec = Bya[:,N/2,:]
Bz_sec = Bza[:,N/2,:]

fig = plt.figure()
ax = fig.add_subplot(111)
ax.streamplot(X, Z, Bx_sec, Bz_sec, color='b')
ax.set_xlim([X.min(), X.max()])
ax.set_ylim([Z.min(), Z.max()])


plt.show()

But I obtain something that looks like if I have put Bza = x! 但是我得到的东西看起来好像是我将Bza = x! I tried to invert the order of vectors but it is unuseful! 我试图反转向量的顺序,但是它没有用! 箭头用x而不是z更改符号 Does anyone of you understand the problem? 你们中有人知道这个问题吗? Thanks 谢谢

Gabriele 加布里埃

one friend told me 一位朋友告诉我

The documentation for streamplot: streamplot的文档:

    x, y : 1d arrays
        an *evenly spaced* grid.
    u, v : 2d arrays
        x and y-velocities. Number of rows should match length of y, and
        the number of columns should match x.

Note that the rows in u and v should match y, and the columns should match x. 请注意,u和v中的行应与y匹配,而列应与x匹配。 I think your u and v are transposed. 我认为您的u和v已转置。

so I used numpy.transpose( ) and everything worked! 因此我使用了numpy.transpose(),一切正常!

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

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