使用 healpy 制作带有 HEALPix 像素化的 2D 直方图 [英] Make a 2D histogram with HEALPix pixellization using healpy

查看:97
本文介绍了使用 healpy 制作带有 HEALPix 像素化的 2D 直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据为天空中物体的坐标,例如如下:

导入pylab为plt将 numpy 导入为 npl = np.random.uniform(-180, 180, 2000)b = np.random.uniform(-90, 90, 2000)

我想做一个 2D 直方图,以便在天空中使用 (l, b) 坐标绘制某个点的密度图,在 Mollweide 投影上使用 HEALPix 像素化.我怎样才能使用 healpy 做到这一点?

教程:

<块引用>

另请注意,通过在银河纬度均匀采样,您会更喜欢银河两极的数据点.如果您想避免这种情况,可以使用余弦将其缩小.

hp.orthview(np.log10(hpx_map+1), rot=[0, 90])hp.graticule(颜色=白色")

The data are coordinates of objects in the sky, for example as follows:

import pylab as plt
import numpy as np
l = np.random.uniform(-180, 180, 2000)
b = np.random.uniform(-90, 90, 2000)

I want to do a 2D histogram in order to plot a map of the density of some point with (l, b) coordinates in the sky, using HEALPix pixellization on Mollweide projection. How can I do this using healpy ?

The tutorial:

http://healpy.readthedocs.io/en/v1.9.0/tutorial.html

says how to plot a 1D array, or a fits file, but I don't find how to do a 2d histogram using this pixellization.

I also found this function, but it is not working , so I am stuck.

hp.projaxes.MollweideAxes.hist2d(l, b, bins=10)

I can do a plot of these points in Mollweide projection this way :

l_axis_name ='Latitude l (deg)'
b_axis_name = 'Longitude b (deg)'

fig = plt.figure(figsize=(12,9))
ax = fig.add_subplot(111, projection="mollweide")
ax.grid(True)

ax.scatter(np.array(l)*np.pi/180., np.array(b)*np.pi/180.)

plt.show()

Thank you very much in advance for your help.

解决方案

Great question! I've written a short function to convert a catalogue into a HEALPix map of number counts:

from astropy.coordinates import SkyCoord
import healpy as hp
import numpy as np

def cat2hpx(lon, lat, nside, radec=True):
    """
    Convert a catalogue to a HEALPix map of number counts per resolution
    element.

    Parameters
    ----------
    lon, lat : (ndarray, ndarray)
        Coordinates of the sources in degree. If radec=True, assume input is in the icrs
        coordinate system. Otherwise assume input is glon, glat

    nside : int
        HEALPix nside of the target map

    radec : bool
        Switch between R.A./Dec and glon/glat as input coordinate system.

    Return
    ------
    hpx_map : ndarray
        HEALPix map of the catalogue number counts in Galactic coordinates

    """

    npix = hp.nside2npix(nside)

    if radec:
        eq = SkyCoord(lon, lat, 'icrs', unit='deg')
        l, b = eq.galactic.l.value, eq.galactic.b.value
    else:
        l, b = lon, lat

    # conver to theta, phi
    theta = np.radians(90. - b)
    phi = np.radians(l)

    # convert to HEALPix indices
    indices = hp.ang2pix(nside, theta, phi)

    idx, counts = np.unique(indices, return_counts=True)

    # fill the fullsky map
    hpx_map = np.zeros(npix, dtype=int)
    hpx_map[idx] = counts

    return hpx_map

You can then use that to populate the HEALPix map:

l = np.random.uniform(-180, 180, 20000)
b = np.random.uniform(-90, 90, 20000)

hpx_map = hpx.cat2hpx(l, b, nside=32, radec=False)

Here, the nside determines how fine or coarse your pixel grid is.

hp.mollview(np.log10(hpx_map+1))

Also note that by sampling uniformly in Galactic latitude, you'll prefer data points at the Galactic poles. If you want to avoid that, you can scale that down with a cosine.

hp.orthview(np.log10(hpx_map+1), rot=[0, 90])
hp.graticule(color='white')

这篇关于使用 healpy 制作带有 HEALPix 像素化的 2D 直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