Search code examples
pythonmatplotlibplotaxis-labels

matplotlib change Axis scale


I have below plot my data is 2D array from range 0 to 10 but I want my plot's axis be between [0,1] how can I change the scale? besides how can I upside down Y axis?

mydata = np.zeros((10,10))
#fill mydata
im = plt.imshow(mydata,interpolation='bilinear', aspect='auto') 
plt.colorbar(im, orientation='vertical')
plt.show()

enter image description here


Solution

    • The extent parameter alters the ticks displayed on the imshow plot. The extent should be passed a 4-tuple or list with the following meaning: extent=(xmin, xmax, ymin, ymax).
    • The y-axis can be inverted by passing origin='lower' to imshow.

    import numpy as np
    import matplotlib.pyplot as plt
    
    mydata = np.random.random((10,10))
    
    im = plt.imshow(mydata,interpolation='bilinear', aspect='auto',
                    origin='lower', extent=[0, 1, 0, 1]) 
    plt.colorbar(im, orientation='vertical')
    plt.show()
    

    enter image description here


    Another way to invert the yaxis is to call ax.invert_yaxis. See (here) and (here) for examples.