简体   繁体   中英

How to center a plotted image?

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.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM