简体   繁体   English

如何将轮廓图(matplotlib)转换为带有标题的FITS格式?

[英]How could I convert a contour plot (matplotlib) to FITS format with header?

I need to make a contour plot and overlie the contours on the image. 我需要绘制轮廓图并覆盖图像上的轮廓。 I have used aplpy library to overlie the contours on an astronomical image. 我已经使用aplpy库覆盖了天文图像上的轮廓。 I have downloaded the 2MASS data in APlpy website( https://github.com/aplpy/aplpy-examples/tree/master/data ) and wrote the following piece of code: 我已经在APlpy网站( https://github.com/aplpy/aplpy-examples/tree/master/data )中下载了2MASS数据,并编写了以下代码:

import numpy as np
import aplpy
import atpy
from pyavm import AVM
import scipy
import matplotlib.pyplot as plt
import scipy.ndimage
from matplotlib import cm
import montage_wrapper
import matplotlib.colors as colors
from scipy import stats

def get_contour_verts(cn):
    contours = []
    # for each contour line
    for cc in cn.collections:
        paths = []
        # for each separate section of the contour line
        for pp in cc.get_paths():
            xy = []
            # for each segment of that section
            for vv in pp.iter_segments():
                xy.append(vv[0])
            paths.append(np.vstack(xy))
        contours.append(paths)
    return contours

# Convert all images to common projection
aplpy.make_rgb_cube(['2MASS_h.fits', '2MASS_j.fits', '2MASS_k.fits'], '2MASS_rgb.fits')

# Customize it
aplpy.make_rgb_image('2MASS_rgb.fits','2MASS_rgb_contour.png',embed_avm_tags=True)

# Launch APLpy figure of 2D cube
img = aplpy.FITSFigure('2MASS_rgb_contour.png')
img.show_rgb()

# Modify the tick labels for precision and format
img.tick_labels.set_xformat('hhmm')
img.tick_labels.set_yformat('ddmm')
# Move the tick labels
img.tick_labels.set_xposition('top')
img.tick_labels.set_yposition('right')
img.tick_labels.set_font(size='small', family='sans-serif', style='normal')

data = scipy.loadtxt('sources.txt')
m1=data[:,0]
m2=data[:,1]
xmin = m1.min()
xmax = m1.max()
ymin = m2.min()
ymax = m2.max()
#Gridding the data 

X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([X.ravel(), Y.ravel()])
values = np.vstack([m1, m2])
kernel = stats.gaussian_kde(values)
Z = np.reshape(kernel(positions).T, X.shape)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(np.rot90(Z), cmap=plt.cm.gist_earth_r, extent=[xmin, xmax, ymin, ymax])
ax.plot(m1, m2, 'k.', markersize=2)
ax.set_xlim([xmin, xmax])
ax.set_ylim([ymin, ymax])

CS = plt.contour(X, Y, Z)
Contour_arrays=get_contour_verts(CS)

#Adding contour plots
for contours_at_level in Contour_arrays:
    radec = [img.pixel2world(*verts.T) for verts in contours_at_level]
    new_radec=[]
    for coor in radec:
        #new_radec.append(np.column_stack((coor[0], coor[1])))
        new_radec.append(np.vstack((coor[0], coor[1])))
    print new_radec
    img.show_lines(new_radec,color='red', zorder=100)

img.save('tutorial.png')

It seems it does not still work at all. 看来它仍然无法正常工作。

You can't "save contours as a FITS file" directly, but there are other approaches you can try. 您不能直接“将轮廓另存为FITS文件”,但是可以尝试其他方法。

You can use the matplotlib._cntr tool as described here: Python: find contour lines from matplotlib.pyplot.contour() to get the endpoints in figure coordinates, then use the WCS to convert between pixel and world coordinates. 您可以按如下所述使用matplotlib._cntr工具: Python:从matplotlib.pyplot.contour()查找轮廓线以获取图形坐标的端点,然后使用WCS在像素坐标和世界坐标之间进行转换。 aplpy.FITSFigure s have convenience functions, F.world2pixel and F.pixel2world , which each accept 2 arrays: aplpy.FITSFigure具有便利功能F.world2pixelF.pixel2world ,它们每个都接受2个数组:

F.pixel2world(arange(5),arange(5))

So if you are working with a grid that is identical to that shown in the FITSFigure window, you could convert your points to world coordinates and plot them with show_lines : 因此,如果使用的网格 FITSFigure窗口中显示的网格相同 ,则可以将点转换为世界坐标,并使用show_lines

ra,dec = F.pixel2world(xpix,ypix)
F.show_lines([[ra,dec]])

or for a more realistic contour case, now copying the code from the linked article: 或者对于更实际的轮廓情况,现在从链接的文章中复制代码:

import numpy as np
import aplpy

def get_contour_verts(cn):
    contours = []
    # for each contour line
    for cc in cn.collections:
        paths = []
        # for each separate section of the contour line
        for pp in cc.get_paths():
            xy = []
            # for each segment of that section
            for vv in pp.iter_segments():
                xy.append(vv[0])
            paths.append(np.vstack(xy))
        contours.append(paths)

    return contours

# This line is copied from the question's code, and assumes that has been run previously
CS = plt.contour(Xgrid, Ygrid, Hsmooth,levels=loglvl,extent=extent,norm=LogNorm())

contours = get_contour_verts(CS) # use the linked code


gc = aplpy.FITSFigure('2MASS.fits',figsize=(10,9))

# each level comes with a different set of vertices, so you have to loop over them
for contours_at_level in Contour_arrays:
    clines = [cl.T for cl in contours_at_level]
    img.show_lines(clines,color='red', zorder=100)

Still the simplest approach is, if you have a FITS file from which you're generating said contours, just use that file directly. 仍然最简单的方法是,如果您有一个FITS文件要从中生成所述轮廓,则直接使用该文件即可。 Or if you don't have a FITS file, you can make one with pyfits by creating your own header. 或者,如果您没有FITS文件,则可以通过创建自己的标头使用pyfits制作一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM