简体   繁体   English

如何居中绘制图像?

[英]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. plt.imshow是我试图确定的重点。 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. 您可以使用imshow extent=(left, right, bottom, top)参数告诉imshow您想要图像的位置。 The values left , right , bottom , top are in data-coordinates. leftrightbottomtop的值在数据坐标中。

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 产生

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 label 放入聚类绘制图像中:在侧面指示它是哪个聚类,并在中心指示? - How to put label in Clustering plotted image : indicating in the side which cluster it is, and also in the center? 如何保存用Matplolib 1.5绘制的图像 - How to save an image plotted with matplolib 1.5 如何在 tkinter 窗口上的图像上绘制线条来显示图像? - How to show image with lines plotted over the image on tkinter window? 如何在 tkinter 画布窗口上显示带有线条的图像? - How to show image with lines plotted over it on tkinter canvas window? 如何生成结果作为图形标题以及与绘制图像连接的文件名? - How to generate result as the figure title along with filename connected with the plotted image? 如何让Matplotlib的imshow生成图像而不被绘制 - How to have matplotlib's imshow generate an image without being plotted 如何在matplotlib中画一条线,使画线的边缘(而不是中心)跟随绘制的数据? - How can draw a line in matplotlib so that the edge (not the center) of the drawn line follows the plotted data? 如何使图像居中 matplotlib - How to Center an Image matplotlib 使用matplotlib绘制的图像未显示颜色条 - Image plotted with matplotlib not showing colorbar 如何在 pygame 中的坐标上居中图像 - How To Center An Image on Coordinates in pygame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM