python - matplotlib: "TypeError: Image data can not convert to float"看起来像一个精细矩阵

标签 python numpy matplotlib

我知道其他人看到了类似的错误(TypeError: Image data can not convert to floatTypeError: Image data can not convert to float using matplotlibType Error: Image data can not convert to float),但我没有看到任何对我有帮助的解决方案。

我正在尝试用 float 据填充一个 numpy 数组,并使用 imshow 绘制它。 Y方向的数据(几乎)是Hermite多项式和高斯包络,而X方向只是一个高斯包络。

from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt

####First we set Ne
Ne=25

###Set up a mesh with size sqrt(Ne) X sqrt(Ne)
sqrtNe=int(np.sqrt(Ne))
Ky=np.array(range(-sqrtNe,sqrtNe+1),dtype=float)
Kx=np.array(range(-sqrtNe,sqrtNe+1),dtype=float)
[KXmesh,KYmesh]=np.meshgrid(Kx,Ky,indexing='ij')

##X-direction is gussian envelope
AxMesh=np.exp(-(np.pi*KXmesh**2)/(4.0*Ne))

Nerror=21 ###This is where the error shows up
for n in range(Nerror,Ne):
    ##Y-direction is a polynomial of degree n ....
    AyMesh=0.0
    for i in range(n/2+1):
        AyMesh+=(-1)**i*(np.sqrt(2*np.pi)*2*KYmesh)**(n-2*i)/(np.math.factorial(n-2*i)*np.math.factorial(i))
    ### .... times a gaussian envelope
    AyMesh=AyMesh*np.exp(-np.pi*KYmesh**2)
    AyMesh=AyMesh/np.max(np.abs(AyMesh))
    WeightMesh=AyMesh*AxMesh
    print("n:",n)
    plt.figure()
    ####Error occurs here #####
    plt.imshow(WeightMesh,interpolation='nearest')
    plt.show(block=False)

当代码到达 impow 时,我收到以下错误消息

Traceback (most recent call last):
  File "FDOccupation_mimimal.py", line 30, in <module>
    plt.imshow(WeightMesh,interpolation='nearest')
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 3022, in imshow
    **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/__init__.py", line 1814, in inner
    return func(ax, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_axes.py", line 4947, in imshow
    im.set_data(X)
  File "/usr/lib/python2.7/dist-packages/matplotlib/image.py", line 449, in set_data
    raise TypeError("Image data can not convert to float")
TypeError: Image data can not convert to float

如果我替换代码

AyMesh=0.0
for i in range(n/2+1):
    AyMesh+=(-1)**i*(np.sqrt(2*np.pi)*2*KYmesh)**(n-2*i)/(np.math.factorial(n-2*i)*np.math.factorial(i))
### .... times a gaussian envelope
AyMesh=AyMesh*np.exp(-np.pi*KYmesh**2)
AyMesh=AyMesh/np.max(np.abs(AyMesh))

简单地

AyMesh=KYmesh**n*np.exp(-np.pi*KYmesh**2)
AyMesh=AyMesh/np.max(np.abs(AyMesh))

问题消失了!? 有谁知道这里发生了什么?

最佳答案

对于大值,np.math.factorial 返回一个 long 而不是 int。具有 long 值的数组属于 dtype object,因为不能使用 NumPy 的类型进行存储。您可以通过

重新转换最终结果
WeightMesh=np.array(AyMesh*AxMesh, dtype=float)

有一个合适的 float 组。

关于python - matplotlib: "TypeError: Image data can not convert to float"看起来像一个精细矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42390451/

相关文章:

python - unittest.main() 运行包中的所有测试模块

python - T 分布的奇怪行为

python - Matplotlib Canvas 绘图

python - 如何在 python 上制作网格(使用 matplotlib 或其他库)

python - iPython、Jupyter -> 属性错误 : 'module' object has no attribute 'cbook'

python - pandas 使用 isin 和append查找数据帧

python - 如何在 Odoo Python 中使文件可下载?

python - 我可以启动终端,让它打开 python 交互式 shell 并在 os x 中使用相同的命令运行一些 python 语句吗?

python - numpy.zeros(n) 和 numpy.zeros(n,1) 之间的区别

python - 停止迭代 : generator_output = next(output_generator)