如何使用 Matplotlib 可视化标量二维数据?
所以我有一个网格网格(矩阵 X 和 Y)以及标量数据(矩阵 Z),我需要将其可视化。最好是一些 2D 图像,在各点处带有颜色,显示 Z 值。 我做了一些研究,但没有找到任何能完全满足我想要的效果的东西。
pyplot.imshow(Z) 看起来不错,但它不接受我的 X 和 Y 矩阵,因此轴是错误的,并且无法处理 X 和 Y 给出的非线性间隔点。
pyplot.pcolor(X ,Y,Z) 制作彩色方块,其颜色对应于其一角的数据,因此它有点歪曲数据(它应该在其中心或其他位置显示数据)。此外,它还忽略数据矩阵中的两条边。
我很确定 Matplotlib 中一定存在一些更好的方法,但文档使得很难获得概述。所以我想问其他人是否知道更好的方法。如果它允许我刷新矩阵 Z 来制作动画,那就太好了。
So i have a meshgrid (matrices X and Y) together with scalar data (matrix Z), and i need to visualize this. Preferably some 2D image with colors at the points showing the value of Z there.
I've done some research but haven't found anything which does exactly what i want.
pyplot.imshow(Z) has a good look, but it doesn't take my X and Y matrices, so the axes are wrong and it is unable to handle non-linearly spaced points given by X and Y.
pyplot.pcolor(X,Y,Z) makes colored squares with colors corresponding to the data at one of its corners, so it kind of misrepresents the data (it should show the data in its center or something). In addition it ignores two of the edges from the data matrix.
I pretty sure there must exist some better way somewhere in Matplotlib, but the documentation makes it hard to get an overview. So i'm asking if someone else knows of a better way. Bonus if it allows me to refresh the matrix Z to make an animation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这看起来不错,但效率低下:
如果是我,我会将数据重新插值(使用 scipy.interpolate)到常规网格并使用 imshow(),设置范围来固定轴。
编辑(根据评论):
可以像这样完成对等值线图进行动画处理,但是,就像我说的,上面的方法效率很低,只是简单地滥用等值线图功能。做你想做的事情最有效的方法是使用 SciPy。你安装了那个吗?
This looks nice, but it's inefficient:
It it were me, I'd re-interpolate (using scipy.interpolate) the data to a regular grid and use imshow(), setting the extents to fix the axes.
Edit (per comment):
Animating a contour plot can be accomplished like this, but, like I said, the above is inefficient just plain abuse of the contour plot function. The most efficient way to do what you want is to employ SciPy. Do you have that installed?
如果您的网格具有统一的间距,您可以继续使用 pcolor,但只需移动 X 和 Y 即可将数据集中在特定值而不是角落处。
您还可以使用散点图将一定大小的点显式放置在确切的 X 和 Y 点处,然后将颜色设置为 Z:
或:
或按照 Paul 的建议,使用轮廓函数之一
If your meshgrid has uniform spacing, you could continue to use
pcolor
, but just shift X and Y for the purposes of centering the data at the particular values rather than at the corners.You could also use a scatter plot to explicitly place points of some size at the exact X and Y points and then set the color to Z:
or:
or as Paul suggested, using one of the contour functions
如果有人看到这篇文章来寻找我正在寻找的内容,我采用了上面的示例并将其修改为使用带有帧输入堆栈的 imshow,而不是动态生成和使用轮廓。从形状(nBins、nBins、nBins)的 3D 图像数组开始,称为帧。
我还找到了一种更简单的方法来完成整个过程,尽管不太健壮:
请注意,这两种方法似乎都只能与
ipython --pylab=tk
一起使用,又名backend = TkAgg< /code>
感谢您提供的一切帮助。
In case anyone comes across this article looking for what I was looking for, I took the above example and modified it to use imshow with an input stack of frames, instead of generating and using contours on the fly. Starting with a 3D array of images of shape (nBins, nBins, nBins), called
frames
.I also found a much simpler way to go about this whole process, albeit less robust:
Note that both of these only seem to work with
ipython --pylab=tk
, a.k.a.backend = TkAgg
Thank you for the help with everything.
以下函数在边界处创建一半大小的框(如附图所示)。
The following function creates boxes of half the size at the boundary (as shown in the attached picture).