Contour plot in Python importing txt table file

matplotlib, python

Solution

Followup from my comment... first, I would replace all these lines:

data = np.loadtxt(r'dataa.txt')

a = [data[:,0]]
b = [data[:,1]]
n = [data[:,2]]

x = np.asarray(a)
y = np.asarray(b)
z = np.asarray(n)

With:

x, y, z = np.genfromtxt(r'dataa.txt', unpack=True)

Your original code is adding an extra axis at the front, since `[data[:,0]]` is a list of arrays with one element. The result is that `x.shape` will be `(1, N)` instead if `(N,)`. All of this can be done automatically using the last line above, or you could just use the same `data` loading and say:

x = data[:,0]
y = data[:,1]
z = data[:,2]

since those slices will give you an array back.

However, you're not quite done, because `plt.contour` expects you to give it a 2d array for `z`, not a 1d array of values. Right now, you seem to have `z` values at given `x, y` points, but `contour` expects you to give it a 2d array, like an image.

Before I can answer that, I need to know how `x` and `y` are spaced. If regularly, you can just populate an array pretty easily. If not regularly, you basically have to interpolate before you can make a contour plot.

To do the interpolation, use

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate

N = 1000 #number of points for plotting/interpolation

x, y, z = np.genfromtxt(r'dataa.txt', unpack=True)

xi = np.linspace(x.min(), x.max(), N)
yi = np.linspace(y.min(), y.max(), N)
zi = scipy.interpolate.griddata((x, y), z, (xi[None,:], yi[:,None]), method='cubic')

fig = plt.figure()
plt.contour(xi, yi, zi)
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

Problem

I am trying to make a contour plot like: Using a table of data like 3 columns in a txt file, with a long number of lines. Using this code: ``` import numpy as np import matplotlib.pyplot as plt import scipy.interpolate data = np.loadtxt(r'dataa.txt') a = [data[:,0]] b = [data[:,1]] n = [data[:,2]] x = np.asarray(a) y = np.asarray(b) z = np.asarray(n) print "x = ", x print "y = ", y print "z = ", z fig=plt.figure() CF = contour(x,y,z,colors = 'k') plt.xlabel("X") plt.ylabel("Y") plt.colorbar() plt.show() ``` I don't know why, it is not working. Python gives me the right axes for the values that I am expecting to see, but in the graph is just a blank and I know that it is importing the data in right way because it shows me my values before the plot. Example of table: (the diference is because my table has 90000 lines) Using this code: ``` import numpy as np import matplotlib.pyplot as plt import scipy.interpolate N = 1000 #number of points for plotting/interpolation x, y, z = np.genfromtxt(r'dataa.txt', unpack=True) xi = np.linspace(x.min(), x.max(), N) yi = np.linspace(y.min(), y.max(), N) zi = scipy.interpolate.griddata((x, y), z, (xi[None,:], yi[:,None]), method='cubic') fig = plt.figure() plt.contour(xi, yi, zi) plt.xlabel("X") plt.ylabel("Y") plt.show() ``` Ive got this result: I think I've got the advices wrongly.

Original source