问题描述
以下最小代码段在n=29
时失败,但在n=28
或n=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上检查了代码,以确保没有以某种方式弄乱我的库。
知道为什么会这样吗?
1楼
这是由于matplotlib 2.2.x系列中的索引错误。 使用在matplotlib 3.x中解决了。 我创建的bug的github问题在 。 该补丁可能会反向移植到2.2.x