Skip to content

Commit

Permalink
ge plots when building docs instead of pre-gen
Browse files Browse the repository at this point in the history
  • Loading branch information
henrysky committed Sep 4, 2023
1 parent ee6fd59 commit 1600c02
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 19 deletions.
7 changes: 6 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@
'sphinx-prompt',
'bokeh.sphinxext.bokeh_plot',
'sphinx_copybutton',
'myst_parser']
'myst_parser',
'matplotlib.sphinxext.plot_directive',
]

bokeh_plot_pyfile_include_dirs = ['bokeh_html']

plot_formats = [("png", 200), ("pdf", 200)]
plot_include_source = False

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

Expand Down
64 changes: 62 additions & 2 deletions docs/source/matplotlib_gallery.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,33 @@ Orbit of Sun
# use mw_scatter instead of scatter because we want a colorbar
mw1.mw_scatter(x, y, c=[z, 'kpc above galactic plane'], s=1)
.. image:: matplotlib_imgs/gallery_sun_single.jpg
.. plot::

import pylab as plt
from mw_plot import MWPlot

from galpy.potential import MWPotential2014
from galpy.orbit import Orbit
import numpy as np
from astropy import units as u

# Orbit Integration using galpy for the Sun
op = Orbit([0., 0., 0., 0., 0., 0.], radec=True, ro=8., vo=220.)
ts = np.linspace(0, 5, 10000) * u.Gyr
op.integrate(ts, MWPotential2014)
x = op.x(ts) * u.kpc
y = op.y(ts) * u.kpc
z = op.z(ts)

# setup a mw-plot instance of bird's eye view of the disc
mw1 = MWPlot(radius=20 * u.kpc, unit=u.kpc, coord='galactocentric', annotation=True, figsize=(15, 12), r0=8)

# set up plot title
mw1.title = 'Orbit of Sun in 5Gyr'

# use mw_scatter instead of scatter because we want a colorbar
mw1.mw_scatter(x, y, c=[z, 'kpc above galactic plane'], s=1)
plt.tight_layout()

Orbit of Sun 2
---------------
Expand Down Expand Up @@ -72,4 +98,38 @@ Orbit of Sun 2
ax1.set_title("Orbit of the Sun in XY plane", fontsize=20)
ax2.set_title("Orbit of the Sun in XZ plane", fontsize=20)
.. image:: matplotlib_imgs/gallery_sun_multi.jpg
.. plot::

import pylab as plt
from mw_plot import MWPlot
from astropy import units as u
from galpy.potential import MWPotential2014
from galpy.orbit import Orbit
import numpy as np

# Orbit Integration using galpy for the Sun
op = Orbit([0., 0., 0., 0., 0., 0.], radec=True, ro=8., vo=220.)
ts = np.linspace(0, 5, 10000) * u.Gyr
op.integrate(ts, MWPotential2014)
x = op.x(ts) * u.kpc
y = op.y(ts) * u.kpc
z = op.z(ts)

# setup a mw-plot instance of bird's eye view of the disc
mw1 = MWPlot(radius=20 * u.kpc, center=(0, 0)*u.kpc, unit=u.kpc, coord='galactocentric', annotation=False, grayscale=True)
mw2 = MWPlot(radius=10 * u.kpc, mode="edge-on", center=(0, 0)*u.kpc, unit=u.kpc, coord='galactocentric', annotation=False, grayscale=True)

# setup subplots with matplotlib
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 7.5))

# transform the whole figure with mw-plot
# mw1.transform([ax1, ax2]) will have the same effect
mw1.transform(ax1)
mw2.transform(ax2)

