将熊猫数据框可视化为热图时出现类型错误

Posted

技术标签:

【中文标题】将熊猫数据框可视化为热图时出现类型错误【英文标题】:Type error in visualising pandas dataframe as heatmap 【发布时间】:2015-08-09 17:55:36 【问题描述】:

我正在尝试将 pandas 数据框可视化为热图,但我尝试的所有绘图功能都出现了奇怪的错误(我尝试了 DataFrame 对象和 DataFrame.values 数组,但没有任何变化)。我不明白这可能是什么原因。这是数据框:

       1     2     3     4     5     6     7     8     9    10    11    12  \
1      0  1163   986  1105  1315  1472   844   560  1033   867   610   703   
2   1163     0  1774   803  1091   899   704   806   891   648  1082  1199   
3    986  1774     0   679   880   798  1268   931   560  1128   774   481   
4   1105   803   679     0   742   654   887   765  1113  1079   605   928   
5   1315  1091   880   742     0   924   580   658  1073  1008   719   699   
6   1472   899   798   654   924     0   619   991  1290  1002  1290   540   
7    844   704  1268   887   580   619     0   639   611   717   812   469   
8    560   806   931   765   658   991   639     0   788  1469   498   588   
9   1033   891   560  1113  1073  1290   611   788     0   665  1172   621   
10   867   648  1128  1079  1008  1002   717  1469   665     0   728  1060   
11   610  1082   774   605   719  1290   812   498  1172   728     0  1146   
12   703  1199   481   928   699   540   469   588   621  1060  1146     0   
13  1193  1065  1163   458   364   741   713   663   777   529   492   548   
14   909   902   894   369   439   768  1043   350   975  1058   630   558   
15   877   957  1303   516   604   954   403   784   987   510  1126   617   
16  1035   662   456   584   812  1049   856   856   684   358  1098   685   
17  1191   496   770  1083   417   809   635   669   912   366   336   418   
18  1118  1118   590   550   930   665   501   664   577   918   458   574   
19   429   526   324   786   658   215   308   518   522   331   540   487   
X   1333  1273   589   908   691   604  1243  1085   853   591   696   921   

      13    14    15    16    17    18   19     X  
1   1193   909   877  1035  1191  1118  429  1333  
2   1065   902   957   662   496  1118  526  1273  
3   1163   894  1303   456   770   590  324   589  
4    458   369   516   584  1083   550  786   908  
5    364   439   604   812   417   930  658   691  
6    741   768   954  1049   809   665  215   604  
7    713  1043   403   856   635   501  308  1243  
8    663   350   784   856   669   664  518  1085  
9    777   975   987   684   912   577  522   853  
10   529  1058   510   358   366   918  331   591  
11   492   630  1126  1098   336   458  540   696  
12   548   558   617   685   418   574  487   921  
13     0   807   504   672   421   446  726   847  
14   807     0   408   616   304   781  270   715  
15   504   408     0   495   331   614  239   577  
16   672   616   495     0   199   324  358   597  
17   421   304   331   199     0   622  678   303  
18   446   781   614   324   622     0  501   373  
19   726   270   239   358   678   501    0   237  
X    847   715   577   597   303   373  237     0

我尝试了不同的方法来绘制它。例如pcolor:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-16-88566b82f0f0> in <module>()
      1 inter_chr = a.make_intrachromosomal_map(BD.dataDict['26769'])
----> 2 plt.pcolor(inter_chr)
      3 plt.show()

/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.pyc in pcolor(*args, **kwargs)
   3016         ax.hold(hold)
   3017     try:
-> 3018         ret = ax.pcolor(*args, **kwargs)
   3019         draw_if_interactive()
   3020     finally:

/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_axes.pyc in pcolor(self, *args, **kwargs)
   4965         collection.set_norm(norm)
   4966         collection.set_clim(vmin, vmax)
-> 4967         collection.autoscale_None()
   4968         self.grid(False)
   4969 

/usr/local/lib/python2.7/dist-packages/matplotlib/cm.pyc in autoscale_None(self)
    333         if self._A is None:
    334             raise TypeError('You must first set_array for mappable')
--> 335         self.norm.autoscale_None(self._A)
    336         self.changed()
    337 

/usr/local/lib/python2.7/dist-packages/matplotlib/colors.pyc in autoscale_None(self, A)
    952         ' autoscale only None-valued vmin or vmax'
    953         if self.vmin is None and np.size(A) > 0:
--> 954             self.vmin = ma.min(A)
    955         if self.vmax is None and np.size(A) > 0:
    956             self.vmax = ma.max(A)

/usr/local/lib/python2.7/dist-packages/numpy/ma/core.pyc in min(obj, axis, out, fill_value)
   6025         # If obj doesn't have a min method,
   6026         # ...or if the method doesn't accept a fill_value argument
