0

I have a series of x,y,z data in a csv file. I tried to make a grid data for contour plotting of them in a grid. My code looks like below:

source_data1= open('XYZ_data.csv').readlines()
firstline= source_data1.pop(0)
xCoord=[]
yCoord=[]
zCoord=[]

for line in source_data1:
    if len(line) > 1:
        X,Y,Z= line.split(',')
        xCoord.append(float(X))
        yCoord.append(float(Y))
        zCoord.append(float(Z))


x = np.linspace(minXData, maxXData, 50)
y = np.linspace(minYData, maxYData, 50)

Z= griddata((xCoord, yCoord), zCoord, (x[None,:], y[:,None]), method='linear')

Now, with the new structured grid (i.e. 50x50), I would like to have the interpolated z components in another csv file in a format like below

x,y,z
12.2344,5.3435,12.4
...

Can someone help me please?

sophros
  • 14,672
  • 11
  • 46
  • 75
Ehsan Izadi
  • 21
  • 1
  • 3
  • Does this answer your question? [Dump a NumPy array into a csv file](https://stackoverflow.com/questions/6081008/dump-a-numpy-array-into-a-csv-file) – Panagiotis Kanavos Sep 01 '20 at 08:58
  • Are you asking how to export to CSV using numpy, or how to interpolate? Those are two completely different things – Panagiotis Kanavos Sep 01 '20 at 08:58
  • 1
    There is `to_csv()` function which allows you to save DataFrame as a *CSV* file. – Pooria_T Sep 02 '20 at 07:09
  • Thanks Pooria_T. This answers my question completely. I just added two lines as below: outGrid=pd.DataFrame(Z) outGrid.to_csv("outGrid") As Z being my grid data – Ehsan Izadi Sep 11 '20 at 07:25

0 Answers0