Last active
April 13, 2022 22:27
-
-
Save Miladiouss/fb06d9e08efdb24f8c8a45c8b6fea503 to your computer and use it in GitHub Desktop.
Proper way of constructing and displaying quantities using Astropy in Jupyter Notebooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Science | |
import numpy as np | |
from astropy.units import Unit | |
# Notebook | |
from IPython.display import Markdown | |
from IPython.core.interactiveshell import InteractiveShell | |
InteractiveShell.ast_node_interactivity = "all" | |
# Handy functions | |
def Qshow(Q, sigfig=2, unit=None, name=None): | |
name = Q.info.name | |
if unit is not None: | |
Q = Q.to(unit) | |
if sigfig is not None: | |
q = Q.round(sigfig - int(np.log10(Q.value))) | |
else: | |
q = Q | |
if name is not None: | |
output = Markdown(f'{name} = {q._repr_latex_()}') | |
else: | |
output = Markdown(f'{q._repr_latex_()}') | |
return output | |
# Parameter | |
density = 1.23456e12 * Unit('g/cm^3') | |
density.info.name = r'$\rho$' | |
# Results | |
Qshow(density) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment