失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python绘制等值线图_Python/Matplotlib双线性插值等值线图

python绘制等值线图_Python/Matplotlib双线性插值等值线图

时间:2019-12-22 22:32:22

相关推荐

python绘制等值线图_Python/Matplotlib双线性插值等值线图

我想绘制一个二维数据图,其中的值是由双线性插值确定的。作为初步测试,我决定只使用维基百科页面上的示例:

/wiki/File:Bilininterp.png

但是,一般来说,我需要这种方法来处理任何一组数据,所以一个只适用于这种特殊情况的解决方案是没有用的。下面的数据中有一个稍微复杂的例子,注释为“另一个例子”。在

以下是我迄今为止所做的几次尝试,并有评论提到了它为什么不起作用:import matplotlib

import numpy as np

import matplotlib.cm as cm

import matplotlib.pyplot as plt

from scipy.interpolate import griddata

# /wiki/File:Bilininterp.png

xi = np.array([0.0, 1.0])

yi = np.array([0.0, 1.0])

zi = np.array([[0.0, 1.0], [1.0, 0.5]])

# Another example

#xi = np.array([0.0, 0.25, 1.0])

#yi = np.array([0.0, 0.75, 1.0])

#zi = np.array([[0.0, 0.5, 1.0], [0.5, 0.7, 0.5], [1.0, 1.0, 1.0]])

# I want 20 "levels" to be shown

contour_breaks = 20

ticks = np.linspace(zi.min(), zi.max(), contour_breaks, endpoint=True)

# Attempt #1 (contour does not use bilinear interpolation)

fig = plt.figure()

axes = fig.add_subplot(111, aspect='equal')

axes.contour(xi, yi, zi, ticks[1:-1], colors='k')

fill = axes.contourf(xi, yi, zi, ticks, cmap=cm.jet)

fig.colorbar(fill, ticks=ticks)

# Attempt 2 (colors are weird for imshow -- they don't seem to be jet. I can't

# make it use ticks to make constant color zones/levels. The contour

# lines are the same as before (no bilinear). Also, you cannot input

# xi and yi, so the data would have to be interpolated to a regular

# grid - see the second set of example data above for an example

# where the data isn't regularly spaced)

fig = plt.figure()

axes = fig.add_subplot(111, aspect='equal')

axes.contour(xi, yi, zi, ticks[1:-1], colors='k')

fill = axes.imshow(zi, interpolation='bilinear', cmap=cm.jet,

extent=(0.,1.,0.,1.))

fig.colorbar(fill, ticks=ticks)

# Attempt 3 (griddata doens't do bilinear interpolation)

fig = plt.figure()

axes = fig.add_subplot(111, aspect='equal')

xi1, yi1 = np.meshgrid(xi, yi)

xi1 = xi1.flatten()

yi1 = yi1.flatten()

zi1 = zi.flatten()

xi2 = np.linspace(0., 1., 100)

yi2 = np.linspace(0., 1., 100)

zi2 = griddata((xi1, yi1), zi1, (xi2[None,:], yi2[:,None]), method='linear')

axes.contour(xi2, yi2, zi2, ticks[1:-1], colors='k')

fill = axes.contourf(xi2, yi2, zi2, ticks, cmap=cm.jet)

fig.colorbar(fill, ticks=ticks)

# Show the plots

plt.show()

如果觉得《python绘制等值线图_Python/Matplotlib双线性插值等值线图》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。