import matplotlib.pyplot as plt
import matplotlib.image as image

POINTS = {'P1': [589.322, 2950.081],
          'P2': [588.681, 2947.203],
          'P3': [597.041, 2939.515],
          'P4': [602.735, 2941.183]}


def image_in_axes():
    
    # Create a figure and axes
    fig = plt.figure()
    fig.subplots_adjust(left=0.02, bottom=0.04, right=0.98, top=0.975)

    ax = fig.add_subplot(111)

    # set axes limits
    ax.set_ylim(2910, 2960)
    ax.set_xlim(570, 615)

    # Read in our png file
    im = image.imread('smiley.png')

    # Is there a way to avoid this???
    ax.get_frame().set_alpha(0)

    # THIS IS THE PROBLEM!!!
    ax.set_aspect('equal')

    # Get the axes transformation
    inv = ax.transData

    for label, coords in POINTS.items():
        x, y = coords

        # Plot our point location
        ax.plot(x, y, 'k.', ms=8)

        # Transform x, y axes coords into display coords
        figx, figy = inv.transform((x,  y))

        # Show our smiley image        
        fig.figimage(im, figx, figy, origin='upper')
               
    fig.savefig('image_in_axes.png', dpi=fig.dpi)


if __name__ == '__main__':
    image_in_axes()

    