Colormap using (UV)coordinates from file

Hi all,

I have been using matplotlib for about 5 hours :-/ so please forgive me if
the question has been asked several times (and i have been trying to find it
in the forum)

But i downloaded phyton(xy) and from there i found the poormans_countour and
some pcolor scripts. I think this is what i need but how do i load a
textfile (.csv or -txt with columns etc) that contains UV-coordinates
(ordinary xy) with a assigned value and put them into the Z variables in the
.py scripts ?? i.e

from numpy import *
from pylab import load
from pylab import save
read_data = load("myfile.txt")

// here how to assign Z to read_data

cmap = cm.get_cmap('jet', 35) # 35 discrete colors

im = imshow(Z, cmap=cmap, interpolation='bilinear')
axis('off')
colorbar()

show()

The same goes for the pcolor plot, how can i replace the func3 so it contais
an array of my values from the loaded file ?

def func3(x,y):
    return (1- x/2 + x**5 + y**3)*exp(-x**2-y**2)

# make these smaller to increase the resolution
dx, dy = 0.05, 0.05

x = arange(-3.0, 3.0, dx)
y = arange(-3.0, 3.0, dy)
X,Y = meshgrid(x, y)

Z = func3(X, Y)

ax = subplot(111)
im = imshow(Z, cmap=cm.jet)
#im.set_interpolation('nearest')
#im.set_interpolation('bicubic')
im.set_interpolation('bilinear')

Thanks in advance :handshake:

/Linda

···

--
View this message in context: http://www.nabble.com/Colormap-using-(UV)coordinates-from-file-tp23572972p23572972.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

This one is from Jeff here at the forum and it does work. But the smoothing
of the color values inbetween the countour is not so good. How to improve
that ?

Also,i am still searching for a load .csv example to use with pcolor or
countour.

from matplotlib.mlab import load
import matplotlib.pyplot as plt
import numpy as np

data = load("C:/Users/Linda/data.txt")
# need to know nlons and nlats beforehand!
nlons = 8; nlats = 25
X = data[0::nlats,0]
Y = data[0:nlats,1]
# data is in nlons,nlats order in file, need to transpose
Z = data[:,2].reshape(nlons,nlats).transpose()
X,Y = np.meshgrid(X,Y)
CS = plt.contourf(X,Y,Z,20)
plt.show()

···

--
View this message in context: http://www.nabble.com/Colormap-using-(UV)coordinates-from-file-tp23572972p23573711.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Ok, but can anyone please help me with how to find nlons and nlats
dynamically.I need to do this becuase I have no idea on how many records the
datasets contains.

i tried user Timmes idea (but it aint working,not even starting):

from matplotlib.mlab import load
import matplotlib.pyplot as plt
import numpy as np

data = np.loadtxt("C:/Users/Linda/data.txt")
y=data[:,0]
x=data[:,1]
z=data[:,2]
### data is loaded from a CSV file

···

###
lats = y # data[:,0]
## lon => x
lons = x # data[:,1]
## values => z
values = z #
###
lat_uniq = list(set(lats.tolist()))
nlats = len(lat_uniq)
lon_uniq = list(set(lons.tolist()))
nlons = len(lon_uniq)
color_map = plt.cm.spectral
print lats.shape, nlats, nlons
yre = lats.reshape(nlats,nlons)
xre = lons.reshape(nlats,nlons)
zre = values.reshape(nlats,nlons)
#### later in the defined map
CT = plt.contourf(xre, yre, zre, cmap=color_map)

cbar = plt.colorbar()

plt.show()

I have also tried to use griddata but no luck

xi = np.linspace(-100,100,100)
yi = np.linspace(-100,100,100)
# grid the data.
zi = griddata(x,y,z,xi,yi)

here is the datafile

