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

Python 将熊猫数据框可视化为热图时出现类型错误,python,pandas,matplotlib,seaborn,Python,Pandas,Matplotlib,Seaborn,我试图将熊猫数据框可视化为热图,但我尝试的所有绘图函数都出现了奇怪的错误(我尝试了dataframe对象和dataframe.valuesarray,但没有任何更改)。我不明白为什么会这样。这是数据帧: 1 2 3 4 5 6 7 8 9 10 11 12 \ 1 0 1163 986 1105 1315 1472 844 560 1033 867 610

我试图将熊猫数据框可视化为热图,但我尝试的所有绘图函数都出现了奇怪的错误(我尝试了dataframe对象和
dataframe.values
array,但没有任何更改)。我不明白为什么会这样。这是数据帧:

       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'
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''
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
1 inter_chr=a.make_染色体内图(BD.dataDict['26769']
---->2 sns.热图(室内温度)
3 plt.show()
/热图中的usr/local/lib/python2.7/dist-packages/seaborn/matrix.pyc(数据、vmin、vmax、cmap、center、robust、annot、fmt、annot_-kws、线宽、线色、cbar、cbar_-kws、cbar_-ax、square、ax、xticklabels、yticklabels、mask、**kwargs)
330绘图仪=_热图仪(数据、vmin、vmax、cmap、中心、稳健、annot、fmt、,
331注释、cbar、cbar、xticklabels、yticklabels、,
-->332(面罩)
333
334#在此处添加pcolormesh kwargs
/usr/local/lib/python2.7/dist-packages/seaborn/matrix.pyc in_uuuuuinit_uuuu(self、data、vmin、vmax、cmap、center、robust、annot、fmt、annot_kws、cbar、cbar_kws、xticklabels、yticklabels、mask)
145#确定颜色映射的良好默认值
146自确定cmap参数(绘图数据、vmin、vmax、,
-->147 cmap,中心,稳健)
148
149#将其他属性保存到对象
/usr/local/lib/python2.7/dist-packages/seaborn/matrix.pyc in_determine_cmap_params(self、plot_数据、vmin、vmax、cmap、center、robust)
159 cmap,中心,稳健):
160“使用一些启发式设置颜色栏和范围的良好默认值。”“”
-->161计算数据=绘图数据.data[~np.isnan(绘图数据.data)]
162如果vmin为无:
163 vmin=np.百分位数(计算数据,2),如果稳健,则为else计算数据.min()
TypeError:输入类型不支持ufunc“isnan”,并且无法根据强制转换规则“安全”将输入安全强制为任何支持的类型

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

过了一会儿,我发现
DF.values
的类型被设置为
对象。它可以通过以下行进行固定:

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

在那之后一切都正常了

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

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

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

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