Skip to content

Commit

Permalink
add option to show grid in skymap with projection
Browse files Browse the repository at this point in the history
  • Loading branch information
henrysky committed Aug 25, 2023
1 parent f3cdd34 commit 47f0713
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Binary file modified docs/source/matplotlib_imgs/single_lmcsmc.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions docs/source/matplotlib_single.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 6 additions & 1 deletion mw_plot/mw_plot_masters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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:
Expand Down
11 changes: 10 additions & 1 deletion mw_plot/mw_plot_matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
):
Expand All @@ -462,6 +465,7 @@ def __init__(
radius=radius,
figsize=figsize,
dpi=dpi,
grid=grid,
)
self._unit = u.degree
self.fontsize = 20
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 47f0713

Please sign in to comment.