Skip to content

Add to WCS tutorial: How to place a scale bar on an image with WCS #443

Closed

Description

@eblur

A colleague of mine asked me this simple question, and I thought it would make a good addition to the WCS tutorial. He ended up finding a solution in the source code for AplPy, but I'm certain we could make it simpler. (i.e., we don't need to the if statements to know if our inputs are an Astropy Quantity versus a Unit)

## Scale length from my colleague is calculated for a cosmology case
#a2261 is at z = 0.22331. For Omega_M = 0.3, Omega_Lambda = 0.7, this corresponds to 1 arcsec = 3.593 kpc
scale_length = 100.0 / 3.593 * u.arcsec # = 100 kpc

## This code plots that scale length on the image
# The following is taken from AplPy overlays
if isinstance(scale_length, u.Quantity):
    barlength = scale_length.to(u.degree).value
elif isinstance(scale_length, u.Unit):
    barlength = length.to(u.degree)
if wcs.is_celestial:
    pix_scale = wcsutils.proj_plane_pixel_scales(wcs)
else:
    print("you can't do that in this coordinate system")
    pix_scale = [1.0, 1.0]
degrees_per_pixel = np.sqrt(pix_scale[0] * pix_scale[1])
barlength = barlength / degrees_per_pixel

Activity

  1. kakirastern commented on Apr 14, 2020

    @kakirastern
    Contributor

    @eblur Yeah, I agree that it would be a good idea!

  2. kakirastern commented on Apr 28, 2020

    @kakirastern
    Contributor

    Have used matplotlib's axes.arrow for the plotting of a scale bar on an image in the Celestial Coordinates 1 (or celestial-coords1) notebook on Google Colab, with a wcs transform thrown in so that I can directly enter the degrees for the attributes for plotting. Maybe this simplified version would be more suitable for our purposes.

  3. keflavich commented on Jul 6, 2021

    @keflavich
    Contributor

    Even though this was closed... I don't see this material in the tutorials. This is the snippet I use. Should we consider adding to core?

    def make_scalebar(ax, left_side, length, color='w', linestyle='-', label='',
                      fontsize=12, text_offset=0.1*u.arcsec):
        axlims = ax.axis()
        lines = ax.plot(u.Quantity([left_side.ra, left_side.ra-length]),
                        u.Quantity([left_side.dec]*2),
                        color=color, linestyle=linestyle, marker=None,
                        transform=ax.get_transform('fk5'),
                       )
        txt = ax.text((left_side.ra-length/2).to(u.deg).value,
                      (left_side.dec+text_offset).to(u.deg).value,
                      label,
                      verticalalignment='bottom',
                      horizontalalignment='center',
                      transform=ax.get_transform('fk5'),
                      color=color,
                      fontsize=fontsize,
                     )
        ax.axis(axlims)
        return lines,txt
  4. kelle commented on Jul 8, 2021

    @kelle
    Member

    There is an implementation of a scale bar in Section 3 of Working with Celestial Coords with WCS I. So, as far as I'm concerned, from a Tutorial perspective, this is addressed.

    If you want to propose to add it to core -- which makes total sense -- I assume you'd want to open that issue/PR in a different repo.

    fig = plt.figure(figsize=(10, 10), frameon=False)
    ax = plt.subplot(projection=wcs_helix)
    ax.arrow(337, -21.2, 0, 0.1,
             head_width=0, head_length=0,
             fc='white', ec='white', width=0.003,
             transform=ax.get_transform('icrs'))
    plt.text(337.05, -21.18, '0.1 deg',
             color='white', rotation=90,
             transform=ax.get_transform('icrs'))
    plt.imshow(image, origin='lower', cmap='cividis', aspect='equal')
    plt.xlabel(r'RA')
    plt.ylabel(r'Dec')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions