matplotlib imshow()具有不规则间隔的数据点 [英] matplotlib imshow() with irregular spaced data points
问题描述
我正在尝试将一些数据放入imshow()图中. 我的问题是数据不是以MxN数组的形式出现,而是以3xN数组的形式出现(x和y坐标和值).这些点未按规则网格排列,而是位于[xmin,xmax,ymin和ymax] = [-pi/2,pi/2,0,3.5]之内.
I am trying to put some data into an imshow() plot. My problem is that the data does not come as a MxN array but as a 3xN array (x- and y coordinate and value). The points are NOT arranged as a regular grid but lie within [xmin,xmax,ymin and ymax]=[-pi/2,pi/2,0,3.5].
In [117]: shape(data)
Out[117]: (3L, 102906L)
如何从这些数据中获得漂亮的图像图? 非常感谢您的帮助.
How can I get a nice image plot from that data? Thank you very much for any help.
该数据表示杆表面上的温度值是轴向和方位角位置的函数,请考虑cfd网格.
btw the data represents temperature values on the surface of a rod as a function of axial and azimuthal position, think of a cfd-mesh.
推荐答案
我建议使用griddata方法进行插值.一个示例是:
I recommend using the griddata-method for interpolation. A sample would be:
import numpy as np
from matplotlib.mlab import griddata
import matplotlib.pyplot as plt
xs0 = np.random.random((1000)) * np.pi - np.pi/2
ys0 = np.random.random((1000)) * 3.5
zs0 = np.random.random((1000))
N = 30j
extent = (-np.pi/2,np.pi/2,0,3.5)
xs,ys = np.mgrid[extent[0]:extent[1]:N, extent[2]:extent[3]:N]
resampled = griddata(xs0, ys0, zs0, xs, ys)
plt.imshow(resampled.T, extent=extent)
plt.plot(xs0, ys0, "r.")
plt.plot(xs, ys, "b.")
plt.title("imshow for irregularly spaced data using griddata")
plt.show()
我想从3 * X数组到三个X数组的过渡是显而易见的.
I guess transition from your 3*X-array to three X-arrays is obvious.
结果是:
红点表示数据的原始"位置,蓝点表示现在规则排列的数据.
Red points show the "original" positions of the data, blue points for the now regularly spaced data.
griddata返回一个掩码数组.屏蔽所有无法评估插值的点,然后将其绘制为白色区域.
griddata returns a masked array. All points for which the interpolation cannot be evaluated are masked and then plotted as white areas.
HTH, 托尔斯滕
这篇关于matplotlib imshow()具有不规则间隔的数据点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!