gpt4 book ai didi

python - matplotlib 特定数量的网格点的流图错误

转载 作者:太空宇宙 更新时间:2023-11-03 21:47:48 26 4
gpt4 key购买 nike

以下最小代码片段在 n=29 时失败,但在 n=28n=30 时有效。

import numpy
import matplotlib.pyplot as plt
x = np.linspace(0,1,n)
plt.streamplot(x,x,np.meshgrid(x,x)[0],np.meshgrid(x,x)[1])
plt.show()

n=29时给出的错误

---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
~/.local/lib/python3.6/site-packages/matplotlib/streamplot.py in _integrate_rk12(x0, y0, dmap, f, maxlength)
526 try:
--> 527 k1x, k1y = f(xi, yi)
528 k2x, k2y = f(xi + ds * k1x,

~/.local/lib/python3.6/site-packages/matplotlib/streamplot.py in backward_time(xi, yi)
434 def backward_time(xi, yi):
--> 435 dxi, dyi = forward_time(xi, yi)
436 return -dxi, -dyi

~/.local/lib/python3.6/site-packages/matplotlib/streamplot.py in forward_time(xi, yi)
425 def forward_time(xi, yi):
--> 426 ds_dt = interpgrid(speed, xi, yi)
427 if ds_dt == 0:

~/.local/lib/python3.6/site-packages/matplotlib/streamplot.py in interpgrid(a, xi, yi)
619 a00 = a[y, x]
--> 620 a01 = a[y, xn]
621 a10 = a[yn, x]

~/.local/lib/python3.6/site-packages/numpy/ma/core.py in __getitem__(self, indx)
3196 # So it's easier to stick to the current version
-> 3197 dout = self.data[indx]
3198 _mask = self._mask

IndexError: index 29 is out of bounds for axis 1 with size 29

这已经让我发疯有一段时间了。当 n=28 或 n=30 时生成图。我什至检查了 google collab 上的代码,以确保我没有以某种方式搞砸我的库。

Here's the link

知道为什么会发生这种情况吗?

最佳答案

这是由于 matplotlib 2.2.x 系列中的索引错误造成的。在 matplotlib 3.x 中用 this patch 解决。我创建的 bug 的 github 问题是 here 。该补丁可能会向后移植到 2.2.x

关于python - matplotlib 特定数量的网格点的流图错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52352129/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com