如何使用Matplotlib可视化标量2D数据

如何使用Matplotlib可视化标量2D数据

本文介绍了如何使用Matplotlib可视化标量2D数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个网格网格(矩阵X和Y)以及标量数据(矩阵Z),我需要对此进行可视化.优选地,一些2D图像在其点处具有颜色的颜色显示那里的Z的值.我做了一些研究,但没有发现任何能完全满足我想要的东西.

pyplot.imshow(Z)外观不错,但是它不使用我的X和Y矩阵,因此轴是错误的,并且无法处理X和Y给定的非线性间隔点.>

pyplot.pcolor(X,Y,Z) 制作彩色方块,其颜色对应于其角落之一的数据,因此它有点歪曲数据(它应该在其中心显示数据或其他东西).此外,它会忽略数据矩阵中的两个边.

我很确定Matplotlib中一定存在某种更好的方法,但是文档很难获得概述.所以我问别人是否知道更好的方法.如果它允许我刷新矩阵 Z 以制作动画,则奖励.

解决方案

这看起来不错,但是效率很低:

from pylab import *原点 = '低'增量= 0.025x = y = arange(-3.0, 3.01, delta)X,Y =网格(x,y)Z1 = bivariate_normal(X,Y,1.0,1.0,0.0,0.0)Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)Z = 10 *(Z1-Z2)nr, nc = Z.shapeCS = 轮廓 f(X,Y,Z,级别 = linspace(Z.min(), Z.max(), len(x)),ls ='-',cmap=cm.bone,起源=起源)CS1 = 轮廓(CS,级别= linspace(Z.min(),Z.max(),len(x)),ls = '-',cmap = cm.bone,起源=起源)表演()

是我,我将数据重新插值(使用scipy.interpolate)到常规网格中,并使用imshow()设置范围以固定轴.

编辑(每条评论):

动画等高线图可以像这样完成,但是,就像我说的,上面只是简单地滥用了等高线图功能,效率低下.做你想做的最有效的方法是使用 SciPy.你已经安装了吗?

  import matplotlibmatplotlib.use('TkAgg') # 在导入 pylab 之前执行此操作导入时间导入matplotlib.pyplot作为plt无花果= plt.figure()ax = fig.add_subplot(111)定义动画():origin ='lower'增量= 0.025x = y = arange(-3.0, 3.01, delta)X,Y =网格(x,y)Z1 = bivariate_normal(X,Y,1.0,1.0,0.0,0.0)Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)Z = 10 * (Z1 - Z2)CS1 = ax.contourf(X, Y, Z,级别= linspace(Z.min(),Z.max(),10),cmap = cm.bone,原点=原点)对于范围内的我(10):tempCS1 = 轮廓 f(X, Y, Z,级别 = linspace(Z.min(), Z.max(), 10),cmap = cm.bone,原点=原点)del tempCS1fig.canvas.draw()time.sleep(0.1)Z += x/10赢 = fig.canvas.manager.windowafter.canvas.manager.window.after(100,动画)plt.show()

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.

解决方案

This looks nice, but it's inefficient:

from pylab import *
origin = 'lower'

delta = 0.025

x = y = arange(-3.0, 3.01, delta)
X, Y = meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 10 * (Z1 - Z2)

nr, nc = Z.shape

CS = contourf(
    X, Y, Z,
    levels = linspace(Z.min(), Z.max(), len(x)),
    ls = '-',
    cmap=cm.bone,
    origin=origin)

CS1 = contour(
    CS,
    levels = linspace(Z.min(), Z.max(), len(x)),
    ls = '-',
    cmap=cm.bone,
    origin=origin)

show()

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?

import matplotlib
matplotlib.use('TkAgg') # do this before importing pylab
import time
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

def animate():
    origin = 'lower'
    delta = 0.025

    x = y = arange(-3.0, 3.01, delta)
    X, Y = meshgrid(x, y)
    Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
    Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
    Z = 10 * (Z1 - Z2)

    CS1 = ax.contourf(
        X, Y, Z,
        levels = linspace(Z.min(), Z.max(), 10),
        cmap=cm.bone,
        origin=origin)

    for i in range(10):
        tempCS1 = contourf(
            X, Y, Z,
            levels = linspace(Z.min(), Z.max(), 10),
            cmap=cm.bone,
            origin=origin)
        del tempCS1
        fig.canvas.draw()
        time.sleep(0.1)
        Z += x/10

win = fig.canvas.manager.window
fig.canvas.manager.window.after(100, animate)
plt.show()

这篇关于如何使用Matplotlib可视化标量2D数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 04:05