[英]Image plotted with matplotlib not showing colorbar
I have a .fit file. 我有一个.fit文件。 I have read the file, displayed the image with scale. 我已阅读文件,并按比例显示图像。 When I want to write this image in .png file, the .png file is displaying the image without scale. 当我想将此图像写入.png文件时,.png文件显示的图像没有缩放比例。 I am attaching the code that I have tried. 我正在附上我尝试过的代码。
import pyfits
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
hdulist = pyfits.open('HMI20170425_134641_6173.fits')
image_data = hdulist[0].data
hdulist.close()
fig=plt.imshow(image_data, cmap='gray')
plt.colorbar()
fig.write_png('image.png')
It is showing output image with scale. 它按比例显示输出图像。 However, the 'image.png' file showing image without scale. 但是,“ image.png”文件显示的图像没有缩放比例。 Please help me in this regard. 请在这方面帮助我。
I guess what you call the scale is actually the colorbar ? 我猜你所说的比例尺实际上是颜色条吗? Which indeed is missing when you use fig.write_png
because here you are saving only the image part of the plot. 当您使用fig.write_png
时,确实确实缺少fig.write_png
因为在这里您仅保存绘图的图像部分。 You should use plt.savefig
instead: 您应该改用plt.savefig
:
# use astropy instead of pyfits which is no more maintained
import astropy.io.fits as pyfits
import matplotlib.pyplot as plt
%matplotlib inline
image_data = pyfits.getdata('HMI20170425_134641_6173.fits')
plt.imshow(image_data, cmap='gray')
plt.colorbar()
plt.savefig('image.png')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.