Skip to content

Commit

Permalink
Merge pull request #25 from ppinard/angular
Browse files Browse the repository at this point in the history
Fix issue #19
  • Loading branch information
ppinard authored Jul 12, 2019
2 parents 8c9f31a + 7b1930f commit 14c2187
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 70 deletions.
13 changes: 5 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
sudo: false
language: python
dist: xenial
cache:
directories:
- $HOME/.pip-cache/
pip: true
python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"
- "3.7-dev"
- "3.7"
install:
- pip install --cache-dir $HOME/.pip-cache --upgrade pip codecov
- pip install --cache-dir $HOME/.pip-cache --upgrade -r requirements.txt
- pip install --upgrade pip codecov
- pip install --upgrade -r requirements.txt
- python setup.py develop
script:
- nosetests
Expand Down
21 changes: 11 additions & 10 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ matplotlib-scalebar

.. image:: https://badge.fury.io/py/matplotlib-scalebar.svg
:target: http://badge.fury.io/py/matplotlib-scalebar

Provides a new artist for matplotlib to display a scale bar, aka micron bar.
It is particularly useful when displaying calibrated images plotted using
plt.imshow(...).
Expand Down Expand Up @@ -34,15 +34,15 @@ How to use
There are two modes of operation:

1. Length, value and units of the scale bar are automatically
determined based on the specified pixel size *dx* and
*length_fraction*.
determined based on the specified pixel size *dx* and
*length_fraction*.
The value will only take the following numbers:
1, 2, 5, 10, 15, 20, 25, 50, 75, 100, 125, 150, 200, 500 or 750.
2. The desired value and units are specified by the user

2. The desired value and units are specified by the user
(*fixed_value* and *fixed_units*) and the length is calculated
based on the specified pixel size *dx*.

The constructor arguments *dx* and *units* specify the pixel dimension.
For example ``scalebar = ScaleBar(0.2, 'um')`` indicates that each pixel is
equal to 0.2 micrometer.
Expand Down Expand Up @@ -98,6 +98,7 @@ Here are parameters of the **ScaleBar** class constructor.
* ``imperial-length``: scale bar showing in, ft, yd, mi, etc.
* ``si-length-reciprocal``: scale bar showing 1/m, 1/cm, etc.
* ``pixel-length``: scale bar showing px, kpx, Mpx, etc.
* ``angle``: scale bar showing °, ʹ (minute of arc) or ʹʹ (second of arc).
* a ``matplotlib_scalebar.dimension._Dimension`` object

* ``label``: optional label associated with the scale bar
Expand Down Expand Up @@ -131,7 +132,7 @@ Here are parameters of the **ScaleBar** class constructor.
* ``label_formatter``: custom function called to format the scalebar text.
Needs to take 2 arguments - the scale value and the unit.
(default: ``None`` which results in ``<value> <unit>``)
* ``fixed_value``: value for the scale bar. If ``None``, the value is
* ``fixed_value``: value for the scale bar. If ``None``, the value is
automatically determined based on *length_fraction*.
* ``fixed_units``: units of the *fixed_value*. If ``None`` and
*fixed_value* is not ``None``, the units of *dx* are used.
Expand Down Expand Up @@ -190,14 +191,14 @@ Contributors
------------

`@maweigert <https://github.com/maweigert>`_,
`@crosbyla <https://github.com/crosbyla>`_,
`@joschkazj <https://github.com/joschkazj>`_,
`@crosbyla <https://github.com/crosbyla>`_,
`@joschkazj <https://github.com/joschkazj>`_,
`@AKuederle <https://github.com/AKuederle>`_,
`@habi <https://github.com/habi>`_,
`@huangziwei <https://github.com/huangziwei>`_,
`@SirJohnFranklin <https://github.com/SirJohnFranklin>`_,
`@alexandrejaguar <https://github.com/alexandrejaguar>`_,
`@parishcm <https://github.com/parishcm>`_ and
`@parishcm <https://github.com/parishcm>`_ and
`@wiai <https://github.com/wiai>`_

License
Expand Down
23 changes: 23 additions & 0 deletions doc/example_angular.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
from matplotlib_scalebar.scalebar import ScaleBar, ANGULAR

delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X ** 2 - Y ** 2)
Z2 = np.exp(-(X - 1) ** 2 - (Y - 1) ** 2)
Z = (Z1 - Z2) * 2

fig, axes = plt.subplots(1, 3, figsize=(9, 3))

for ax, dx in zip(axes, [delta, delta / 60, delta / 3600]):
ax.imshow(Z)

scalebar = ScaleBar(dx, "deg", ANGULAR)
ax.add_artist(scalebar)

ax.set_title('dx = {:.6f}deg'.format(dx))

fig.savefig("example_angular.png")
31 changes: 23 additions & 8 deletions matplotlib_scalebar/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,21 @@ class _Dimension(object):
def __init__(self, base_units, latexrepr=None):
self._base_units = base_units
self._units = {base_units: 1.0}

if latexrepr is None:
latexrepr = base_units
self._latexrepr = {base_units: latexrepr}

def add_units(self, units, factor, latexrepr=None):
"""
Add new possible units.
:arg units: units
:type units: :class:`str`
:arg factor: multiplication factor to convert new units into base units
:arg factor: multiplication factor to convert new units into base units
:type factor: :class:`float`
:arg latexrepr: LaTeX representation of units (if ``None``, use *units)
:type latexrepr: :class:`str`
"""
Expand Down Expand Up @@ -79,14 +80,17 @@ def to_latex(self, units):
raise ValueError('Unknown units: %s' % units)
return self._latexrepr[units]

def create_label(self, value, latexrepr):
return '{} {}'.format(value, latexrepr)

@property
def base_units(self):
return self._base_units

class SILengthDimension(_Dimension):

def __init__(self):
super(SILengthDimension, self).__init__('m')
super().__init__('m')
for prefix, factor in _PREFIXES_FACTORS.items():
latexrepr = None
if prefix == u'\u00b5' or prefix == 'u':
Expand All @@ -96,7 +100,7 @@ def __init__(self):
class SILengthReciprocalDimension(_Dimension):

def __init__(self):
super(SILengthReciprocalDimension, self).__init__('1/m', 'm$^{-1}$')
super().__init__('1/m', 'm$^{-1}$')
for prefix, factor in _PREFIXES_FACTORS.items():
latexrepr = '{0}m$^{{-1}}$'.format(prefix)
if prefix == u'\u00b5' or prefix == 'u':
Expand All @@ -106,7 +110,7 @@ def __init__(self):
class ImperialLengthDimension(_Dimension):

def __init__(self):
super(ImperialLengthDimension, self).__init__('ft')
super().__init__('ft')
self.add_units('th', 1 / 12000)
self.add_units('in', 1 / 12)
self.add_units('yd', 3)
Expand All @@ -118,8 +122,19 @@ def __init__(self):
class PixelLengthDimension(_Dimension):

def __init__(self):
super(PixelLengthDimension, self).__init__('px')
super().__init__('px')
for prefix, factor in _PREFIXES_FACTORS.items():
if factor < 1:
continue
self.add_units(prefix + 'px', factor)

class AngleDimension(_Dimension):

def __init__(self):
super().__init__('deg', '$^\\circ$')
self.add_units("'", 1 / 60, '$^\\prime$')
self.add_units("''", 1 / 3600, '$^{\\prime\\prime}$')

def create_label(self, value, latexrepr):
# Overriden to remove space between value and units.
return '{}{}'.format(value, latexrepr)
Loading

0 comments on commit 14c2187

Please sign in to comment.