Matplotlib 绘制天文散点图-平均值与经度(银河系)

Matplotlib 绘制天文散点图-平均值与经度(银河系),matplotlib,scatter-plot,hdf5,astropy,astronomy,Matplotlib,Scatter Plot,Hdf5,Astropy,Astronomy,我想在healpix文件中创建一个以X轴作为经度坐标的散点图 (Healpix) Y轴作为hdf5文件中的平均值 (法拉第天空2020) 迄今为止的代码: from astropy.io import fits #libraries from astropy import units as u from astropy.coordinates import Galactic import matplotlib.pyplot as plt import

我想在healpix文件中创建一个以X轴作为经度坐标的散点图 (Healpix)

Y轴作为hdf5文件中的平均值 (法拉第天空2020)

迄今为止的代码:

from astropy.io import fits                        #libraries
from astropy import units as u
from astropy.coordinates import Galactic
import matplotlib.pyplot as plt
import h5py
from astropy_healpix import HEALPix
import numpy as np

filename='pixel_coords_map_ring_galactic_res9.fits'                       #healpix

hdulist=fits.open(filename) 
nside = hdulist[1].header['NSIDE']
order = hdulist[1].header['ORDERING']
hp = HEALPix(nside=nside, order=order, frame=Galactic())    

print(hdulist[1].header)
print(nside)
print(order)

ggl = hdulist[1].data['LONGITUDE']           #storing coordinate values in ggl and ggb
ggb = hdulist[1].data['LATITUDE'] 
print(ggl)

gl = ggl * u.degree                            #convering to galactic coordinates
gb = ggb * u.degree
print(gl)

c = Galactic(l=gl,b=gb) 
l_rad = c.l.wrap_at(180 * u.deg).radian            #X Axis
b_rad = c.b.radian

with h5py.File('faraday2020.hdf5','r') as hdf:            #importing raw data from hdf5 file
    print(hdf.keys())
    faraday_sky_mean = hdf['faraday_sky_mean'][:]           #Y Axis
    faraday_sky_std = hdf['faraday_sky_std'][:]

我完全不知道如何绘制二维方形散点图,因为经度和平均值的格式不同。另外,我需要经度在银河系坐标系中。请帮忙。

我想你很接近了。嗯,这个散点图比使用两个天空图坐标(
projection=“aitoff”
)进行绘制要容易。这个过程类似于我在您之前的问题上发布的答案:。您只需要在函数参数中添加一些小柚木

我修改了你的代码,创建了一个二维散点图。以下是差异的简要总结:

  • 从astropy.com导入SkyCoord更改了
    (而不是
    HEALPix
  • 更改了matplot类型(删除投影=
  • 在散点图上将y变量从
    b_rad
    更改为
    faraday_sky_mean
  • 已从
    plt.scatter()
    中删除
    c=faraday\u sky\u mean
    ,因此数据点不进行颜色编码
请参阅下面的代码

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

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

with fits.open(fits_file) as hdulist:
    nside = hdulist[1].header['NSIDE']
    order = hdulist[1].header['ORDERING']
    #hp = HEALPix(nside=nside, order=order, frame=Galactic())    
    
    #print(hdulist[1].header)
    #print(nside)
    #print(order)
    
    ggl = hdulist[1].data['LONGITUDE']           #storing coordinate values in ggl and ggb
    ggb = hdulist[1].data['LATITUDE'] 
    #print(ggl)
    
    gl = ggl * u.degree                            #convering to galactic coordinates
    gb = ggb * u.degree
    #print(gl)
    
    #c = Galactic(l=gl,b=gb) 
    c = SkyCoord(l=gl,b=gb, frame='galactic', unit = (u.deg, u.deg))  
    l_rad = c.l.wrap_at(180 * u.deg).radian            #X Axis
    b_rad = c.b.radian
    print(len(l_rad))

with h5py.File(faraday_file,'r') as hdf:            #importing raw data from hdf5 file
    #print(hdf.keys())
    faraday_sky_mean = hdf['faraday_sky_mean'][:]           #Y Axis
    print(len(faraday_sky_mean))
    faraday_sky_std = hdf['faraday_sky_std'][:]
    
plt.figure(figsize=(8,4.2))
plt.subplot(111)

plt.title("Mean", y=1.08, fontsize=20)
plt.grid(True)
P2 = plt.scatter(l_rad, faraday_sky_mean, s=20, cmap='hsv')

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

plt.subplots_adjust(top=0.95, bottom=0.0)
plt.show()
print('DONE')    

我想你很接近了。嗯,这个散点图比使用两个天空图坐标(
projection=“aitoff”
)进行绘制要容易。这个过程类似于我在您之前的问题上发布的答案:。您只需要在函数参数中添加一些小柚木

我修改了你的代码,创建了一个二维散点图。以下是差异的简要总结:

  • 从astropy.com导入SkyCoord更改了
    (而不是
    HEALPix
  • 更改了matplot类型(删除投影=
  • 在散点图上将y变量从
    b_rad
    更改为
    faraday_sky_mean
  • 已从
    plt.scatter()
    中删除
    c=faraday\u sky\u mean
    ,因此数据点不进行颜色编码
请参阅下面的代码

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

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

with fits.open(fits_file) as hdulist:
    nside = hdulist[1].header['NSIDE']
    order = hdulist[1].header['ORDERING']
    #hp = HEALPix(nside=nside, order=order, frame=Galactic())    
    
    #print(hdulist[1].header)
    #print(nside)
    #print(order)
    
    ggl = hdulist[1].data['LONGITUDE']           #storing coordinate values in ggl and ggb
    ggb = hdulist[1].data['LATITUDE'] 
    #print(ggl)
    
    gl = ggl * u.degree                            #convering to galactic coordinates
    gb = ggb * u.degree
    #print(gl)
    
    #c = Galactic(l=gl,b=gb) 
    c = SkyCoord(l=gl,b=gb, frame='galactic', unit = (u.deg, u.deg))  
    l_rad = c.l.wrap_at(180 * u.deg).radian            #X Axis
    b_rad = c.b.radian
    print(len(l_rad))

with h5py.File(faraday_file,'r') as hdf:            #importing raw data from hdf5 file
    #print(hdf.keys())
    faraday_sky_mean = hdf['faraday_sky_mean'][:]           #Y Axis
    print(len(faraday_sky_mean))
    faraday_sky_std = hdf['faraday_sky_std'][:]
    
plt.figure(figsize=(8,4.2))
plt.subplot(111)

plt.title("Mean", y=1.08, fontsize=20)
plt.grid(True)
P2 = plt.scatter(l_rad, faraday_sky_mean, s=20, cmap='hsv')

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

plt.subplots_adjust(top=0.95, bottom=0.0)
plt.show()
print('DONE')    

注:两个链接都指向法拉第天空2020数据的同一页面。但是,您的代码读取“pixel\u coords\u map\u ring\u galactic\u res9.fits”(HEALPix坐标),因此这是正确的。注意:两个链接都指向法拉第天空2020数据的同一页面。但是,您的代码读取“pixel\u coords\u map\u ring\u galactic\u res9.fits”(HEALPix坐标),所以这是正确的。