使用matlab meshgrid [英] use of matlab meshgrid

查看:82
本文介绍了使用matlab meshgrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我必须继续解决网格不规则排列的数据的问题.对于如何将数据网格化为常规网格的问题,我似乎看不到任何明确的答案,对我来说,软件文档对那些已经知道的人来说也很好. 我有29点的x,y,z数据,标题为"Lon Lat Z".我要用这些数据绘制轮廓:

Sorry i have to keep coming back to the problem gridding irregularly spaced data. I do not seem to see any clear responses to questions of how to grid data to a regular grid and the software documentation to me is good for those who already know. I have x, y, z data on 29 points, with a header "Lon Lat Z". to plot contours with this data here is what I do:

  1. 读完数据后,制作一个300 x 300点的常规网格进行插值

  1. After reading in the data, make a 300 by 300 point regular grid onto which to interpolate

numcols, numrows = 300, 300
xi = np.linspace(data.Lon.min(), data.Lon.max(), numcols)
yi = np.linspace(data.Lat.min(), data.Lat.max(), numcols)
xi, yi = np.meshgrid(xi, yi)

此时打印xi和yi会根据我的数据给我x和y,并插值300x300点.

Print xi and print yi at this point gives me x and y according to my data, interpolated over 300x300 points.

将数据插值到上面创建的网格上

Interpolate the data over the grid created above

x, y, z = data.Lon.values, data.Lat.values, data.Z.values
zi = griddata(x, y, z, xi, yi)

在这一点上,如果我做print zi我会得到

At this point if I do print zi I get

[[---...,---]
[---...,---]
[---...,---]
...,
[---...,---]
[---...,---]
[---...,---]]

[[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]
...,
[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]]

我期望看到插值z的值.我也有一个定义为轮廓覆盖的地图对象.绘图功能为我提供了具有正确轮廓值的底图和轮廓单独图形. 我的问题是为什么我会得到轮廓的空白值,以及如何正确绘制它们? 为了完整起见,这是我的绘图功能

I was expecting to see values of interpolated z. I also have a map object defined to be overlaid by contours. The plotting function gives me separate figures, for the basemap and for the contours, with the correct contour values. My question is why am I getting blank values for contours and how come they are plotted correctly? For completeness here is my plotting function

fig=plt.figure(figsize=(8,4.5))
im = plt.contourf(xi, yi, zi)
plt.show()

出现两个图(一个底图和一个等高线并排)

Two plots come up (a base map and contours side by side)

请帮忙.

推荐答案

以下方法应该起作用:

numcols, numrows = 300, 300
xi = np.linspace(data.Lon.min(), data.Lon.max(), numrows)
yi = np.linspace(data.Lat.min(), data.Lat.max(), numcols)
xi, yi = np.meshgrid(xi, yi)

x, y, z = data.Lon.values, data.Lat.values, data.Z.values
points = np.vstack((x,y)).T
values = z
wanted = (xi, yi)
zi = griddata(points, values, wanted)

最后一行是griddata的工作方式(假设您使用scipy.interpolate.griddata?) 您遇到的问题是,您似乎给了griddata五个参数,而如果我查看

So that last line is how griddata works (assuming you use scipy.interpolate.griddata?) The problem you have is that you seem to give griddata five arguments, while if i look at http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html#scipy.interpolate.griddata It says the following:

scipy.interpolate.griddata(points, values, xi, method='linear', fill_value=nan)

因此,在您的情况下,我想给出五个参数是错误的地方(请确认如果这样操作是否可行,因为我没有您的数据,所以我无法测试它是否给出正确的结果).

So in your case, giving five arguments is where it goes wrong I guess (confirm if it works if you do it like this, since I don't have your data so I can't test if it gives the correct result).

因此,在您的情况下,已知值的点是x,而该点处的已知值是y值,而您想知道它们的点是z值.不知道method='linear'如何处理您的论点,您输入的fill_value也是不好的,因此您应该只给出正确的输入(我认为这是我制定它们的方式是正确的),然后它应该起作用是的.

So in your case, your points where the values are known are the x, while the values which are known at that point are the y-values, and the points where you want to know them are at the z-values. No idea how the method='linear' copes with your argument, and the fill_value you give in is bad too, so you should just give the right inputs (which I think are correct the way I formulated them), and then it should work right.

以txt格式读取数据,并编写以下代码.您可以运行它来查看是否是您想要的结果吗?

edit: read in your data as a txt, and wrote the following code. Can you run it to see if that is the result you wanted?

import numpy as np
from scipy.interpolate import griddata
class d():
    def __init__(self):
        A0 = open("test.txt","rb") # i just copypasted your data into a txt (without first row), and reading it in in this class, so that the names are the same as yours
        A1 = A0.readlines()
        A = np.zeros((len(A1),3))
        for i, l in enumerate(A1):
            li = l.split()
            A[i,0] = float(li[0])
            A[i,1] = float(li[1])
            A[i,2] = float(li[2])
        self.Lon = A[:,0]
        self.Lat = A[:,1]
        self.Z = A[:,2]

data = d()
numcols, numrows = 30, 30
xi = np.linspace(data.Lon.min(), data.Lon.max(), numrows)
yi = np.linspace(data.Lat.min(), data.Lat.max(), numcols)
xi, yi = np.meshgrid(xi, yi)

x, y, z = data.Lon, data.Lat, data.Z
points = np.vstack((x,y)).T
values = z
wanted = (xi, yi)
zi = griddata(points, values, wanted)
import pylab as plt
fig = plt.figure(0, figsize=(8,4.5))
im = plt.contourf(xi, yi, zi)
plt.colorbar()
fig2 = plt.figure(1, figsize=(8,4.5))
im = plt.scatter(xi, yi, c= zi)
plt.colorbar()
plt.show()

这篇关于使用matlab meshgrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