How to visualize scalar 2D data with Matplotlib?

前端 未结 4 987
梦谈多话
梦谈多话 2021-02-08 10:17

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

4条回答
  •  隐瞒了意图╮
    2021-02-08 10:48

    The following function creates boxes of half the size at the boundary (as shown in the attached picture).

    import matplotlib.pyplot as plt
    import numpy as np
    from scipy.ndimage.filters import convolve
    
    def pcolor_all(X, Y, C, **kwargs):
        X = np.concatenate([X[0:1,:], X], axis=0)
        X = np.concatenate([X[:,0:1], X], axis=1)
    
        Y = np.concatenate([Y[0:1,:], Y], axis=0)
        Y = np.concatenate([Y[:,0:1], Y], axis=1)
    
        X = convolve(X, [[1,1],[1,1]])/4
        Y = convolve(Y, [[1,1],[1,1]])/4
    
        plt.pcolor(X, Y, C, **kwargs)
    
    X, Y = np.meshgrid(
        [-1,-0.5,0,0.5,1],
        [-2,-1,0,1,2])
    
    C = X**2-Y**2
    
    plt.figure(figsize=(4,4))
    
    pcolor_all(X, Y, C, cmap='gray')
    
    plt.savefig('plot.png')
    

提交回复
热议问题