Skip to content

Commit 793eea0

Browse files
authored
Add common astronomical lengths (#56)
* Add astronomical length units * add astro-length to README * Fix bugs
1 parent 343c40f commit 793eea0

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ Dimension of *dx* and *units*. It can either be equal:
169169

170170
* `si-length` (default): scale bar showing km, m, cm, etc.
171171
* `imperial-length`: scale bar showing in, ft, yd, mi, etc.
172+
* `astro-length`: scale bar showing pc, kpc, Mpc, ly, AU, etc.
172173
* `si-length-reciprocal`: scale bar showing 1/m, 1/cm, etc.
173174
* `pixel-length`: scale bar showing px, kpx, Mpx, etc.
174175
* `angle`: scale bar showing °, ʹ (minute of arc) or ʹʹ (second of arc)

matplotlib_scalebar/dimension.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,18 @@ def __init__(self):
136136
self.add_units("lea", 15840)
137137

138138

139+
class AstronomicalLengthDimension(_Dimension):
140+
def __init__(self):
141+
super().__init__("pc")
142+
for prefix, factor in _PREFIXES_FACTORS.items():
143+
latexrepr = None
144+
if prefix == "\u00b5" or prefix == "u":
145+
latexrepr = _LATEX_MU + "pc"
146+
self.add_units(prefix + "pc", factor, latexrepr)
147+
self.add_units("ly", 0.30659485)
148+
self.add_units("AU", 4.84813681e-06)
149+
150+
139151
class PixelLengthDimension(_Dimension):
140152
def __init__(self):
141153
super().__init__("px")

matplotlib_scalebar/scalebar.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"SI_LENGTH",
3434
"SI_LENGTH_RECIPROCAL",
3535
"IMPERIAL_LENGTH",
36+
"ASTRO_LENGTH",
3637
"PIXEL_LENGTH",
3738
]
3839

@@ -66,6 +67,7 @@
6667
SILengthDimension,
6768
SILengthReciprocalDimension,
6869
ImperialLengthDimension,
70+
AstronomicalLengthDimension,
6971
PixelLengthDimension,
7072
AngleDimension,
7173
)
@@ -126,13 +128,15 @@ def _validate_legend_loc(loc):
126128
SI_LENGTH = "si-length"
127129
SI_LENGTH_RECIPROCAL = "si-length-reciprocal"
128130
IMPERIAL_LENGTH = "imperial-length"
131+
ASTRO_LENGTH = "astro-length"
129132
PIXEL_LENGTH = "pixel-length"
130133
ANGLE = "angle"
131134

132135
_DIMENSION_LOOKUP = {
133136
SI_LENGTH: SILengthDimension,
134137
SI_LENGTH_RECIPROCAL: SILengthReciprocalDimension,
135138
IMPERIAL_LENGTH: ImperialLengthDimension,
139+
ASTRO_LENGTH: AstronomicalLengthDimension,
136140
PIXEL_LENGTH: PixelLengthDimension,
137141
ANGLE: AngleDimension,
138142
}
@@ -212,6 +216,7 @@ def __init__(
212216
* ``:const:`si-length```: scale bar showing km, m, cm, etc.
213217
* ``:const:`imperial-length```: scale bar showing in, ft, yd, mi, etc.
214218
* ``:const:`si-length-reciprocal```: scale bar showing 1/m, 1/cm, etc.
219+
* ``:const:`astro-length```: scale bar showing pc, kpc, Mpc, ly, AU, etc.
215220
* ``:const:`pixel-length```: scale bar showing px, kpx, Mpx, etc.
216221
* ``:const:`angle```: scale bar showing \u00b0, \u2032 or \u2032\u2032.
217222
* a :class:`matplotlib_scalebar.dimension._Dimension` object

0 commit comments

Comments
 (0)