Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions matplotlib_scalebar/scalebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def __init__(
self,
dx,
units="m",
dimension="si-length",
dimension="auto",
label=None,
length_fraction=None,
height_fraction=None,
Expand Down Expand Up @@ -230,6 +230,7 @@ def __init__(
* ``:const:`pixel-length```: scale bar showing px, kpx, Mpx, etc.
* ``:const:`angle```: scale bar showing \u00b0, \u2032 or \u2032\u2032.
* a :class:`matplotlib_scalebar.dimension._Dimension` object
* ``:const:`auto``` (the default): autodetect dimension based on *units*
:type dimension: :class:`str` or
:class:`matplotlib_scalebar.dimension._Dimension`

Expand Down Expand Up @@ -355,7 +356,14 @@ def __init__(
raise ValueError("loc and location are specified and not equal")

self.dx = dx
self.dimension = dimension # Should be initialize before units
if dimension == "auto":
for cls in _DIMENSION_LOOKUP.values():
dimension = cls()
if dimension.is_valid_units(units):
break
else:
raise ValueError(f"Invalid unit ({units})")
self.dimension = dimension # Should be initialized before units
self.units = units
self.label = label
self.length_fraction = length_fraction
Expand Down
5 changes: 5 additions & 0 deletions tests/test_scalebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,8 @@ def test_info():

plt.close()
del fig


def test_auto_dimension():
assert ScaleBar(1, "kpc").dimension.base_units == "pc"
assert ScaleBar(1, "1/um").dimension.base_units == "1/m"