# you can plot something on top of the transformed subplot
ax1.scatter(x, y, c='r', s=0.1)
ax2.scatter(x, z, c='r', s=0.1)
ax1.set_title("Orbit of the Sun in XY plane", fontsize=20)
ax2.set_title("Orbit of the Sun in XZ plane", fontsize=20)
plt.tight_layout()
89 changes: 85 additions & 4 deletions docs/source/matplotlib_multi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,28 @@ Tranform a single subplot
ax1.plot([0, 1], [0, 1])
ax1.plot([0, 1], [1, 0])
.. image:: matplotlib_imgs/single1.jpg
.. plot::

import pylab as plt
from mw_plot import MWPlot
from astropy import units as u

# setup a mw-plot instance of bird's eyes view of the disc
mw1 = MWPlot(radius=20 * u.kpc, center=(0, 0)*u.kpc, unit=u.kpc, coord='galactocentric', rot90=2, grayscale=False, annotation=False)

# setup subplots with matplotlib
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 7.5))

# transform the second subplot with mw-plot
mw1.transform(ax2)

# you can plot something on top of the transformed subplot
ax2.scatter(8, 0, c='r', s=100)

# plot something in the first subplot
ax1.plot([0, 1], [0, 1])
ax1.plot([0, 1], [1, 0])
plt.tight_layout()

Tranform multiple subplots
--------------------------------
Expand Down Expand Up @@ -59,7 +80,29 @@ Tranform multiple subplots
ax1.plot([20, -20], [20, -20])
ax1.plot([20, -20], [-20, 20])
.. image:: matplotlib_imgs/multi1.jpg
.. plot::

import pylab as plt
from mw_plot import MWPlot
from astropy import units as u

# setup a mw-plot instance of bird's eyes view of the disc
mw1 = MWPlot(radius=20 * u.kpc, center=(0, 0)*u.kpc, unit=u.kpc, coord='galactocentric', rot90=2, grayscale=False, annotation=False)

# setup subplots with matplotlib
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 7.5))

# transform the whole figure with mw-plot
# mw1.transform(fig) will have the same effect
mw1.transform([ax1, ax2])

# you can plot something on top of the transformed subplot
ax2.scatter(8, 0, c='r', s=100)

# plot something in the first subplot
ax1.plot([20, -20], [20, -20])
ax1.plot([20, -20], [-20, 20])
plt.tight_layout()

Tranform subplots with different style
---------------------------------------
Expand Down Expand Up @@ -91,7 +134,29 @@ Not only you can transform with one style, you can do multiple style too
fig.tight_layout()
.. image:: matplotlib_imgs/multi2.jpg
.. plot::

import pylab as plt
from mw_plot import MWPlot, MWSkyMap
from astropy import units as u

# setup a mw-plot instance of bird's eyes view of the disc
mw1 = MWPlot(radius=20 * u.kpc, center=(0, 0)*u.kpc, unit=u.kpc, coord='galactocentric', rot90=2, grayscale=False, annotation=False)
mw2 = MWPlot(radius=20 * u.kpc, center=(0, 0)*u.kpc, unit=u.kpc, coord='galactocentric', rot90=2, grayscale=True, annotation=False)
mw3 = MWSkyMap()

# setup subplots with matplotlib
fig = plt.figure(figsize=(15, 15))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(212)

# transform the subplots with different style
mw1.transform(ax1)
mw2.transform(ax2)
mw3.transform(ax3)

plt.tight_layout()

Tranform all subplots
-----------------------
Expand All @@ -115,4 +180,20 @@ You can quickly transform all subplots in a figure
# mw1.transform([ax1, ax2]) will have the same effect
mw1.transform(fig)
.. image:: matplotlib_imgs/multi3.jpg
.. plot::

import pylab as plt
from mw_plot import MWPlot
from astropy import units as u

# setup a mw-plot instance of bird's eyes view of the disc
mw1 = MWPlot(radius=20 * u.kpc, center=(0, 0)*u.kpc, unit=u.kpc, coord='galactocentric', grayscale=False, annotation=False)

# setup subplots with matplotlib
fig, (ax_top, ax_bottom) = plt.subplots(2, 4, figsize=(20, 10))

