Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type error in visualising pandas dataframe as heatmap

I'm trying to visualize a pandas dataframe as a heatmap, and am getting weird errors with all plotting functions I try (I tried with both the DataFrame object, and DataFrame.values array, and nothing changes). I don't understand what could be the reason for it. This is the dataframe:

       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

I tried different ways to plot it. For example, 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'

Or 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

And seaborn.heatmap gives this:

---------------------------------------------------------------------------
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''

So, it's some kind of type problem, but I can't see where it comes from. I tried putting both int and float values into the DF, but nothing changes.

like image 838
Phlya Avatar asked May 27 '15 14:05

Phlya


People also ask

What is Typeerror in pandas?

This error occurs when you attempt to plot values from a pandas DataFrame, but there are no numeric values to plot. This error typically occurs when you think a certain column in the DataFrame is numeric but it turns out to be a different data type.

What is key error in DataFrame?

Pandas KeyError occurs when we try to access some column/row label in our DataFrame that doesn't exist. Usually, this error occurs when you misspell a column/row name or include an unwanted space before or after the column/row name.


1 Answers

I figured it out after a while: Appears that the type of DF.values was set to object. It can be fixed by the following line:

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

After that everything works!

like image 112
Phlya Avatar answered Oct 11 '22 06:10

Phlya