matplotlib特定数量的网格点的streamplot错误

问题描述 投票:1回答:1

以下最小代码片段在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时产生图。我甚至检查了谷歌协作上的代码,以确保我没有搞砸我的库。

Here's the link

知道为什么会这样吗?

python matplotlib plot
1个回答
0
投票

这是因为matplotlib 2.2.x系列中的索引错误。使用this patch在matplotlib 3.x中解决。我创建的bug的github问题是here。补丁可能会被反向移植到2.2.x.

© www.soinside.com 2019 - 2024. All rights reserved.