Python matplotlib:“TypeError:图像数据无法转换为浮点数”,看起来像一个精细的矩阵
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42390451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
matplotlib: "TypeError: Image data can not convert to float" for what look like a fine matrix
提问by Mikael Fremling
I know that other people are seeing similar errors (TypeError: Image data can not convert to float, TypeError: Image data can not convert to float using matplotlib, Type Error: Image data can not convert to float) but i don't see any solution there that helps me.
我知道其他人也看到了类似的错误(TypeError: Image data can not convert to float, TypeError: Image data can not convert to float using matplotlib, Type Error: Image data can not convert to float)但我没有看到任何那里的解决方案对我有帮助。
I'm trying to populate a numpy-array with floating point data and the plot it using imshow. The data in the Y-direction (almost) a Hermite polynomial and a Gaussian envelope, whereas the X-direction is just a Gaussian envelope.
我正在尝试用浮点数据填充一个 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)
When the code reaches the impow then i get the following error message
当代码到达 imow 时,我收到以下错误消息
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
If i replace the code
如果我替换代码
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))
with simply
用简单的
AyMesh=KYmesh**n*np.exp(-np.pi*KYmesh**2)
AyMesh=AyMesh/np.max(np.abs(AyMesh))
the problem goes away!? Does anyone understand what is happening here?
问题消失了!?有谁明白这里发生了什么?
回答by Pierre de Buyl
For large values, np.math.factorial
returns a long
instead of an int
. Arrays with long
values are of dtype object
as the cannot be stored using NumPy's types. You can re-convert the final result by
对于大值,np.math.factorial
返回 along
而不是int
。带有long
值的数组是 dtype,object
因为不能使用 NumPy 的类型存储。您可以通过以下方式重新转换最终结果
WeightMesh=np.array(AyMesh*AxMesh, dtype=float)
to have a proper float array.
有一个合适的浮点数组。