matplotlib pcolormesh plot from x,y,z data

后端 未结 4 562
深忆病人
深忆病人 2021-01-06 03:03

I have data in a textfile in tableform with three columns. I use np.genfromtxt to read all the columns into matplotlib as x, y, z.

I want to create a color meshplot

4条回答
  •  既然无缘
    2021-01-06 03:50

    It seems you are plotting X and Y as 2D arrays while Z is still a 1D array. Try something like:

    Znew=np.reshape(z,(len(xmesh[:,0]),len(xmesh[0,:])))
    diagram1.pcolormesh(xmesh,ymesh,Znew) 
    

    Update: Tou have a X/Y grid of size 4x4:

    x = np.genfromtxt('mesh.txt', dtype=float, delimiter=' ', usecols = (0))
    y = np.genfromtxt('mesh.txt', dtype=float, delimiter=' ', usecols = (1))
    z = np.genfromtxt('mesh.txt', dtype=float, delimiter=' ', usecols = (2))
    

    Reshape the arrays as suggestet by @Gustav Larsson and myself like this:

    Xnew=np.reshape(x,(4,4)) 
    Xnew=np.reshape(y,(4,4))
    Znew=np.reshape(z,(4,4))
    

    Which gives you three 4x4 arrays to plot using pcolormesh:

    diagram1.pcolormesh(Xnew,Ynew,Znew) 
    

提交回复
热议问题