简体   繁体   English

Python 中二维 Numpy 数组的等值线图

[英]Contour plot of 2D Numpy array in Python

my aim is to get a contour plot in Python for an (100,100) array imported and created with Fortran.我的目标是在 Python 中为使用 Fortran 导入和创建的 (100,100) 数组获取等高线图。

I imported the array from Fortran in the following way:我通过以下方式从 Fortran 导入数组:

x=np.linspace(0.02,10,100), y=np.linspace(0.47,4,100) x=np.linspace(0.02,10,100), y=np.linspace(0.47,4,100)

f = (np.fromfile(('/path/result.dat'
), dtype=np.float64).reshape((len(x), len(y)), order="F"))

So the result is dependent from x and y and gives a value for every combination of x and y.因此,结果取决于 x 和 y,并为 x 和 y 的每个组合给出一个值。 How can I create a corresponding contour plot?如何创建相应的等高线图? So far what I tried was:到目前为止,我尝试的是:

X, Y= np.meshgrid(x, y)


plt.contourf(X, Y, f, colors='black')
plt.show()

But the resulting contour plot shows values that dont make sense.但是生成的等值线图显示的值没有意义。 I also tried imshow() but it did not work.我也试过 imshow() 但它没有用。 If you could help me, I will be very grateful!如果你能帮助我,我将不胜感激!

The arrangement of X , Y , and f plays a role here. XYf的排列在这里起作用。 Without looking at how the result.dat was generated, though, it is difficult to answer this question.但是,如果不查看result.dat是如何生成的,就很难回答这个问题。 Intuition tells me, the values of f(x,y) may not match with the meshgrid.直觉告诉我, f(x,y)的值可能与网格不匹配。

The improper values might be arising because the values of X and Y don't correspond to the values of f .可能会出现不正确的值,因为XY的值不对应于f的值。 Try order = "C" or order = "A" .尝试order = "C"order = "A" Also, your x and y should really be defined before reshaping the data.此外,您的xy应该在重塑数据之前真正定义。

x=np.linspace(0.02,10,100)
y=np.linspace(0.47,4,100)
f = np.fromfile(('/path/result.dat'), dtype=np.float64).reshape((len(x), len(y)), order="<>")

Maybe try reordering X and Y if this doesn't work.如果这不起作用,也许可以尝试重新排序XY

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

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