# transform the whole figure with mw-plot
# mw1.transform([ax1, ax2]) will have the same effect
mw1.transform(fig)

plt.tight_layout()
121 changes: 115 additions & 6 deletions docs/source/matplotlib_single.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,27 @@ MilkyWay Bird's Eye
mw1.scatter(8 * u.kpc, 0 * u.kpc, c="r", s=200)
.. plot::

.. image:: matplotlib_imgs/single_earth.jpg
import pylab as plt
import numpy as np
from astropy import units as u
from mw_plot import MWPlot

# setup a mw-plot instance of bird's eye view of the disc
mw1 = MWPlot(
radius=20 * u.kpc,
unit=u.kpc,
coord="galactocentric",
annotation=True,
figsize=(15, 12),
)

# set up plot title
mw1.title = "Bird's Eyes View"

mw1.scatter(8 * u.kpc, 0 * u.kpc, c="r", s=200)
plt.tight_layout()

Annotation
^^^^^^^^^^^
Expand All @@ -57,7 +76,20 @@ Annotation
mw1.scatter_annotate(["Earth", "Galactic \n Center"], [[8.0, 0.0], [0.0, 0.0]] * u.kpc)
.. image:: matplotlib_imgs/annotation.jpg
.. plot::

import pylab as plt
import numpy as np
from astropy import units as u
from mw_plot import MWPlot

mw1 = MWPlot(radius=20 * u.kpc, unit=u.kpc, coord="galactocentric", annotation=True)

# set up plot title
mw1.title = "Annotation"

mw1.scatter_annotate(["Earth", "Galactic \n Center"], [[8.0, 0.0], [0.0, 0.0]] * u.kpc)
plt.tight_layout()

MilkyWay Sky Map
------------------
Expand All @@ -82,7 +114,26 @@ MilkyWay Sky Map
mw1.scatter(lsmc_ra, lsmc_dec, c="r", s=200)
.. image:: matplotlib_imgs/single_lmcsmc.jpg
.. plot::

import pylab as plt
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
# grayscale: whether to turn the background image to grayscale
mw1 = MWSkyMap(projection="aitoff", grayscale=False)

# 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)
plt.tight_layout()

You can also plot with grid
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -107,7 +158,26 @@ You can also plot with grid
mw1.scatter(lsmc_ra, lsmc_dec, c="r", s=200)
.. image:: matplotlib_imgs/single_lmcsmc_w_grid.jpg
.. plot::

import pylab as plt
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 Galactic grid
mw1 = MWSkyMap(projection="aitoff", grid=True)

# set up plot title
mw1.title = "LMC and SMC in red dots with Galactic 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)
plt.tight_layout()

.. code-block:: python
:linenos:
Expand All @@ -129,7 +199,27 @@ You can also plot with grid
mw1.scatter(lsmc_ra, lsmc_dec, c="r", s=200)
.. image:: matplotlib_imgs/single_lmcsmc_w_radecgrid.jpg
.. plot::

import pylab as plt
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)
plt.tight_layout()


.. code-block:: python
:linenos:
Expand All @@ -151,4 +241,23 @@ You can also plot with grid
mw1.scatter(lsmc_ra, lsmc_dec, c="r", s=200)
.. image:: matplotlib_imgs/single_lmcsmc_w_eclgrid.jpg
.. plot::

import pylab as plt
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", eclgrid=True)

# set up plot title
mw1.title = "LMC and SMC in red dots with Ecliptic 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)
plt.tight_layout()
1 change: 1 addition & 0 deletions mw_plot/mw_plot_masters.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def images_read(self):
/ float(img.shape[1])
* ((self._ext[1] - self._ext[0]) / (self._ext[3] - self._ext[2]))
)
self._aspect = np.abs(self._aspect)

self.lrbt_rot()

Expand Down
Loading

0 comments on commit 1600c02

Please sign in to comment.