# Visualizing images in sky coordinates using WCS in a notebook?

**URL:** <https://www.rubin.community/t/visualizing-images-in-sky-coordinates-using-wcs-in-a-notebook/4210>\
**Category:** Support\
**Created:** [June 17, 2020, 12:28pm UTC](https://www.rubin.community/t/visualizing-images-in-sky-coordinates-using-wcs-in-a-notebook/4210 "2020-06-17T12:28:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bechtol](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/bechtol/32/685_2.png) [@bechtol](https://www.rubin.community/u/bechtol)\
**Post date:** [June 17, 2020, 12:28pm UTC](https://www.rubin.community/t/visualizing-images-in-sky-coordinates-using-wcs-in-a-notebook/4210/1 "2020-06-17T12:28:51Z")

</div>

Is there a recommended way to visualize images processed with the Stack (e.g., the masked image of an `Exposure`) in sky coordinates using the WCS in a notebook? Is this possible with `afw.display` or another tool?

---

<div class="post-metadata">

**Author:** ![mrawls](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/mrawls/32/2810_2.png) [@mrawls](https://www.rubin.community/u/mrawls)\
**Post date:** [June 26, 2020, 5:54pm UTC](https://www.rubin.community/t/visualizing-images-in-sky-coordinates-using-wcs-in-a-notebook/4210/2 "2020-06-26T17:54:57Z")

</div>

If you use one of the notebook magic commands that gives you interactive plots, like `%matplotlib ipympl`, you can hover over the `afw.display`-ed images and see the sky coordinates. If you want to plot objects at certain coordinates on top of the image, you can use the `pixelToSky` and/or `skyToPixel` methods available with the image’s WCS.

---

<div class="post-metadata">

**Author:** ![bechtol](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/bechtol/32/685_2.png) [@bechtol](https://www.rubin.community/u/bechtol)\
**Post date:** [June 26, 2020, 6:31pm UTC](https://www.rubin.community/t/visualizing-images-in-sky-coordinates-using-wcs-in-a-notebook/4210/3 "2020-06-26T18:31:58Z")

</div>

Thanks @mrawls. So it seems that one can get the transformation for specific coordinates in the image (either with the hover technique you mentioned, or by overplotting points with known sky coordinates into the pixel coordinates of the image), but maybe it is not possible to directly plot the image in another coordinate system with the current tooling?

---

<div class="post-metadata">

**Author:** ![yusra](https://avatars.discourse-cdn.com/v4/letter/y/f4b2a3/32.png) [@yusra](https://www.rubin.community/u/yusra)\
**Post date:** [June 26, 2020, 7:09pm UTC](https://www.rubin.community/t/visualizing-images-in-sky-coordinates-using-wcs-in-a-notebook/4210/4 "2020-06-26T19:09:25Z")

</div>

@bechtol Do you have an example of what you have in mind?

I was imagining something like this: [https://docs.astropy.org/en/stable/visualization/wcsaxes/index.html](https://docs.astropy.org/en/stable/visualization/wcsaxes/index.html)  
Same image, WCS-based axes.

But are you wanting to warp the image to another projection?

---

<div class="post-metadata">

**Author:** ![gpdf](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/gpdf/32/65_2.png) [@gpdf](https://www.rubin.community/u/gpdf)\
**Post date:** [June 27, 2020, 2:53am UTC](https://www.rubin.community/t/visualizing-images-in-sky-coordinates-using-wcs-in-a-notebook/4210/5 "2020-06-27T02:53:03Z")

</div>

When using Firefly for image visualization, driven by `afw.display` from a Rubin/LSST stack image, or “natively” against a data service, the WCS is known to the display, and sky coordinates (in equatorial or Galactic systems) for the location of the mouse are displayed continuously, or can be captured for a selected point. In addition, a coordinate grid can be plotted over the image in any of several systems.

---

<div class="post-metadata">

**Author:** ![MelissaGraham](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/melissagraham/32/2549_2.png) [@MelissaGraham](https://www.rubin.community/u/MelissaGraham)\
**Post date:** [June 15, 2021, 12:32am UTC](https://www.rubin.community/t/visualizing-images-in-sky-coordinates-using-wcs-in-a-notebook/4210/6 "2021-06-15T00:32:34Z")

</div>

I found this topic because I’m also looking to change the `afwDisplay` axes labels from pixels to sky coordinates. (Attention @jeffcarlin).

I have made a kludge, screenshot below, but surely there’s a better way? @bechtol did you ever find a way that works?

One better way might be, as @yusra suggested, to put the image’s WCS into an astropy WCS and then use `plt.subplot(projection=wcs)`, but I’m not sure how to do the first part. Trying this:  
`from astropy.wcs import WCS`  
`co_wcs = WCS(cutout_image.getWcs())`  
Gives an error that `'lsst.afw.geom._geom.SkyWcs' object has no attribute 'keys'`, so there’s a formatting mismatch? (I am still looking into how to define an astropy wcs from what getWcs gives me; the contents of `co_wcs` can be seen in the screenshot below).

Another better way is maybe to just use `mtv`, if possible? It does seem to take a `wcs`, but passing something like the following doesn’t change the axes labels:  
`afw_display.mtv( cutout_image.image, wcs=cutout_image.getWcs() )`

This is my kludge. It is ugly.

 ![Screen Shot 2021-06-14 at 5.21.18 PM](https://us1.discourse-cdn.com/flex002/uploads/rubin/original/2X/6/694880a61e92be62fd6eae4d0966bd4f0a012358.jpeg)

---

<div class="post-metadata">

**Author:** ![ktl](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/ktl/32/1373_2.png) [@ktl](https://www.rubin.community/u/ktl)\
**Post date:** [June 15, 2021, 2:24pm UTC](https://www.rubin.community/t/visualizing-images-in-sky-coordinates-using-wcs-in-a-notebook/4210/7 "2021-06-15T14:24:27Z")

</div>

A [Slack message](https://lsstc.slack.com/archives/C2JPMCF5X/p1620690324280700?thread_ts=1620682879.272500&cid=C2JPMCF5X) by @suberlak last month showed how to create an astropy WCS from an image’s FITS headers and then plot it using `plt.subplot(projection=wcs)`. In this case, I think you need `co_wcs = WCS(cutout_image.getWcs().getFitsMetadata())`.

---

<div class="post-metadata">

**Author:** ![ktl](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/ktl/32/1373_2.png) [@ktl](https://www.rubin.community/u/ktl)\
**Post date:** [June 15, 2021, 2:25pm UTC](https://www.rubin.community/t/visualizing-images-in-sky-coordinates-using-wcs-in-a-notebook/4210/8 "2021-06-15T14:25:15Z")

</div>

Note that your kludge can be misleading because the RA/dec axes do not align with the image x/y axes that you are labeling.

---

<div class="post-metadata">

**Author:** ![MelissaGraham](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/melissagraham/32/2549_2.png) [@MelissaGraham](https://www.rubin.community/u/MelissaGraham)\
**Post date:** [June 15, 2021, 6:48pm UTC](https://www.rubin.community/t/visualizing-images-in-sky-coordinates-using-wcs-in-a-notebook/4210/9 "2021-06-15T18:48:42Z")

</div>

Awesome, thanks @ktl !

 ![Screen Shot 2021-06-15 at 11.56.51 AM](https://us1.discourse-cdn.com/flex002/uploads/rubin/original/2X/7/7631c0abba152615c7a3e823cd36c3ffb9adda9e.png)
