Lesson 01: read blzars data from catalog and draw a scatter plot
讀入blzar 資料,並畫出天圖分佈
Example Code
- import numpy as np
- import matplotlib.pyplot as plt
- from astropy.io import ascii
- import astropy.coordinates as coord
- import astropy.units as u
- RowData = ascii.read("bzcat5.dat", readme="ReadMe")
- sign = np.arange(len(RowData['DE-']))
- for i in range(len(RowData['DE-'])):
- if(RowData['DE-'][i] == '-'):
- sign[i] = -1*RowData['DEd'][i]
- else:
- sign[i] = RowData['DEd'][i]
- ra = coord.Angle((RowData['RAh'], RowData['RAm'], RowData['RAs']), unit='hourangle')
- ra = ra.wrap_at(180*u.degree)
- dec = coord.Angle((sign ,RowData['DEm'], RowData['DEs']), unit=u.deg)
- fig = plt.figure(figsize=(8,6))
- ax = fig.add_subplot(111, projection="mollweide")
- ax.grid(True)
- MaxColor=max(RowData['z'])
- ax.scatter(ra.radian, dec.radian, c=RowData['z']/MaxColor)
- plt.show()
Note and Comments
- import matplotlib.pyplot as plt 匯入圖形函式庫Matplotlib中的pyplot資料庫, 並命名爲plt
- from astropy.io import ascii 匯入astropy.io 輸入輸出函式庫中的ASCII資料庫, 我們將使用ascii.read和是讀取資料
- import astropy.coordinates as coord 匯入astropy.coordinates座標函式庫, 並命名爲coord
- import astropy.units as u 匯入astropy.units單位函式庫, 並命名爲u
- RowData = ascii.read("bzcat5.dat", readme="ReadMe") 依照ReadMe檔案中的定義讀取bzcat5.dat檔案
- &npsb;&npsb;&npsb;&npsb;&npsb; 讀入的檔案可以利用ReadMe檔案中定義中的資料名稱來讀取每一行的資料. ex:RowData['z'] z是ReadMe檔案中定義中的紅移資料名稱
- sign = np.arange(len(RowData['DE-'])) 由於bzcat5.dat資料中角度的正負號爲是用"-"代表負號, " "空白代表正號, 於是我們宣告一個新的數列sign用來存放+1與-1值,也就是正負號
- for i in range(len(RowData['DE-'])): 將原始bzcat5.dat資料中的角度大小與正負號值結合,並存入sign中
- if(RowData['DE-'][i] == '-'):
- &npsb;&npsb;&npsb;&npsb;&npsb;sign[i] = -1*RowData['DEd'][i]
- else:
- &npsb;&npsb;&npsb;&npsb;&npsb;sign[i] = RowData['DEd'][i]
- ra = coord.Angle((RowData['RAh'], RowData['RAm'], RowData['RAs']), unit='hourangle') 將時間角度的位置(包含角分角秒)存放在ra數列內, 並作為接下來作圖時的y軸
- dec = coord.Angle((sign ,RowData['DEm'], RowData['DEs']), unit=u.deg) 將角度的位置存放在dec數列內, 並作為接下來作圖時的x軸
- fig = plt.figure(figsize=(8,6)) 宣告一個長寬比8:6的畫圖物件fig
- ax = fig.add_subplot(111, projection="mollweide") fig中包含一個圖(ax0), 並宣告此圖將使用mollweide投影法, 也可以使用其它投影法:aitoff mollweide hammer 例圖放置與本網頁末
- MaxColor=max(RowData['z']) RowData['z'] 是資料中的紅移位置資訊, 我們利用max()函數找出.資料中的最大值 我們將該紅移資料歸一化(由0-1), 並用來當作是圖中資料點的距離資訊
- ax.scatter(ra.radian, dec.radian, c=RowData['z']/MaxColor) 將角度數據填入scatter圖中. c爲梅個資料點的顏色, 我們將紅移資料歸一化(由0-1), 並用來當作是圖中資料點的距離資訊
- plt.show() 圖形設定完畢,開始作圖
lambert
aitoff
mollweide
hammer