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

标签 python matplotlib plot

以下最小代码片段在 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/

相关文章:

python - 在 C++ 中实现 python 的 'if X in List'

python - 将数据框 reshape 为长格式 python

python - 删除轴上选定的刻度

python - 在条形图上叠加箱线图 matplotlib/python

python - 如何使用 django-import-export 导出属性值

python - FFT 乘法 Python 3.4.3

python - matplotlib, plt.show() 在不同的方法 = 没有 on_clicked

python - 仅具有高亮值的 matplotlib 条形图

r - 将 geom_point 和 geom_line 与 position_jitterdodge 结合用于两个分组因子

Matlab 图 -> Word -> PDF 结果质量很差