Skip to content

Commit

Permalink
add showing RA/DEC grid in skymap
Browse files Browse the repository at this point in the history
  • Loading branch information
henrysky committed Aug 25, 2023
1 parent 28797e6 commit f17e764
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
Binary file modified docs/source/matplotlib_imgs/single_lmcsmc_w_grid.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 24 additions & 2 deletions docs/source/matplotlib_single.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ You can also plot with grid
from mw_plot import MWSkyMap
# setup a MWSkyMap instance with projection, other projection can be 'hammer', 'mollweide' etc
# grid: whether to show the grid
# grid: whether to show the Galactic grid
mw1 = MWSkyMap(projection="aitoff", grid=True)
# set up plot title
mw1.title = "LMC and SMC in red dots"
mw1.title = "LMC and SMC in red dots with Galactic Grid"
# LMC and SMC coordinates
lsmc_ra = [78.77, 16.26] * u.degree
Expand All @@ -108,3 +108,25 @@ You can also plot with grid
mw1.scatter(lsmc_ra, lsmc_dec, c="r", s=200)
.. image:: matplotlib_imgs/single_lmcsmc_w_grid.jpg

.. code-block:: python
:linenos:
import numpy as np
from astropy import units as u
from mw_plot import MWSkyMap
# setup a MWSkyMap instance with projection, other projection can be 'hammer', 'mollweide' etc
# radecgrid: whether to show the RA/DEC grid
mw1 = MWSkyMap(projection="aitoff", radecgrid=True)
# set up plot title
mw1.title = "LMC and SMC in red dots with RA/DEC Grid"
# LMC and SMC coordinates
lsmc_ra = [78.77, 16.26] * u.degree
lsmc_dec = [-69.01, -72.42] * u.degree
mw1.scatter(lsmc_ra, lsmc_dec, c="r", s=200)
.. image:: matplotlib_imgs/single_lmcsmc_w_radecgrid.jpg
38 changes: 37 additions & 1 deletion mw_plot/mw_plot_matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import astropy.units as u
import astropy.coordinates as apycoords
from galpy.util.coords import radec_to_lb

import pylab as plt
from matplotlib.colors import LinearSegmentedColormap
Expand Down Expand Up @@ -439,8 +440,10 @@ class MWSkyMap(MWSkyMapMaster):
:type radius: astropy.Quantity
:param grayscale: whether to use grayscale background
:type grayscale: bool
:param grid: whether to show grid
:param grid: whether to show galactic grid
:type grid: bool
:param radecgrid: whether to show ra & dec grid
:type radecgrid: bool
:param figsize: Matplotlib figure size
:type figsize: turple
Expand All @@ -455,6 +458,7 @@ def __init__(
radius=(180, 90) * u.deg,
grayscale=False,
grid=False,
radecgrid=False,
figsize=(10, 6.5),
dpi=None,
):
Expand All @@ -473,6 +477,7 @@ def __init__(
self.cmap = "viridis"
self.imalpha = 1.0
self.tight_layout = True
self.radecgrid = radecgrid

self.fig = None
self.ax = None
Expand Down Expand Up @@ -630,6 +635,37 @@ def initialize_mwplot(self, fig=None, ax=None):
for i in [-150, -120, -90, -60, -30, 0, 30, 60, 90, 120, 150]:
self.ax.plot(np.deg2rad([i, i]), np.deg2rad([-75, 75]), c=self._opposite_color, lw=0.5, zorder=3)

if self.radecgrid is True:
for i in [-75, -60, -45, -30, -15, 0, 15, 30, 45, 60, 75]:
ras = np.linspace(0, 360, 180)
des = np.linspace(i, i, 180)
l, b = radec_to_lb(ras, des, degree=True).T
l = l - 180.
if np.max(np.diff(l)) > 2.:
idx = np.argmax(l) + 1
l = np.concatenate([l[idx:], l[:idx]])
b = np.concatenate([b[idx:], b[:idx]])
idx = np.argsort(l)
self.ax.plot(np.deg2rad(l), np.deg2rad(b), c="white", lw=0.5, zorder=3)


for i in [30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330]:
ras = np.linspace(i, i, 180)
des = np.linspace(-75, 75, 180)
l, b = radec_to_lb(ras, des, degree=True).T
l = l - 180.
if np.max(np.diff(l)) > 0.5:
idx = np.argmax(np.diff(l))
idx = np.argmax(l) + 1
l = np.concatenate([l[idx:], l[:idx]])
b = np.concatenate([b[idx:], b[:idx]])
idx_break = np.argmax(np.diff(l))
self.ax.plot(np.deg2rad(l[:idx_break]), np.deg2rad(b[:idx_break]), c="white", lw=0.5, zorder=3)
self.ax.plot(np.deg2rad(l[idx_break+1:]), np.deg2rad(b[idx_break+1:]), c="white", lw=0.5, zorder=3)
else:
self.ax.plot(np.deg2rad(l), np.deg2rad(b), c="white", lw=0.5, zorder=3)


def show(self, *args, **kwargs):
if self.fig is None:
raise AttributeError("Nothing to show, please plot some data first")
Expand Down

0 comments on commit f17e764

Please sign in to comment.