Pandas彩色地图

2024-09-27 21:29:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着在熊猫身上做顺序颜色图。这是我的结果,我想做彩色地图。在

      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()

但它总是出错。在

^{pr2}$

Tags: importfalsedfdatalabelsasnpcolumn
1条回答
网友
1楼 · 发布于 2024-09-27 21:29:06

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

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

相关问题 更多 >

    热门问题