熊猫彩色地图

时间:2015-07-27 18:49:56

标签: python pandas color-mapping

我尝试在熊猫上进行连续色彩映射。这是我的结果,我想做色彩图。

      A     G     C     T    -
A     -  5823  1997  1248  962
G  9577     -  2683  2492  788
C  2404  2574     -  9569  722
T  1272  1822  5931     -  767
-   795   583   599   559    -



df = pd.DataFrame(index= ["A", "G", "C", "T", "-"], columns=["A", "G", "C", "T", "-"])
import matplotlib.pyplot as plt
import numpy as np
column_labels = list("AGCT-")
row_labels = list("AGCT-")
data = df
fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=plt.cm.Blues)

ax.set_xticks(np.arange(data.shape[0])+0.5, minor=False)
ax.set_yticks(np.arange(data.shape[1])+0.5, minor=False)

ax.invert_yaxis()
ax.xaxis.tick_top()

ax.set_xticklabels(row_labels, minor=False)
ax.set_yticklabels(column_labels, minor=False)
plt.show()

但它一直出错。

 File "//anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)

  File "//anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
    builtins.execfile(filename, *where)

  File "/Users/macbookpro/Desktop/mutations/first.py", line 115, in <module>
    heatmap = ax.pcolor(data, cmap=plt.cm.Blues)

  File "//anaconda/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 4967, in pcolor
    collection.autoscale_None()

  File "//anaconda/lib/python2.7/site-packages/matplotlib/cm.py", line 335, in autoscale_None
    self.norm.autoscale_None(self._A)

  File "//anaconda/lib/python2.7/site-packages/matplotlib/colors.py", line 956, in autoscale_None
    self.vmax = ma.max(A)

  File "//anaconda/lib/python2.7/site-packages/numpy/ma/core.py", line 6036, in max
    return asanyarray(obj).max(axis=axis, fill_value=fill_value, out=out)

  File "//anaconda/lib/python2.7/site-packages/numpy/ma/core.py", line 5280, in max
    result = self.filled(fill_value).max(axis=axis, out=out).view(type(self))

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

1 个答案:

答案 0 :(得分:0)

问题是数据框中的' - '字符导致值存储为字符串,而不是整数。

您可以将数据帧转换为这样的整数(第一部分将' - '替换为0,第二部分将数据类型更改为int):

df = df.where(df != '-', 0).astype(int)
df

         A     G     C     T    -
A     0  5823  1997  1248  962
G  9577     0  2683  2492  788
C  2404  2574     0  9569  722
T  1272  1822  5931     0  767
-   795   583   599   559    0