如何将绘图图像居中?

时间:2013-06-30 01:10:35

标签: python graph plot centering

我没有发布整个代码,因为大多数代码都不相关。我只需要帮助设置图像居中。

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()

plt.imshow就是我想要的中心。它绘制正确,但所有,但情节在左下角。如何将它放在图形窗口的中间?我需要这个,以便我可以调整缩放的参数。

1 个答案:

答案 0 :(得分:1)

您可以使用extent=(left, right, bottom, top)参数告诉imshow您想要图像的位置。值leftrightbottomtop位于数据坐标中。

例如,

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()

产生

enter image description here