简体   繁体   English

流图错误:无法将浮点NaN转换为整数

[英]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. 我正在尝试使用streamplot函数绘制速度场,但是由于某种原因它失败了。 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? 这是关于函数的原始SO帖子,并带有示例: 如何在python中使用绘制程序绘制流线,当我知道速度(numpy 2d数组)的u和v分量时? . 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. 我认为问题在于(x,y)网格的密度(x,y)顺便说一下,您在uv初始化中已经切换了xy )小于streamplot网格的密度。 When you set density=1 or (1,1) (they should be equivalent) then "the domain is divided into a 25x25 grid". 当您设置density=1(1,1) (它们应该等效)时,则“将域划分为25x25的网格”。 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. 我认为这意味着,与流图或xy网格的密度相比,如果您的数据在足够窄的区域中为非零值,则需要进行一些平滑处理。 I couldn't get it to work by increasing those densities ( density or the linspace spacing). 我无法通过增加这些密度( densitylinspace空间间距)来使其工作。 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. 似乎streamplot函数在这些情况下不是很可靠,也许您应该提交一个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()

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

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