Non-linear axes for imshow in matplotlib

前端 未结 2 781
孤城傲影
孤城傲影 2021-01-03 23:30

I am generating 2D arrays on log-spaced axes (for instance, the x pixel coordinates are generated using logspace(log10(0.95), log10(2.08), n).

I want to

2条回答
  •  醉梦人生
    2021-01-03 23:56

    In my view, it is better to use pcolor and regular (non-converted) x and y values. pcolor gives you more flexibility and regular x and y axis are less confusing.

    import pylab as plt
    import numpy as np
    from matplotlib.colors import LogNorm
    from matplotlib.ticker import LogFormatterMathtext
    
    x=np.logspace(1, 3, 6)
    y=np.logspace(0, 2,3)
    X,Y=np.meshgrid(x,y)
    z = np.logspace(np.log10(10), np.log10(1000), 5)
    Z=np.vstack((z,z))
    
    im = plt.pcolor(X,Y,Z, cmap='gray', norm=LogNorm())
    plt.axvline(100, color='red')
    
    plt.xscale('log')
    plt.yscale('log')
    
    plt.colorbar(im, orientation='horizontal',format=LogFormatterMathtext())
    plt.show()
    

    enter image description here

    As pcolor is slow, a faster solution is to use pcolormesh instead.

    im = plt.pcolormesh(X,Y,Z, cmap='gray', norm=LogNorm())
    

提交回复
热议问题