mb49
mb49

Reputation: 73

How to centre the origin in the centre of an imshow() plot

As a part of a long program to simulate diffraction through an aperture, I'm fiddling around trying to get my resulting plt.imshow() to have its origin in the centre of the plot, i.e. I wish to change the axes.

The relevant code section is:

    n=40
    lam=0.0006
    k=(2*np.pi)/lam
    z=float(input("Type screen distance, z in mm: "))
    rho=float(input("Type rho in mm: "))

    delta = 2/n
    intensity = np.zeros((n+1,n+1))

    for i in range(n+1):
        x=-1+(i*delta)
        for j in range(n+1):
            y =-1+(j*delta)
            intensity[i,j] = (abs(square_2dsimpson_eval(-rho/2,rho/2,n)))**2  
    plt.imshow(intensity)
    plt.show()

generating the below plot. Thanks in advance. enter image description here

Upvotes: 4

Views: 6708

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339340

In order to position the origin at the center of the image plot, you can use a symmetric extent.

import numpy as np
import matplotlib.pyplot as plt

x = np.array([[1,2,1,4],[2,5,3,1],[0,1,2,2]])
plt.imshow(x, extent=[-x.shape[1]/2., x.shape[1]/2., -x.shape[0]/2., x.shape[0]/2. ])
plt.show()

enter image description here

Upvotes: 7

Related Questions