简体   繁体   中英

Streamplot error: Cannot convert float NaN to integer

I'm trying to use the streamplot function to plot a velocity field but for some reason it is failing. Here is an original SO post about the function with an example: how to plot streamlines , when i know u and v components of velocity(numpy 2d arrays), using a plotting program in python? . The example works fine for me; however, I tried to modify the values to simplify the function and imitate initial conditions and now it no longer works.

Here's my "simplified" code:

import matplotlib.pyplot as plt
import numpy as np
from streamplot import streamplot

x = np.linspace(0, 1, 10)
y = np.linspace(0, 2, 10)
u = np.zeros((len(x), len(y)))
v = np.zeros((len(x), len(y)))
u[:,len(y)-1]=1
speed = np.sqrt(u*u + v*v)

plt.figure()
plt.subplot(121)
streamplot(x, y, u, v,density=1, INTEGRATOR='RK4', color='b')
plt.subplot(122)
streamplot(x, y, u, v, density=(1,1), INTEGRATOR='RK4', color=u,
           linewidth=5*speed/speed.max())
plt.show()

Any recommendations or help is appreciated.

I think the problem is that the density of your (x,y) grid (you've switched x and y in your initialization of u and v , by the way) is less than the density of the streamplot grid. When you set density=1 or (1,1) (they should be equivalent) then "the domain is divided into a 25x25 grid". I think that means that there is some smoothing going on if your data is nonzero in a slim enough region compared to the density of either the streamplot or your xy grid. I couldn't get it to work by increasing those densities ( density or the linspace spacing). but if you make two columns nonzero at the edge, it seems to work fine.

Seems like the streamplot function is not very robust for these cases and perhaps you should submit a bug.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 1, 10)
y = np.linspace(0, 2, 10)
u = np.zeros((y.size, x.size))
v = np.zeros((y.size, x.size))
u[:,-2:] = 1
speed = np.sqrt(u*u + v*v)

plt.figure()
plt.subplot(121)
plt.streamplot(x, y, u, v,density=1, color='b')
plt.subplot(122)
plt.streamplot(x, y, u, v, density=(1,1), color=u, linewidth=5*speed/speed.max())
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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