Non-linear axes for imshow in matplotlib

前端 未结 2 798
孤城傲影
孤城傲影 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:59

    Actually, it works fine. I'm confused.

    Previously I was getting errors about "Images are not supported on non-linear axes" which is why I asked this question. But now when I try it, it works:

    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.logspace(1, 3, 5)
    y = np.linspace(0, 2, 3)
    z = np.linspace(0, 1, 4)
    Z = np.vstack((z, z))
    
    plt.imshow(Z, extent=[10, 1000, 0, 1], cmap='gray')
    plt.xscale('log')
    
    plt.axvline(100, color='red')
    
    plt.show()
    

    This is better than pcolor() and pcolormesh() because

    1. it's not insanely slow and
    2. is interpolated nicely without misleading artifacts when the image is not shown at native resolution.

提交回复
热议问题