在MatPlotLib中生成热图

0 投票
1 回答
1052 浏览
提问于 2025-04-18 15:03

我一直在尝试生成一个热图,跟着这里的一个热门回答:在Matplotlib中使用散点数据集生成热图

在你们的帮助下,我整理了我的数据,现在可以相对轻松地提取x和y坐标(虽然代码看起来不太美观)。

这些坐标已经很好地放入了数组中,但当我把这些数组放进热图代码里时,输出结果却是这样的:

enter image description here

我使用的代码如下:

import csv
import string, numpy as np
import numpy as np
import numpy.random
import matplotlib.pyplot as plt
import pickle
import operator as oper
import re
from math import sqrt
import re # used here to remove non-numeric values



file = open('P2E2D_Long_Format.csv', 'rb')

reader1 = csv.reader(file, delimiter = '\t')
non_decimal = re.compile(r'[^\d.]+')

x = []
y = []

count = 0 

for row in reader1:
    #print row
    if 'SCL' in row[0] and count <= 5000:
        #print row[0]
        if len(row[2]) and len(row[3]) > 0: #ensures that x and y CoOr are not empty.
            if '   .' not in row[2] and 'SAMPLES' not in row[3]:
                #populates list with values in row 2 & 3            
                xCoOr = row[2]
                yCoOr = row[3]

                #this code removes the blank spaces before numbers
                x1 = non_decimal.sub('', xCoOr)
                y1 = non_decimal.sub('', yCoOr)
                print x1, 'XCoOr'
                print y1, 'YCoOr'


                #changes values from file, that are seen as string, to a float            
                x1 = float(x1)
                y1 = float(y1)
                #print x1            
                x = x + [x1]
                y = y + [y1]
                count = count + 1
                print count



myarrayx = np.asarray(x)
myarrayy = np.asarray(y)

myarrayx = myarrayx - 508
myarrayy = myarrayy - 384

myarrayx = myarrayx / 100
myarrayy = myarrayy / 100


heatmap, xedges, yedges = np.histogram2d(myarrayx, myarrayy, bins=50)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
plt.clf()
plt.imshow(heatmap, extent=extent)
plt.show()

file.close()   # <---IMPORTANT
#Long_Format.close()
#textFile.close()

row[0]是试验ID,row2是x坐标,row[3]是y坐标。

从上面的输出图来看,y轴似乎完全消失了。

有没有人有什么想法?

这是我正在处理的一些数据:

"Trial ID   Frame ID                    X-CoOr  Y-CoOr  Time"


"SCL    5413917 PreBeep1_1st_Sketchpad  653.8   542.1   4844"

"SCL    5413917 PreBeep1_1st_Sketchpad  654.7   542.2   4847"

"SCL    5413919 order of frames:        655.5   541.9   4849"

"SCL    5413919 order of frames:        656.1   541.2   4851"

"SCL    5413921 crosshair               655.8   540.8   4851"

"SCL    5413921 crosshair               655.7   540.6   4847"

"SCL    5413923 sketchpad               655.7   540.6   4843"

"SCL    5413923 sketchpad               655.5   540.6   4838"

"SCL    5413925 sketchpad               655.3   540.7   4838"

"SCL    5413925 sketchpad               655.1   540     4833"

"SCL    5413927 sketchpad               655.3   538.9   4829"

"SCL    5413927 sketchpad               655.4   538.1   4825"

"SCL    5413929 buffer1                 655.6   537.8   4824"

"SCL    5413929 buffer1                 655.5   537.5   4824"

"SCL    5413931 Diode1                  655.2   537.3   4824"

"SCL    5413931 Diode1                  654.9   537.6   4831"

"SCL    5413931 Diode1                  654.9   538.1   4836"

"SCL    5413931 Diode1                  654.8   538.6   4841"

"SCL    5413931 Diode1                  654.8   539 4841"

"SCL    5413931 Diode1                  655.6   539.1   4841"

编辑:我手动调整了图形窗口中x轴和y轴的最小值和最大值,如下所示:

enter image description here

可以看到,x轴的最大值非常大。我不太明白这是为什么。我怀疑这个值一定出现在我的数据的xCoOr列中。

不过,即使我调整了最大值和最小值,热图还是空白的。

感谢你们的时间。

1 个回答

2

我的同事解决了这个问题。

问题出在宽高比上。

我首先把所有x或y的值超过2000的都过滤掉了(因为数据中有一些巨大的异常值)。

然后,我的同事写了以下代码:

plt.imshow(heatmap, extent=extent, aspect='auto')

关键是他添加了

aspect='auto'

这让x轴的长度拉伸到和y轴一样,从而让所有内容都更清晰可见。

修正后的输出:

enter image description here

感谢所有考虑这个问题的人。

撰写回答