astropy常用命令 python天文绘图

绘图(结合mpl)

  1. 画出图像及contour
from astropy.wcs import WCS
from astropy.io import fits
import matplotlib.pyplot as plt

hdu=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)
  1. 添加标记及其他文件的坐标转换
    参考官网链接 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]))

#添加标记等见上述链接
  1. 调整显示, 增加图例, 添加第二坐标轴等次要步骤

添加第二坐标轴参考官网链接
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'
  1. 坐标轴及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}$')
  1. 保存
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'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值