大家好,又见面了,我是你们的朋友全栈君。
我正在尝试使用具有以下格式的文件的matplotlib来绘制等高线图:
x1 y1 z1
x2 y2 z2
等等
我可以用numpy.loadtxt加载它来获取向量.到目前为止,没有麻烦.
我读这个是为了学习如何绘图,并且可以通过复制粘贴来重现它,所以我确定我的安装没有错:
我知道我必须输入x和y作为矢量,z作为数组输入,这可以用griddata完成.这也是我在这个网站上找到的.
文件说:
zi = griddata(x,y,z,xi,yi) fits a surface of the form z = f*(*x, y) to the data in the (usually) nonuniformly spaced vectors (x, y, z). griddata() interpolates this surface at the points specified by (xi, yi) to produce zi. xi and yi must describe a regular grid, can be either 1D or 2D, but must be monotonically increasing.
为了这个例子,我写了这段代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as ml
x=np.linspace(1.,10.,20)
y=np.linspace(1.,10.,20)
z=np.linspace(1.,2.,20)
xi=np.linspace(1.,10.,10)
yi=np.linspace(1.,10.,10)
zi = ml.griddata(x,y,z,xi,yi)
但是,当涉及到griddata时,我收到以下错误:
IndexError:索引无效
所以,我试着修改一下doc的例子,如下所示:
from matplotlib.mlab import griddata
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-2.1,2.1,300)
y = np.linspace(-2.1,2.1,300)
z = x*np.exp(-x**2-y**2)
# define grid.
xi = np.linspace(-2.1,2.1,100)
yi = np.linspace(-2.1,2.1,200)
# grid the data.
zi = griddata(x,y,z,xi,yi,interp=’linear’)
我得到了同样的错误.我不明白出了什么问题.
谢谢你的帮助.
解决方法:
考虑:
x = np.linspace(1., 10., 20)
y = np.linspace(1., 10., 20)
z = np.linspace(1., 2., 20)
这意味着我们知道沿x = y线的某些点的z值.
从那里,
zi = ml.griddata(x,y,z,xi,yi)
要求mlab.griddata推断矩形网格中所有点的z值.
我们已经提供了很多关于z如何沿着这条线变化的信息,但没有关于z如何在垂直方向上变化的信息(远离x = y线).由于mlab.griddata拒绝猜测,因此引发了错误.
如果您的初始x,y数据更随机分布,您将获得更好的结果:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as ml
ndata = 10
ny, nx = 100, 200
xmin, xmax = 1, 10
ymin, ymax = 1, 10
# x = np.linspace(1, 10, ndata)
# y = np.linspace(1, 10, ndata)
x = np.random.randint(xmin, xmax, ndata)
y = np.random.randint(ymin, ymax, ndata)
z = np.random.random(ndata)
xi = np.linspace(xmin, xmax, nx)
yi = np.linspace(ymin, ymax, ny)
zi = ml.griddata(x, y, z, xi, yi)
plt.contour(xi, yi, zi, 15, linewidths = 0.5, colors = ‘k’)
plt.pcolormesh(xi, yi, zi, cmap = plt.get_cmap(‘rainbow’))
plt.colorbar()
plt.scatter(x, y, marker = ‘o’, c = ‘b’, s = 5, zorder = 10)
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
plt.show()
如果你想让mlab.griddata以任意方式沿着x = y线将数据外推到整个网格,你可以添加两个额外的边界点(xmin,ymax,z [0])和(xmax,ymin,z [ – 1]):
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as ml
np.random.seed(8)
ndata = 10
ny, nx = 100, 200
xmin, xmax = 1, 10
ymin, ymax = 1, 10
x = np.linspace(1, 10, ndata)
y = np.linspace(1, 10, ndata)
z = np.random.random(ndata)
x = np.r_[x,xmin,xmax]
y = np.r_[y,ymax,ymin]
z = np.r_[z,z[0],z[-1]]
xi = np.linspace(xmin, xmax, nx)
yi = np.linspace(ymin, ymax, ny)
# Requires installation of natgrid
# http://sourceforge.net/projects/matplotlib/files/matplotlib-toolkits/
zi = ml.griddata(x, y, z, xi, yi, interp=’nn’)
# Or, without natgrid:
# zi = ml.griddata(x, y, z, xi, yi, interp=’linear’)
plt.contour(xi, yi, zi, 15, linewidths = 0.5, colors = ‘k’)
plt.pcolormesh(xi, yi, zi, cmap = plt.get_cmap(‘rainbow’))
plt.colorbar()
plt.scatter(x, y, marker = ‘o’, c = ‘b’, s = 10, zorder = 10)
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
plt.show()
标签:python,matplotlib,interpolation
来源: https://codeday.me/bug/20191004/1851562.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/141297.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...