Contour plot in Python importing txt table file

前端 未结 2 1710
情话喂你
情话喂你 2021-01-07 03:21

I am trying to make a contour plot like:

\"contour\"

Using a table of data like 3 columns in a txt file

2条回答
  •  不要未来只要你来
    2021-01-07 03:50

    The code below worked for me:

    import scipy.interpolate    
    import numpy as np  
    
    N = 500 #number of points for plotting/interpolation    
    x, y, z = np.genfromtxt(r'data.dat', unpack=True)
    xll = x.min();  xul = x.max();  yll = y.min();  yul = y.max()
    
    xi = np.linspace(xll, xul, N)
    yi = np.linspace(yll, yul, N)
    zi = scipy.interpolate.griddata((x, y), z, (xi[None,:], yi[:,None]), method='cubic')
    
    contours = plt.contour(xi, yi, zi, 6, colors='black')
    plt.clabel(contours, inline=True, fontsize=7)
    plt.imshow(zi, extent=[xll, xul, yll, yul], origin='lower', cmap=plt.cm.jet, alpha=0.9)
    plt.xlabel(r'$x$')
    plt.ylabel(r'$y$')
    plt.clim(0, 1)
    plt.colorbar()
    plt.show()
    

提交回复
热议问题