-> 6027         return asanyarray(obj).min(axis=axis, fill_value=fill_value, out=out)
   6028 min.__doc__ = MaskedArray.min.__doc__
   6029 

/usr/local/lib/python2.7/dist-packages/numpy/ma/core.pyc in min(self, axis, out, fill_value)
   5179         # No explicit output
   5180         if out is None:
-> 5181             result = self.filled(fill_value).min(axis=axis, out=out).view(type(self))
   5182             if result.ndim:
   5183                 # Set the mask

AttributeError: 'int' object has no attribute 'view'

imshow:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-1d4365bb4ce8> in <module>()
      1 inter_chr = a.make_intrachromosomal_map(BD.dataDict['26769'])
----> 2 plt.imshow(inter_chr)
      3 plt.show()

/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.pyc in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, hold, **kwargs)
   2959                         vmax=vmax, origin=origin, extent=extent, shape=shape,
   2960                         filternorm=filternorm, filterrad=filterrad,
-> 2961                         imlim=imlim, resample=resample, url=url, **kwargs)
   2962         draw_if_interactive()
   2963     finally:

/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_axes.pyc in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
   4642                        filterrad=filterrad, resample=resample, **kwargs)
   4643 
-> 4644         im.set_data(X)
   4645         im.set_alpha(alpha)
   4646         if im.get_clip_path() is None:

/usr/local/lib/python2.7/dist-packages/matplotlib/image.pyc in set_data(self, A)
    432         if (self._A.dtype != np.uint8 and
    433             not np.can_cast(self._A.dtype, np.float)):
--> 434             raise TypeError("Image data can not convert to float")
    435 
    436         if (self._A.ndim not in (2, 3) or

TypeError: Image data can not convert to float

seaborn.heatmap 给出了这个:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-7ce156cf69d4> in <module>()
      1 inter_chr = a.make_intrachromosomal_map(BD.dataDict['26769'])
----> 2 sns.heatmap(inter_chr)
      3 plt.show()

/usr/local/lib/python2.7/dist-packages/seaborn/matrix.pyc in heatmap(data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, linewidths, linecolor, cbar, cbar_kws, cbar_ax, square, ax, xticklabels, yticklabels, mask, **kwargs)
    330     plotter = _HeatMapper(data, vmin, vmax, cmap, center, robust, annot, fmt,
    331                           annot_kws, cbar, cbar_kws, xticklabels, yticklabels,
--> 332                           mask)
    333 
    334     # Add the pcolormesh kwargs here

/usr/local/lib/python2.7/dist-packages/seaborn/matrix.pyc in __init__(self, data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, cbar, cbar_kws, xticklabels, yticklabels, mask)
    145         # Determine good default values for the colormapping
    146         self._determine_cmap_params(plot_data, vmin, vmax,
--> 147                                     cmap, center, robust)
    148 
    149         # Save other attributes to the object

/usr/local/lib/python2.7/dist-packages/seaborn/matrix.pyc in _determine_cmap_params(self, plot_data, vmin, vmax, cmap, center, robust)
    159                                cmap, center, robust):
    160         """Use some heuristics to set good defaults for colorbar and range."""
--> 161         calc_data = plot_data.data[~np.isnan(plot_data.data)]
    162         if vmin is None:
    163             vmin = np.percentile(calc_data, 2) if robust else calc_data.min()

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

所以,这是某种类型问题,但我看不出它来自哪里。我尝试将intfloat 值都放入DF,但没有任何变化。

【问题讨论】:

【参考方案1】:

过了一会才弄明白:好像DF.values的类型设置为object。它可以通过以下行修复:

DF = DF[DF.columns].astype(float)  # or int

之后一切正常!

【讨论】:

【参考方案2】:

更改数据框值的类型对我不起作用。但是,这有帮助,我不知道为什么它会跳过类型问题:

    plt.figure()
    plt.pcolor(dataframe)
    plt.colorbar()
    plt.savefig("myheatmap.png")
    plt.close()

【讨论】:

【参考方案3】:

我也有同样的问题。在将我的 2D 列表转换为 DataFrame 之前,我运行了一个 for 循环,使用以下代码将每个条目转换为浮点数 ...

test = [test[i].astype(float) for i in range(len(test))]

【讨论】:

【参考方案4】:

除了@Phlya,你可以直接用dtype=np.float64初始化数据框

df = pd.Dataframe(index=[0,1,2,3], columns=['a','b','c','d'], dtype=np.float64)

【讨论】:

以上是关于将熊猫数据框可视化为热图时出现类型错误的主要内容,如果未能解决你的问题,请参考以下文章

使用日期时间绘制切片熊猫数据框时出现 KeyError

从订单簿绘制热图(熊猫数据框)

与熊猫外部合并时出现重复问题

使用熊猫数据框时出现 KeyError

绘制决策树分类器时出现交互错误,获取值数组.. 使树很难可视化

熊猫发送包含数据框的电子邮件作为可视表