Matplotlib contour from xyz data: griddata invalid index

后端 未结 2 774
伪装坚强ぢ
伪装坚强ぢ 2020-12-04 23:14

I\'m trying to do a contour plot using matplotlib of a file with the following format:

x1 y1 z1

x2 y2 z2

etc

I can load it with numpy.loadtxt

2条回答
  •  春和景丽
    2020-12-04 23:38

    ok, I finally found the solution to plot it. For those interested, here is the trick: use the griddata from Scipy with the 'nearest' method.

    from scipy.interpolate import griddata
    import numpy as np
    import matplotlib.pyplot as plt
    x=np.linspace(1.,10.,20)
    y=np.linspace(1.,10.,20)
    z=z = np.random.random(20)
    xi=np.linspace(1.,10.,10)
    yi=np.linspace(1.,10.,10)
    
    X,Y= np.meshgrid(xi,yi)
    Z = griddata((x, y), z, (X, Y),method='nearest')
    plt.contourf(X,Y,Z)
    

提交回复
热议问题