Skip to content

Commit

Permalink
Add bounding box to scale bar (#106)
Browse files Browse the repository at this point in the history
* Add bounding box to scale bar

* Fix tests
  • Loading branch information
fmaussion committed Jul 6, 2018
1 parent b1ac94a commit 0c53866
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/examples/plot_change_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
smap.set_lonlat_contours(add_ytick_labels=False, interval=5, linewidths=1.5,
linestyles='-', colors='C1')

# Add a scalebar
smap.set_scale_bar(location=(0.87, 0.04), add_bbox=True)

# done!
smap.visualize()
plt.show()
32 changes: 30 additions & 2 deletions salem/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from matplotlib.colors import LinearSegmentedColormap
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.collections import PatchCollection, LineCollection
from shapely.geometry import MultiPoint, LineString
from shapely.geometry import MultiPoint, LineString, Polygon
from descartes.patch import PolygonPatch
from matplotlib.transforms import Transform as MPLTranform
import matplotlib.path as mpath
Expand Down Expand Up @@ -933,7 +933,8 @@ def set_rgb(self, img=None, crs=None, interp='nearest',
self._rgb = np.dstack(out)

def set_scale_bar(self, location=None, length=None, maxlen=0.25,
**kwargs):
add_bbox=False, bbox_dx=1.2, bbox_dy=1.2,
bbox_kwargs=None, **kwargs):
"""Add a legend bar showing the scale to the plot.
Parameters
Expand All @@ -946,6 +947,17 @@ def set_scale_bar(self, location=None, length=None, maxlen=0.25,
maxlen : float
the maximum lenght of the bar (in the plot's relative coordinates)
when choosing the length automatically
add_bbox : bool
add a bounding box to the scale bar (WIP: experimental)
bbox_dx : bool, default: 1.2
a multiplicating factor controlling the x size of the bounding box
(trial and error works best for this one)
bbox_dy : bool, default: 1.2
a multiplicating factor controlling the y size of the bounding box
(trial and error works best for this one)
bbox_kwargs : dict
kwarg to pass to set_geometry() for the bounding box (e.g.
facecolor, alpha, etc...)
kwargs : dict
any kwarg accepted by ``set_geometry``. Defaults are put on
``color``, ``linewidth``, ``text``, ``text_kwargs``... But you can
Expand All @@ -966,6 +978,12 @@ def set_scale_bar(self, location=None, length=None, maxlen=0.25,

# coordinates for the scalebar
line = LineString(([sbcx - length/2, sbcy], [sbcx + length/2, sbcy]))
# Of the bounding box
bbox = [[sbcx - length / 2 * bbox_dx, sbcy - length / 4 * bbox_dy],
[sbcx - length / 2 * bbox_dx, sbcy + length / 4 * bbox_dy],
[sbcx + length / 2 * bbox_dx, sbcy + length / 4 * bbox_dy],
[sbcx + length / 2 * bbox_dx, sbcy - length / 4 * bbox_dy],
]

# Units
if self.grid.proj.is_latlong():
Expand All @@ -983,10 +1001,20 @@ def set_scale_bar(self, location=None, length=None, maxlen=0.25,
kwargs.setdefault('text', '{} '.format(length) + units)
kwargs.setdefault('text_delta', (0.0, 0.015))
kwargs.setdefault('linewidth', 3)
kwargs.setdefault('zorder', 99)
tkw = kwargs.get('text_kwargs', {})
tkw.setdefault('horizontalalignment', 'center')
tkw.setdefault('zorder', 99)
tkw.setdefault('color', kwargs['color'])
kwargs['text_kwargs'] = tkw
if add_bbox:
if bbox_kwargs is None:
bbox_kwargs = {}
bbox_kwargs.setdefault('facecolor', 'w')
bbox_kwargs.setdefault('edgecolor', 'k')
bbox_kwargs.setdefault('zorder', 98)
poly = Polygon(np.asarray(bbox))
self.set_geometry(poly, crs=self.grid.proj, **bbox_kwargs)
self.set_geometry(line, crs=self.grid.proj, **kwargs)

def transform(self, crs=wgs84, ax=None):
Expand Down
10 changes: 8 additions & 2 deletions salem/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,17 @@ def test_read_to_grid(self):
res = _memory_shapefile_to_grid.call_and_shelve(shape_cpath,
grid=g.grid,
**d)
h1 = res.argument_hash
try:
h1 = res.argument_hash
except AttributeError:
h1 = res.timestamp
res = _memory_shapefile_to_grid.call_and_shelve(shape_cpath,
grid=g.grid,
**d2)
h2 = res.argument_hash
try:
h2 = res.argument_hash
except AttributeError:
h2 = res.timestamp
self.assertEqual(h1, h2)


Expand Down

0 comments on commit 0c53866

Please sign in to comment.