diff --git a/changelog.md b/changelog.md index c68fc4b..c7c4feb 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,7 @@ ## [0.11.0] - WIP ### Changed - Using new Matplotlib `pcolormesh` API introduced in v3.7.0 to plot sky map with projections faster with less memory +- Add option to plot grid in sky map with projection (e.g., "aitoff") ## [0.10.0] - 1 March 2023 ### Changed diff --git a/docs/source/matplotlib_imgs/single_lmcsmc.jpg b/docs/source/matplotlib_imgs/single_lmcsmc.jpg index 7718809..5b97e18 100644 Binary files a/docs/source/matplotlib_imgs/single_lmcsmc.jpg and b/docs/source/matplotlib_imgs/single_lmcsmc.jpg differ diff --git a/docs/source/matplotlib_single.rst b/docs/source/matplotlib_single.rst index 430ccaa..65d477e 100644 --- a/docs/source/matplotlib_single.rst +++ b/docs/source/matplotlib_single.rst @@ -83,3 +83,28 @@ MilkyWay Sky Map mw1.scatter(lsmc_ra, lsmc_dec, c="r", s=200) .. image:: matplotlib_imgs/single_lmcsmc.jpg + +You can also plot with grid +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. 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 + # grid: whether to show the grid + mw1 = MWSkyMap(projection="aitoff", grid=True) + + # set up plot title + mw1.title = "LMC and SMC in red dots" + + # 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_grid.jpg diff --git a/mw_plot/mw_plot_masters.py b/mw_plot/mw_plot_masters.py index d1aa96a..a10f6ad 100644 --- a/mw_plot/mw_plot_masters.py +++ b/mw_plot/mw_plot_masters.py @@ -262,7 +262,7 @@ class MWSkyMapMaster(MWPlotMaster): MWSkyMap master class """ - def __init__(self, grayscale, projection, center, radius, figsize, dpi): + def __init__(self, grayscale, projection, center, radius, figsize, dpi, grid=False): if projection in [ "equirectangular", "aitoff", @@ -279,11 +279,16 @@ def __init__(self, grayscale, projection, center, radius, figsize, dpi): self._grayscale = grayscale self.figsize = figsize self.dpi = dpi + self.grid = grid self._gh_imgbase_url = ( "https://github.com/henrysky/milkyway_plot/raw/master/mw_plot/" ) self._initialized = False + self._opposite_color = "white" + if self._grayscale: + self._opposite_color = "black" + # check if running in browser-based ipython (aka jupyter) self._in_jupyter = False try: diff --git a/mw_plot/mw_plot_matplotlib.py b/mw_plot/mw_plot_matplotlib.py index 68f1cf5..91f7e3d 100644 --- a/mw_plot/mw_plot_matplotlib.py +++ b/mw_plot/mw_plot_matplotlib.py @@ -439,6 +439,8 @@ class MWSkyMap(MWSkyMapMaster): :type radius: astropy.Quantity :param grayscale: whether to use grayscale background :type grayscale: bool + :param grid: whether to show grid + :type grid: bool :param figsize: Matplotlib figure size :type figsize: turple @@ -452,6 +454,7 @@ def __init__( center=(0, 0) * u.deg, radius=(180, 90) * u.deg, grayscale=False, + grid=False, figsize=(10, 6.5), dpi=None, ): @@ -462,6 +465,7 @@ def __init__( radius=radius, figsize=figsize, dpi=dpi, + grid=grid, ) self._unit = u.degree self.fontsize = 20 @@ -616,10 +620,15 @@ def initialize_mwplot(self, fig=None, ax=None): length=self.fontsize / 2, ) if self.title is not None: - ax.set_title(self.title, fontsize=self.fontsize) + ax.set_title(self.title, fontsize=self.fontsize, y=1.05) self.fig, self.ax = fig, ax self._initialized = True + if self.grid is True: + for i in [0, -15, 15, -30, 30, -45, 45, -60, 60, -75, 75]: + self.ax.plot(np.deg2rad([-180, 180]), np.deg2rad([i, i]), c=self._opposite_color, lw=0.5, zorder=3) + 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) def show(self, *args, **kwargs): if self.fig is None: