How to center a plotted image?

江枫思渺然 提交于 2020-01-15 06:34:41

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/17386151/how-to-center-a-plotted-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!