How to plot a density map in python?

后端 未结 2 876
囚心锁ツ
囚心锁ツ 2020-12-14 01:52

I have a .txt file containing the x,y values of regularly spaced points in a 2D map, the 3rd coordinate being the density at that point.

4.882812500000000E-0         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-14 02:11

    The comment from @HYRY is good, but a complete minimal working answer (with a picture!) is better. Using plt.pcolormesh

    import pylab as plt
    import numpy as np
    
    # Sample data
    side = np.linspace(-2,2,15)
    X,Y = np.meshgrid(side,side)
    Z = np.exp(-((X-1)**2+Y**2))
    
    # Plot the density map using nearest-neighbor interpolation
    plt.pcolormesh(X,Y,Z)
    plt.show()
    

    enter image description here

    If the data looks like your sample, numpy can load it using the command numpy.genfromtext.

提交回复
热议问题