Reputation: 2050
I've been attempting to generate a heatmap by following the top answer over here: Generate a heatmap in MatPlotLib using a scatter data set
With some help from you kind folk here I've shaped my data so I can mine with relative ease (though not very pretty code) the x and y coordinates.
The coordinates are put nicely into arrays but when I plug those arrays into the heatmap code I get the following output:
The code I'm using is as follows:
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] is the Trial ID and row2 is the x CoOr and row[3] the y CoOr.
When looking at the outputted figure above it appears as though the y-axis is completely absent.
Does anyone have any ideas?
Here is some of the data I'm working with:
"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"
EDIT: I have manually manipulated the min and max values on the x and y axis in the figure window as follows:
As can be seen the x-axis max value is huge. I'm not sure why this is. I suspect this value must appear in the xCoOr column in my data.
However, even when I adjust max and min values the heatmap is blank.
Thanks for your time on this.
Upvotes: 0
Views: 1078
Reputation: 2050
My colleague solved the issue.
The problem was with the aspect ratio.
I first filtered out any x or y values above 2000 (as there was some enormous artefacts in the data).
My colleague then wrote the following code:
plt.imshow(heatmap, extent=extent, aspect='auto')
The key being he added
aspect='auto'
This stretched the x-axis to match the y-axis and thus made everything more visible.
Corrected output:
Thanks to everyone who considered the problem.
Upvotes: 2