I assume that N_z_values = N_x_values * N_y_values
. If there are the same number of values on each line, then you should be able to parse all the data containing lines at once, then split it up according to N_x_values
.
For example, if N_x_values = 27
and N_y_values = 28
and there are 5 values per line, then you could do
import numpy as np
data = []
N_x_values, N_y_values = 0, 0
with open(file_name, 'r') as in_file:
# skip 2 lines, grab N_X, grab N_Y
[in_file.next() for _ in range(2)]
N_x_values = int(in_file.next().strip())
N_y_values = int(in_file.next().strip())
for line in in_file:
line = line.strip().split(' ')
data.append(map(float, line))
data = np.array(data)
data = data.reshape(np.prod(data.shape))
x_cutoff = N_x_values
y_cutoff = N_y_values
x = data[:x_cutoff]
y = data[x_cutoff:y_cutoff]
z = data[y_cutoff:].reshape(N_x_values, N_y_values)
Now your values are in arrays of the form
x.shape = (27, )
y.shape = (28, )
z.shape = (27, 28)
matplotlib.pyplot.contour
can take these values directly. For example
import matplotlib.pyplot as plt
f = plt.figure()
ax = f.add_suplot(111)
ax.contourf(x, y, z)
ax.colorbar()
plt.show()