Matplotlib: how to make imshow read x,y coordinates from other numpy arrays?

前端 未结 3 649
甜味超标
甜味超标 2020-11-28 15:56

When you want to plot a numpy array with imshow, this is what you normally do:

import numpy as np
import matplotlib.pyplot as plt

A=np.array([[         


        
3条回答
  •  有刺的猬
    2020-11-28 16:37

    Setting the extent

    Assuming you have

    real_x=np.array([15,16,17])
    real_y=np.array([20,21,22,23])
    

    you would set the image extent as

    dx = (real_x[1]-real_x[0])/2.
    dy = (real_y[1]-real_y[0])/2.
    extent = [real_x[0]-dx, real_x[-1]+dx, real_y[0]-dy, real_y[-1]+dy]
    plt.imshow(data, extent=extent)
    

    Changing ticklabels

    An alternative would be to just change the ticklabels

    real_x=np.array([15,16,17])
    real_y=np.array([20,21,22,23])
    plt.imshow(data)
    plt.gca().set_xticks(range(len(real_x)))
    plt.gca().set_yticks(range(len(real_x)))
    plt.gca().set_xticklabels(real_x)
    plt.gca().set_yticklabels(real_y)
    

提交回复
热议问题