user1841859
user1841859

Reputation: 155

python Contour plot

I have a file which looks like this:

1237665126927237227 7.49126127875 1500 7.0
1237665126927237227 6.64062342139 1750 7.0
1237665126927237227 5.79903397289 2000 7.0
1237665126927237227 7.24807646775 1500 7.5
1237665126927237227 6.51250095795 1750 7.5
1237665126927237227 5.74908888515 2000 7.5
1237665126927237227 6.91915170741 1500 8.0
1237665126927237227 6.29638684709 1750 8.0
1237665126927237227 5.62891381033 2000 8.0
1237665126927237227 6.54437390102 1500 8.5
1237665126927237227 5.98359412299 1750 8.5
1237665126927237227 5.43512459898 2000 8.5

etc

I need to create a plot with the 3rd column as the x axis and 4th column as the y axis, with the 2nd column as a contour on it, with contour lines at 1,2,3,4 and so on.

Im am trying to do something along the lines of,

from pylab import *

ChiTable= np.loadtxt('ChiTableSingle.txt')

xlist = linspace(ChiTable[2]) 
ylist = linspace(ChiTable[3]) 
X, Y = meshgrid (xlist, ylist)

Z =partsChi[1] 

figure()
CP1 = contour(X, Y, Z)
clabel(CP1, inline=True, fontsize=10)

pl.show()

but im just getting myself totally confused by it all. Im getting an error saying z input needs to be a 2d array, which i can understrnd why as ive made X,Y into a 2d array, and z needs to be values matching up to this, but ive got no idea how id go about that.

Upvotes: 0

Views: 2034

Answers (1)

tacaswell
tacaswell

Reputation: 87366

You need to reshape your data, not use meshgrid.

Something like:

xdim = 3
ydim = 3
X = np.asarray(ChiTable[2]).reshape((xdim, ydim))
Y = np.asarray(ChiTable[3]).reshape((xdim, ydim))
Z = np.asarray(ChiTable[1]).reshape((xdim, ydim))

contour(X, Y, Z)

meshgrid takes in two 1-D arrays and gives you back a cross of them, reshape changes an array with N total number of elements into an array with the same number of elements, but shaped differently.

Upvotes: 2

Related Questions