Python / Pyplot:绘制给定 (X,Y) 处的二维数据

Python / Pyplot: Plotting 2D-Data at given (X,Y)

我有以下类型的一些数据: grid = np.array([posx, posy]) 其中posx和posy是X/Y位置的一部分,存放在另一个数组中。 (转置的)网格可能看起来像:

grid = np.array([posx, posy])
print grid.T  
[[   2.47685286    2.51629155]
[   2.47685286    8.51629155]
[   2.47685286   14.51629155]
[   8.47685286    5.51629155]
[   8.47685286   11.51629155]
[  14.47685286    2.51629155]
[  14.47685286    8.51629155]
[  14.47685286   14.51629155]]

尤其是每个 "row" 中的 y 位置都不相同,点数也不同,我认为这是我的问题之一。

此外,相应的数据存储在另一个(1D-)数组中,如 data = [2.3 4.7 -0.3 .....] 具有与我的点数相同的条目数。 我的目标是将这些数据绘制成一种平滑的热图,通过颜色显示高/低值的位置。到目前为止我用过:

import numpy as np
import matplotlib.pyplot as p
p.imshow(data, interpolation=None)
p.colorbar()
p.show()

显然我的问题是我需要调整点的位置。 我搜索了其他一些 posts,但使用这种数据形式它从未解决过。 我也尝试通过简单地重塑数据来调整它,但是由于点数不规则,这没有用

因为我是新来的,我也很高兴收到关于如何改进我的 post 的评论(需要更多的输入等) 提前致谢!

我认为您的问题可以通过创建常规二维矩阵然后使用 scipy.interpolate 在数据点之间插入数据来解决。可以在以下位置找到示例:http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html#id1

这个问题有几种解决方法。

如果您只想将点显示为某种大小的标记,颜色取决于 z 数组中的值,那么 散点图 会做得很好。但是,如果点之间的 space 也应该着色,则应使用 插值和等高线 。幸运的是,这些东西也在 matplotlib 中实现了不规则 spaced 数据(“unstructured grid”上的数据),这就是你所拥有的,因为点不能轻易映射到规则网格(尽管在你给出的这个小例子,似乎确实存在等长三角形的趋势。

以下 3 个示例说明了您可能想要进一步研究的函数:plt.scatterplt.tripcolorplt.tricontourf。我把数据集弄大了一点,这样你就可以感受到 z.

表示的函数
x,y = (2*np.random.rand(50)-1 for _ in range(2))
z = np.exp(-x*x - y*y) - np.cos(x)  # turtle-surface around origin
f, ax = plt.subplots(1,3, sharex=True, sharey=True, num=2, subplot_kw={'xlim': (-1,1), 'ylim': (-1, 1)})

ax[0].scatter(x,y, s=500*(z-z.min()), c=z, cmap='hot') # scatterplot with variable-sized markers and colors
ax[1].tripcolor(x, y, z, cmap='hot') # creates a tesselation and colors the formed triangles based on the values in the 3 nodes
ax[2].tricontourf(x, y, z, cmap='hot') # estimates the underlying surface

for indx in (1,2):
    ax[indx].triplot(x,y, 'ko ') # add the locations of the points
for axes in ax: # turn off the needless clutter
    axes.tick_params(axis='both', which='both', bottom='off', left='off', labelbottom='off', labelleft='off')
ax[0].set_title('scatter')
ax[1].set_title('tripcolor')
ax[2].set_title('tricontourf')