40 7 10.65251434
41 7.20 16.841345477
42 7.5 10.923153289
46 7.75 13.644313708
45 8 3.550977951
45 8.25 3.352525137
45 8.5 3.080082094
45 8.75 2.971992657
45 9 2.998723785
45 9.25 3.080082094
45 9.5 3.185687405
45 9.75 3.102075854
45 10 3.185687405
45 10.25 3.213960325
45 10.5 3.32326373
45 10.75 3.465643983
45 11 3.612980369
45 11.25 3.644313708
45 11.5 30.701277511
45 11.75 30.923153289
45 12 3.797848342
45 12.25 3.612980369
45 12.5 3.435577844
45 12.75 3.294210812
45 13 3.26536503
45.25 7 16.485050223
45.25 7.25 16.343081631
45.25 7.5 13.856783573
45.25 7.75 13.405725407
45.25 8 13.550977951
45.25 8.25 13.294210812
45.25 8.5 3.294210812
45.25 8.75 3.185687405
45.25 9 3.15761656
45.25 9.25 3.213960325
45.25 9.5 3.15761656
45.25 9.75 3.32326373
45.25 10 3.405725407
45.25 10.25 3.495925216
45.25 10.5 3.465643983
45.25 10.75 3.550977951
45.25 11 3.465643983
45.25 11.25 3.765429652
45.25 11.5 3.95669157
45.25 11.75 3.797848342
45.25 12 3.923153289
45.25 12.25 3.733239867
45.25 12.5 3.550977951
45.25 12.75 3.520306012
45.25 13 3.376085288
45.5 7 6.383367092
45.5 7.25 6.383367092
45.5 7.5 6.009422688
45.5 7.75 4.679469855
45.5 8 3.435577844
45.5 8.25 3.435577844
45.5 8.5 3.236725042
45.5 8.75 3.236725042
45.5 9 3.185687405
45.5 9.25 3.102075854
45.5 9.5 3.102075854
45.5 9.75 3.185687405
45.5 10 3.352525137
45.5 10.25 3.405725407
45.5 10.5 3.376085288
45.5 10.75 3.612980369
45.5 11 3.520306012
45.5 11.25 3.352525137
45.5 11.5 3.823949103
45.5 11.75 3.856783573
45.5 12 3.856783573
45.5 12.25 3.765429652
45.5 12.5 3.669541114
45.5 12.75 3.550977951
45.5 13 3.435577844
45.75 7 5.309043916
45.75 7.25 6.057519881
45.75 7.5 5.030958443
45.75 7.75 4.836570243
45.75 8 4.836570243
45.75 8.25 2.724965001
45.75 8.5 2.607751091
45.75 8.75 3.26536503
45.75 9 2.898163214
45.75 9.25 2.872155245
45.75 9.5 1.893252754
45.75 9.75 2.043669061
45.75 10 1.75488883
45.75 10.25 2.004264146
45.75 10.5 2.971992657
45.75 10.75 1.804949998
45.75 11 2.846334614
45.75 11.25 5.519419657
45.75 11.5 2.517818813
45.75 11.75 3.733239867
45.75 12 3.376085288
45.75 12.25 3.550977951
45.75 12.5 14.612980369
45.75 12.75 10.520306012
45.75 13 31.495925216
46 7 5.06399168
46 7.25 4.949174095
46 7.5 5.266087828
46 7.75 5.352298328
46 8 4.757472437
46 8.25 2.800325674
46 8.5 3.612980369
46 8.75 3.185687405
46 9 2.323282473
46 9.25 1.671485743
46 9.5 3.856783573
46 9.75 4.572079662
46 10 4.679469855
46 10.25 4.679469855
46 10.5 5.309043916
46 10.75 3.294210812
46 11 3.405725407
46 11.25 3.669541114
46 11.5 3.495925216
46 11.75 4.255093726
46 12 3.495925216
46 12.25 3.185687405
46 12.5 3.213960325
46 12.75 3.550977951
46 13 3.520306012
46.25 7 1.969297411
46.25 7.25 14.908706364
46.25 7.5 3.052767233
46.25 7.75 3.765429652
46.25 8 3.95669157
46.25 8.25 5.06399168
46.25 8.5 5.266087828
46.25 8.75 3.669541114
46.25 9 3.185687405
46.25 9.25 3.797848342
46.25 9.5 13.352525137
46.25 9.75 15.439709782
46.25 10 25.69098301
46.25 10.25 4.949174095
46.25 10.5 5.736883145
46.25 10.75 5.105542055
46.25 11 4.255093726
46.25 11.25 3.701277511
46.25 11.5 4.255093726
46.25 11.75 4.572079662
46.25 12 3.98369323
46.25 12.25 4.148941623
46.25 12.5 3.129746478
46.25 12.75 3.236725042
46.25 13 3.550977951
46.5 7 2.872155245
46.5 7.25 3.701277511
46.5 7.5 3.15761656
46.5 7.75 3.765429652
46.5 8 5.18951259
46.5 8.25 6.105948261
46.5 8.5 5.266087828
46.5 8.75 5.69098301
46.5 9 16.009422688
46.5 9.25 5.147381739
46.5 9.5 5.829636932
46.5 9.75 5.654489904
46.5 10 6.243327668
46.5 10.25 5.395852976
46.5 10.5 5.736883145
46.5 10.75 6.057519881
46.5 11 5.147381739
46.5 11.25 3.520306012
46.5 11.5 3.856783573
46.5 11.75 4.148941623
46.5 12 4.71833512
46.5 12.25 4.71833512
46.5 12.5 3.701277511
46.5 12.75 3.889851131
46.5 13 3.32326373
46.75 7 1.859766825
46.75 7.25 2.198852355
46.75 7.5 2.345277833
46.75 7.75 2.517818813
46.75 8 3.856783573
46.75 8.25 3.856783573
46.75 8.5 5.06399168
46.75 8.75 4.184077131
46.75 9 5.829636932
46.75 9.25 3.644313708
46.75 9.5 3.765429652
46.75 9.75 5.309043916
46.75 10 6.009422688
46.75 10.25 5.147381739
46.75 10.5 5.609155594
46.75 10.75 5.783100444
46.75 11 5.147381739
46.75 11.25 3.581868928
46.75 11.5 4.908706364
46.75 11.75 3.465643983
46.75 12 3.465643983
46.75 12.25 4.148941623
46.75 12.5 3.98369323
46.75 12.75 3.581868928
46.75 13 3.644313708

