Pandas 彩图

Pandas color map

我尝试在 pandas 上制作顺序色图。这是我的结果,我想做颜色图。

      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'

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

您可以像这样将您的数据框转换为整数(第一部分将“-”替换为 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