绘图(结合mpl)
- 画出图像及contour
from astropy.wcs import WCS
from astropy.io import fits
import matplotlib.pyplot as plthdu=fits.open(filename)[0]
wcs=WCS(hdu.header)ax=plt.subplot(projection=wcs)#show image
ax.imshow(hdu.data, vmin=, vmax=, origin='lower')
'''or maybe np.log10(hdu[0].data)'''
#show contour
ax.contour(hdu.data, levels=[], colors='white', alpha=0.5)
- 添加标记及其他文件的坐标转换
参考官网链接 Making plots with world coordinates
#添加其他坐标系的contour
ax.contour(othercoord.data, transform=ax.get_transform(WCS(othercoord.header)),\levels=, colors='white')
#添加方框
from matplotlib.patches import Rectangle
r = Rectangle((x_center., y_center.), 宽., 高., edgecolor='white', facecolor='none')
ax.add_patch(r)#shili
table_len=table['x'].shape[0]
for i in range(table_len):r = Rectangle((table['x'][i]-table['r'][i]/2,table['y'][i]-table['r2'][i]/2), table['r'][i],table['r2'][i], edgecolor='white', facecolor='none')ax.add_patch(r)
#绘制多边形
pgon = plt.Polygon(([0.15,0.15], [0.35, 0.4], [0.2, 0.6], [0.3, 0.2]))#添加标记等见上述链接
- 调整显示, 增加图例, 添加第二坐标轴等次要步骤
添加第二坐标轴参考官网链接
Make a plot with both redshift and universe age axes using astropy.cosmology
Plots with different scales
#调整显示
建议在imshow时即使用 np.log10(hdu[0].data)#增加图例(用"label")
plt.plot(x,y,label="my little line")
plt.legend(fontsize=10.)#调整色彩
imshow中参数 cmap
最清晰但过于明亮: 'jet'
高级灰但略不明显: 'viridis'
- 坐标轴及label
坐标轴相关 CoordinateHelper
#调整坐标轴格式
ax.coords[0].set_major_formatter('d.dd')
ax.coords[1].set_major_formatter('d.dd')
#在label中正确使用\rm
plt.xlabel(r'$Italic,{\rm Normal}$')
- 保存
plt.savefig('xxx.pdf',bbox_inches='tight')
Others:
https://matplotlib.org/faq/howto_faq.html
#自动选合适的bar_label颜色
text_color = 'white' if r * g * b < 0.5 else 'darkgrey'