如何通过pyds9用Python将ds9中的圆形区域颜色从绿改为红
我正在使用pyds9来自动加载天文相关的fits图像。
我可以设置其他所有参数,比如缩放比例、颜色和缩放级别。对于每张图像,我想在特定位置画一个小圆圈,以突出显示那个区域。默认情况下,这个圆圈是绿色的。我该如何改变这个颜色呢?
另外,有没有办法改变这个圆圈的粗细?我在可见性方面遇到了一些问题。绿色在某些颜色映射和缩放组合下不太明显,红色可能会更好。
我查看了XPAset命令,确实有办法做到这一点。但我不知道如何在pyds9中实现。这里是所有XPAset命令的链接:http://ds9.si.edu/ref/xpa.html#regions
xpaset命令是:
*$xpaset -p ds9 regions command '{circle 100 100 20 # color=red}'*
我该如何将这个xpaset命令转换为pyds9的d.set()
方法呢?
我想要的类似于:d.set('regions','fk5; circle(100,100,20") # color=red')
以下是我正在使用的代码:
import ds9
# x is the RA and y is the DEC
# for describing the location of astronomical objects
x = 200.1324
y = 20.3441
# the four FITS images to be loaded
image_list = ['img1.fits.gz','img2.fits.gz','img3.fits.gz','img4.fits.gz']
#initializing a ds9 window
d = ds9.ds9(target='DS9:*', start=True, verify=True)
for i in range(len(image_list)):
d.set('frame ' + str(i+1))
d.set('file ' + image_list[i])
d.set('cmap bb')
d.set('cmap invert')
d.set('scale zscale')
d.set('scale linear')
d.set('regions','fk5; circle('+str(x)+','+str(y)+',20")')
# This previous line draws a green circle with center at (x,y)
# and radius of 20 arc-sec. But the color is by default green.
# I want to change this to lets say red. How do I do it ???
# arranging the 4 images in tile format
d.set('tile')
for i in range(len(image_list)):
d.set('frame ' + str(i+1))
d.set('zoom to fit')
d.set('saveimage png myimagename.png')
# time to remove all the images from the frames
# so that the some new set of images could be loaded
for i in range(len(image_list)):
d.set('frame delete')
2 个回答
这个 pyds9 命令可以正常工作:
d.set("regions command {circle 512 512 20 # color=red}")
注意,我只是把你原来 xpaset 命令里的单引号去掉了。关于引号的使用有点复杂:在 xpaset 的命令中,你需要用单引号来保护打开的 "{" 括号,但在 Python 中就不需要了。另外,记得这整个内容是一个字符串(从技术上讲,它是 regions 参数列表的一部分——可以参考 xpa 的文档)。
祝好,
埃里克
附言:考虑到以下这个 xpa 命令也能正常工作,可能会让事情更清楚:
xpaset -p ds9 'regions command {circle 512 512 20 # color=red}'
在这里,整个字符串周围的单引号保护了打开的括号不被 Unix shell 处理,同时强调了参数列表作为一个整体字符串的特性。
看起来我不能在之前的回答上添加评论,所以我这里提供一个新的回答,跟上面的内容有关。
我们研究了一下,发现“command”语法中有个小问题。相反,你应该使用标准的xpa语法,也就是在参数列表中传入字符串“regions”,然后在数据缓冲区中放入实际的区域字符串。在Unix命令行中,可以这样做:
echo 'fk5; circle 23:23:22.176 +58:50:01.23 9.838" # color=red' | xpaset ds9 regions
数据会被发送到xpaset的标准输入,参数列表则放在命令行中,位于目标之后。
在Python中,可以这样做:
d.set('regions', 'fk5; circle 23:23:22.176 +58:50:01.23 9.838" # color=red')
这里,第一个参数是参数列表(“regions”),第二个参数是要发送给DS9的数据缓冲区,这里包含了区域字符串。
如上所示,你可以使用双引号来指定弧秒,从而发送一个以弧秒为单位的区域。你可以查看区域规范以获取更多语法信息:
https://www.cfa.harvard.edu/~john/funtools/regions.html
最后,抱歉,但无法通过命令行或pyds9来编辑区域。