Python 是否使用matplotlib在colormap上未显示批注?

Python 是否使用matplotlib在colormap上未显示批注?,python,matplotlib,heatmap,colorbar,Python,Matplotlib,Heatmap,Colorbar,我正在尝试为我的Needleman-Wunsch算法创建一个热图,但是,我似乎无法标记图形的各个单元格。该图形与我希望的非常接近,但是,我只是缺少单元格中的各个值。我遵循了matplotlib的文档和示例,但是,我似乎找不到错误,我确信它很简单,但我似乎无法发现它 dna1 = [c for c in "CAAAGACCTGAAGAGCCAGTGGACTCCACCCCACTTTCTGGTCTGACCAATAT"] dna2 = [c for c in "ACCACA

我正在尝试为我的Needleman-Wunsch算法创建一个热图,但是,我似乎无法标记图形的各个单元格。该图形与我希望的非常接近,但是,我只是缺少单元格中的各个值。我遵循了matplotlib的文档和示例,但是,我似乎找不到错误,我确信它很简单,但我似乎无法发现它

dna1 = [c for c in "CAAAGACCTGAAGAGCCAGTGGACTCCACCCCACTTTCTGGTCTGACCAATAT"]
dna2 = [c for c in "ACCACACTCTCTGGGCTGACCAATTACAGCGCTTCTACAGAACTGAAGACTCAC"]

fig, ax = plt.subplots(figsize=(20,20))
im = ax.imshow(raw_data, aspect='auto')

# We want to show all ticks...
ax.set_xticks(np.arange(len(dna1)))
ax.set_yticks(np.arange(len(dna2)))
# ... and label them with the respective list entries
ax.set_xticklabels(dna1)
ax.set_yticklabels(dna2)

# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
         rotation_mode="anchor")

# Gridlines based on minor ticks
plt.pcolormesh(raw_data, edgecolors='k', linewidth=1)

# Loop over data dimensions and create text annotations.

for i in range(len(dna1)):
    for j in range(len(dna2)):
        try:
            ax.text(i, j, raw_data[i, j], ha="center", va="center", color="w", fontsize = 15)
        except KeyError:
            continue
        

ax.set_title("")
plt.show()
产出如下:

原始_数据的示例:


运行以下代码

fig, ax = plt.subplots(figsize=(20,20))

Z = np.random.rand(6, 10)
x = np.arange(0.5, 11, 1)
y = np.arange(0.5, 7, 1)

plt.pcolormesh(x,y,Z, edgecolors='k', linewidth=1)

# Loop over data dimensions and create text annotations.

for i in range(10):
    for j in range(6):
        ax.text(i+1, j+1, "lol", ha="center", va="center", color="b", fontsize = 50)
plt.show()
我们得到了输出

您是否尝试过删除行
im=ax.imshow(原始数据,aspect='auto')


你能分享一个
原始数据的例子吗?@ZeroPancakes我添加了原始数据的.txt文件的屏幕截图是的,它产生的输出稍好一些,但图上的单元格仍然没有注释,还是空白的。我上面的for循环正确吗?@lambda那么问题可能是在输出格式/缩放或
for
循环中。什么数据类型是原始数据
np.array
?谢谢,我知道这是正确的,但是我已经做了您建议的更改,但是由于某些原因,它仍然没有出现在我的面前,我不明白什么可能是正确的issue@lambda能否在
行之后添加带有打印语句的lint,除了:
行?例如,
print(f'{i},{j}')
i添加了一个lint输出,它只是迭代0,1。。。。55,1...,55,55. 我认为问题在于0,0字段?出于某种原因,它给出了一个我在使用异常行时忽略的关键错误,我不确定这是否是问题所在尽管@warpedI应该添加,我的数据是一个55 x 54矩阵如果你复制并粘贴我答案中的所有代码,并执行它,它会给出我发布的图像吗?
dna1 = [c for c in "CAAAGACCTGAAGAGCCAGTGGACTCCACCCCACTTTCTGGTCTGACCAATAT"]
dna2 = [c for c in "ACCACACTCTCTGGGCTGACCAATTACAGCGCTTCTACAGAACTGAAGACTCAC"]

fig, ax = plt.subplots(figsize=(20,20))

# dummy data:
raw_data = np.random.randint(0,10,(len(dna2),len(dna1)))

# you just need the colormesh, and can dispense with the imshow.
# (using both leads to the overlapping images in your question)
plt.pcolormesh(raw_data, edgecolors='k', linewidth=1)

# We want to show all ticks... 
# ... and shift them by 0.5 to avoid ambiguity
ax.set_xticks(np.arange(0.5, len(dna1)))
ax.set_yticks(np.arange(0.5, len(dna2)))
# ... and label them with the respective list entries
ax.set_xticklabels(dna1)
ax.set_yticklabels(dna2)

# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
         rotation_mode="anchor")

# Loop over data dimensions and create text annotations.

for i in range(len(dna1)):
    for j in range(len(dna2)):
        try: # i switched the raw_data indices
            ax.text(i+.5, j+.5, raw_data[j, i], ha="center", va="center", color="w", fontsize = 15)
        except:
            continue
        

ax.set_title("")
plt.show()