How to center a plotted image?

14,702

You could use the extent=(left, right, bottom, top) parameter to tell imshow where you want the image. The values left, right, bottom, top are in data-coordinates.

For example,

import matplotlib.pyplot as plt
import matplotlib.image as mimage
import matplotlib.cbook as cbook
datafile = cbook.get_sample_data('logo2.png', asfileobj=False)
im = mimage.imread(datafile)
fig, ax = plt.subplots(figsize=(5.,5.))
myaximage = ax.imshow(im,
                      aspect='auto',
                      extent=(20, 80, 20, 80),
                      alpha=0.5)
ax.plot(range(100))
plt.show()

produces

enter image description here

Share:
14,702
vdogsandman
Author by

vdogsandman

Hi! I'm a high school student learning Python, at the moment for applications relating to astrophysics/astronomy, but also for just general knowledge. Besides a brief JAVA class this is my first foray into the world of programming and computer science, and I'm truly fascinated by the world of code.

Updated on June 04, 2022

Comments

  • vdogsandman
    vdogsandman almost 2 years

    I didn't post the whole code because most of it isn't relevant. I just need help with centering the image.

    ra_new2=cat['ra'][z&lmass&ra&dec][i]
    dec_new2=cat['dec'][z&lmass&ra&dec][i]
    target_pixel_x = ((ra_new2-ra_ref)/(pixel_size_x))+reference_pixel_x     
    target_pixel_y = ((dec_new2-dec_ref)/(pixel_size_y))+reference_pixel_y    
    fig = plt.figure(figsize=(5.,5.))
    galaxy=plt.imshow(img[target_pixel_x-200:target_pixel_x+200, target_pixel_y- 
    200:target_pixel_y+200], vmin=-0.01, vmax=0.1, cmap='Greys')
    plt.show()
    

    The plt.imshow is what I'm trying to center. It plots correctly and all, but the plot is at the bottom left. How do I put this in the middle of the graph window? I need this so that I can adjust the parameters of the zoom.