spec3
spec3

Reputation: 501

using matplotlib streamplot

I have problem using 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.

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.

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! I tried to invert the order of vectors but it is unuseful! the arrows change sign with x, not with z Does anyone of you understand the problem? Thanks

Gabriele

Upvotes: 1

Views: 1241

Answers (1)

spec3
spec3

Reputation: 501

one friend told me

The documentation for 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. I think your u and v are transposed.

so I used numpy.transpose( ) and everything worked!

Upvotes: 2

Related Questions