简体   繁体   English

Python读取X和Y值并分配给Z特定的XY对

[英]Python reading X and Y values and assigning to Z specific X Y pairs

I have used python to create 2D XY plots in the past from pairs of data in a file, but now I need to create a contour plot from data I have in a file. 我过去使用python从文件中的数据对创建2D XY图,但是现在我需要从文件中的数据创建轮廓图。 The file looks like this: 该文件如下所示:

<Descriptive string>
<some "random" number>
<number of X values:nx>
<Number of Y values:ny>
X1 X2 X3 X4 X5
X6 X7 X8 X9 X10
...
... Xnx
Y1 Y2 Y3 Y4 Y5
Y6 Y7 Y8 Y9 Y10
...
... Yny
Z(X1,Y1) Z(X1,Y2) Z(X1,Y3) Z(X1,Y4) Z(X1,Y5)
Z(X1,Y6) Z(X1,Y7) Z(X1,Y8) Z(X1,Y9) Z(X1,Y10)
...
... 
Z(X1,Yny) Z(X2,Y1) Z(X2,Y3) Z(X2,Y4) Z(X2,Y5)
...
...
Z(X2,Yny) ...
...
...
Z(Xnx,Yny)

So far I've been able to read the values of X and Y although probably not in the most convenient form, even the values of Z, but then I am unable to assign them properly so that Z1 goes with (X1,Y1), Z2 with (X1,Y2) up to Zny going with (X1,Yny) and at the end Znx*ny going with (Xnx,Yny). 到目前为止,我已经能够读取X和Y的值,尽管可能不是最方便的形式,甚至包括Z的值,但是我无法正确地分配它们,以至于Z1与(X1,Y1)匹配, Z2与(X1,Y2)一直到Zny与(X1,Yny)一起,最后Znx * ny与(Xnx,Yny)一起。 Hope this is clear enough... So far, this is the piece of code that I have: 希望这已经足够清楚了……到目前为止,这是我拥有的一段代码:

import numpy as np

# Read from .dat file:
with open("trans_acrolein_ResWVFunAP1R12.dat", "r") as f:
    fl = f.readline()
    xnum = f.readlines()[2]
    ynum = f.readlines()[3]

# Initialize some variables to be lists.
xval = []
yval = []

# Read the values of the number of X and Y values.
for line in xnum:
    px = line.split()
    xval.append(int(px[0]))

for line in ynum:
    py = line.split()
    yval.append(int(py[0]))

linesx = np.ceil(xval/5.0)
linesy = np.ceil(yval/5.0)
linesz = np.ceil((xval*yval)/5.0)

with open("trans_acrolein_ResWVFunAP1R12.dat", "r") as f:
    for line in f:
        x = []
        y = []
        for element in line[4:3+linesx].split():
            x.append(element)
            for element in line[4+linesx:3+linesx+linesy].split():
                y.append(element)

Since I don't know how to read all the elements consecutively I used first a way of counting the number of lines that have X values, then the ones with Y values, and finally the ones with Z values, but I guess its not very efficient. 由于我不知道如何连续读取所有元素,因此我首先使用了一种计算具有X值的行数的方法,然后是具有Y值的行数,最后是具有Z值的行数,但我想它不是很高效。 I will really appreciate if someone can help me with this. 如果有人可以帮助我,我将非常感激。 Thanks, 谢谢,

I assume that N_z_values = N_x_values * N_y_values . 我假设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 . 如果每行上的值数量相同,那么您应该能够一次解析所有包含行的数据,然后根据N_x_values对其进行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 例如,如果N_x_values = 27N_y_values = 28并且每行有5个值,那么您可以

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. matplotlib.pyplot.contour可以直接获取这些值。 For example 例如

import matplotlib.pyplot as plt
f = plt.figure()
ax = f.add_suplot(111)
ax.contourf(x, y, z)
ax.colorbar()
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM