简体   繁体   English

将 function 值列表转换为二维数组

[英]Converting list of function values into 2d array

Suppose I have a text file whose lines contain three values (2 coordinates and the value of a function evaluated at those coordinates):假设我有一个文本文件,其行包含三个值(2 个坐标和在这些坐标处评估的 function 的值):

x y f(x, y)

Using numpy, how do I convert this data (read using genfromtxt , say) to an array of x values, an array of y values and a 2D array of function values, such that I can visualize f(x, y) using matplotlib 's imshow , for example? Using numpy, how do I convert this data (read using genfromtxt , say) to an array of x values, an array of y values and a 2D array of function values, such that I can visualize f(x, y) using matplotlib ' s imshow ,例如?

Edit: Easily done by using panda 's pivot_table .编辑:使用pandapivot_table轻松完成。

This code will get you started.此代码将帮助您入门。 It will grab the input from file one line at a time, and parse the values from each line.它将一次从文件中获取一行输入,并解析每一行的值。 You can then do what you want with the values (append them to a list, for example, then convert the list to a numpy data structure)然后,您可以对这些值执行您想要的操作(例如,将它们附加到列表中,然后将列表转换为numpy数据结构)

with open(filename) as file:
  for line in file:
    elements = line.split(" ")
    x = elements[0]
    y = elements[1]
    f_x_y = elements[2]

Good luck on your Python journey!祝您的 Python 旅程好运!

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

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