--
View this message in context: http://www.nabble.com/Colormap-using-(UV)coordinates-from-file-tp23572972p23576916.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Why dont anyone answer me???

atlest give me a hint...

Ok, so i want to use the my csv as input and then plot the contour. I have
the follwing code but gives

error:list object has no attribute reshape

is using list diffrent than using numpys loadtext and then into variables
???

from matplotlib.mlab import load
import matplotlib.pyplot as plt
import numpy as np
import csv

portfolio = csv.reader(open("file.csv", "rb"))
portfolio_list = []
portfolio_list.extend(portfolio)
lats = []
lons = []
values=[]

for data in portfolio_list:
    lats.append(data[0])
    lons.append(data[1])
    values.append(data[2])
print lats
print lons

nlats = len(lats)
nlons = len(lons)

yre = lats.reshape(nlats,nlons)
xre = lons.reshape(nlats,nlons)
zre = values.reshape(nlats,nlons)
#### later in the defined map
CT = plt.contourf(xre, yre, zre, cmap=color_map)

cbar = plt.colorbar()

plt.show()

···

--
View this message in context: http://www.nabble.com/Colormap-using-(UV)coordinates-from-file-tp23572972p23583803.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

atlest give me a hint...

The hint:

error:list object has no attribute reshape

Listen to Python: all the info is there.
A list is not a numpy array.
Don't treat it like one.

However you can do:
numpy.reshape(mylist, myshape)

Alan Isaac

···

On 5/17/2009 10:21 AM Linda_swe apparently wrote:

Hi Alan,

Thanks and it works for y and x but i guess i dont know how to shapet Z
("total size of new array must be unchanged")

Overall i just dont get it how to calcuate the nlons and nlats fro Z.

yre = np.reshape(lats,nlats)
xre = np.reshape(lons,nlons)

print yre
print xre

zre = np.reshape(values,(nlats,nlons)).transpose()
CT = plt.contourf(xre, yre, zre,58,cmap=plt.cm.jet)

cbar = plt.colorbar()

plt.show()

AlanIsaac wrote:

···

On 5/17/2009 10:21 AM Linda_swe apparently wrote:

atlest give me a hint...

The hint:

error:list object has no attribute reshape

Listen to Python: all the info is there.
A list is not a numpy array.
Don't treat it like one.

However you can do:
numpy.reshape(mylist, myshape)

Alan Isaac

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables
unlimited royalty-free distribution of the report engine
for externally facing server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
View this message in context: http://www.nabble.com/Colormap-using-(UV)coordinates-from-file-tp23572972p23584266.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

("total size of new array must be unchanged")

Again, this means what it says.
Your data array is the wrong size.
You cannot for example reshape a (3,) array into a (2,2) array.

http://www.scipy.org/Numpy_Example_List_With_Doc#head-11717acafb821da646a8db6997e59b820ac8761a

You can contour any 2d array.
You can optionally supply 1d coordinates.

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.contourf

import matplotlib.pyplot as plt
import numpy as np
plt.contour(np.add.outer(np.arange(20),np.arange(20)))

<matplotlib.contour.ContourSet instance at 0x019BF4B8>

plt.show()

Alan Isaac

···

On 5/17/2009 11:11 AM Linda_swe apparently wrote: