Matplotlib 使用hdf5文件中的astropy在skyplot上绘制平均值和标准偏差值

Matplotlib 使用hdf5文件中的astropy在skyplot上绘制平均值和标准偏差值,matplotlib,hdf5,astropy,polar-coordinates,fits,Matplotlib,Hdf5,Astropy,Polar Coordinates,Fits,我正在尝试创建一个skyplot(使用astropy),其中包含hdf5文件中的平均值和标准偏差值。数据链接为(法拉第天空2020)。 到目前为止,我已经编写了以下代码,其中数据从hdf5文件读取到ggl和ggb,然后将值转换为gb和gl中的银河坐标(l和b)。我需要在skyplot中绘制这些值 from astropy import units as u from astropy.coordinates import SkyCoord import matplotlib.pyplot as p

我正在尝试创建一个skyplot(使用astropy),其中包含hdf5文件中的平均值和标准偏差值。数据链接为(法拉第天空2020)。 到目前为止,我已经编写了以下代码,其中数据从hdf5文件读取到ggl和ggb,然后将值转换为gb和gl中的银河坐标(l和b)。我需要在skyplot中绘制这些值

from astropy import units as u
from astropy.coordinates import SkyCoord
import matplotlib.pyplot as plt
import numpy as np
import h5py

dat = []

ggl=[]

ggb=[]

with h5py.File('faraday2020.hdf5','r') as hdf:
    print(list(hdf.keys()))
    faraday_sky_mean = hdf['faraday_sky_mean'][:]
    faraday_sky_std = hdf['faraday_sky_std'][:]
    
print(faraday_sky_mean.shape, faraday_sky_mean.dtype) 
print(f'Max Mean={max(faraday_sky_mean)}, Min Mean={min(faraday_sky_mean)}') 
print(faraday_sky_std.shape, faraday_sky_std.dtype) 
print(f'Max StdDev={max(faraday_sky_std)}, Min StdDev={min(faraday_sky_std)}') 

ggl = faraday_sky_mean.tolist()
print(len(ggl),type(ggl[0]))
ggb = faraday_sky_std.tolist()
print(len(ggb),type(ggb[0]))

gl = ggl * u.degree
gb = ggb * u.degree


c = SkyCoord(l=gl,b=gb, frame='galactic', unit = (u.deg, u.deg)) #, 

l_rad = c.l.wrap_at(180 * u.deg).radian
b_rad = c.b.radian

###
plt.figure(figsize=(8,4.2))
plt.subplot(111, projection="aitoff")

plt.title("Mean and standard dev", y=1.08, fontsize=20)
plt.grid(True)

P1=plt.plot(l_rad, b_rad,c="blue", s=220, marker="h", alpha=0.7) #, 

plt.subplots_adjust(top=0.95, bottom=0.0)
plt.xlabel('l (deg)', fontsize=20)
plt.ylabel('b (deg)', fontsize=20)

plt.subplots_adjust(top=0.95, bottom=0.0)
plt.show()
但是,我得到以下错误:

'got {}'.format(angles.to(u.degree)))

ValueError: Latitude angle(s) must be within -90 deg <= angle <= 90 deg, got [1.12490771 0.95323024 0.99124631 ... 4.23648627 4.28821608 5.14498169] deg
'got{}.格式(角度到(u度)))

ValueError:纬度角必须在-90度以内我明白了为什么绘制此数据时出现问题。链接文件(
法拉第2020.hdf5
)中的数据只是重建法拉第天空的平均值和标准偏差。请参阅链接页面上的注释:“所有地图均以分辨率Nside=512的银河系显示,并以环排序方案存储。单位为rad/m2。”换句话说,您需要从其他来源获取skyplot坐标

通过谷歌搜索,在NASA戈达德·拉姆达工具网站上找到了坐标:。具体地说,您需要NSide=512/Galactic/Ring像素顺序的此文件:

所以,第一个问题解决了。接下来,您需要读取FITS格式的文件以获得坐标。Astropy有“fits”模块可以做到这一点。请参阅下面的代码

from astropy.io import fits
from astropy import units as u
from astropy.coordinates import SkyCoord
import matplotlib.pyplot as plt
import h5py

filename='pixel_coords_map_ring_galactic_res9.fits'

with fits.open(filename) as hdul:
    print(hdul.info())
    arr = hdul[1].data
    print(arr.shape)
# Returns:
# (3145728,)
    print(arr.dtype)
# Returns:
# dtype((numpy.record, [('LONGITUDE', '>f4'), ('LATITUDE', '>f4')]))

ggl = arr['LONGITUDE'][:].tolist()
ggb = arr['LATITUDE'][:].tolist() 

gl = ggl * u.degree
gb = ggb * u.degree

c = SkyCoord(l=gl,b=gb, frame='galactic', unit = (u.deg, u.deg))  

l_rad = c.l.wrap_at(180 * u.deg).radian
b_rad = c.b.radian
上面的代码为您提供了天空图坐标的
l_rad
b_rad
。接下来,您需要合并到我之前给您的代码中,以阅读法拉第天空平均值和STDEV

with h5py.File('faraday2020.hdf5','r') as hdf:
    faraday_sky_mean = hdf['faraday_sky_mean'][:]
    faraday_sky_std = hdf['faraday_sky_std'][:]
最后,使用matplotlib绘制两组数据。我将绘图更改为使用散点图,用
c=faraday\u sky\u mean
(平均值)对标记进行颜色编码。您也可以使用
faraday_sky_stddev
获得标准偏差值

plt.figure(figsize=(8,4.2))
plt.subplot(111, projection="aitoff")

plt.title("Mean", y=1.08, fontsize=20)
plt.grid(True)
# P1=plt.plot(l_rad, b_rad,c="blue", marker="h", alpha=0.7) #, s=220) 
P2 = plt.scatter(l_rad, b_rad, s=20, c=faraday_sky_mean, cmap='hsv')

plt.subplots_adjust(top=0.95, bottom=0.0)
plt.xlabel('l (deg)', fontsize=20)
plt.ylabel('b (deg)', fontsize=20)

plt.subplots_adjust(top=0.95, bottom=0.0)
plt.show()
print('DONE')
把它们放在一起,你会得到下面的图片。我认为这是准确的(但对天体物理学一无所知,所以不是100%确定)。这会让你找到正确的方向。祝你好运


这是我先前答案的延伸。最初的帖子想在天文云图上绘制法拉第天空2020年数据的平均值和标准偏差。参考数据源(来自Radboud大学)仅包括平均值和标准偏差。相关的经度和纬度坐标是从美国宇航局戈达德·兰姆达工具站获得的。下面的代码显示了如何将两个文件中的数据合并到一个HDF5文件中。为方便起见,此处重复了指向数据源的链接:

  • 链接到
  • 链接到
生成的文件名为:“faraday2020_with_coords.h5”


检查打印输出的最大/最小值。你应该得到:
Max-Mean=2857.1,Min-Mean=-1406.1
Max-StdDev=696.6,Min-StdDev=0.0206
。请注意,
gl=ggl*u.degree
不会更改大小,只更改python类(从ggl[0]的float到gl[0]的astropy.units.quantity.quantity)。非常感谢您的帮助,非常感谢!很高兴这有帮助。我将投票结束你之前的两个问题(因为这一个涵盖了1个问题和答案中的所有内容)。
from astropy.io import fits
import h5py

fits_file = 'pixel_coords_map_ring_galactic_res9.fits'
faraday_file = 'faraday2020.hdf5'

with fits.open(fits_file) as hdul, \    
     h5py.File(faraday_file,'r') as h5r, \
     h5py.File('faraday2020_with_coords.h5','w') as h5w:

    arr = hdul[1].data

    dt = [('LONGITUDE', float), ('LATITUDE', float), \
          ('faraday_sky_mean', float), ('faraday_sky_std', float) ]
                             
    ds = h5w.create_dataset('skyplotdata', shape=(arr.shape[0],), dtype=dt)
    ds['LONGITUDE'] = arr['LONGITUDE'][:]
    ds['LATITUDE']  = arr['LATITUDE'][:]
    ds['faraday_sky_mean'] = h5r['faraday_sky_mean'][:]
    ds['faraday_sky_std']  = h5r['faraday_sky_std'][:]