diff --git a/lib/matplotlib/tests/baseline_images/test_anchored_artists/anchored_sizebar.pdf b/lib/matplotlib/tests/baseline_images/test_anchored_artists/anchored_sizebar.pdf new file mode 100644 index 000000000000..9790448961a8 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_anchored_artists/anchored_sizebar.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_anchored_artists/anchored_sizebar.png b/lib/matplotlib/tests/baseline_images/test_anchored_artists/anchored_sizebar.png new file mode 100644 index 000000000000..d65a77b5af9f Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_anchored_artists/anchored_sizebar.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_anchored_artists/anchored_sizebar.svg b/lib/matplotlib/tests/baseline_images/test_anchored_artists/anchored_sizebar.svg new file mode 100644 index 000000000000..653fe9eacbbc --- /dev/null +++ b/lib/matplotlib/tests/baseline_images/test_anchored_artists/anchored_sizebar.svg @@ -0,0 +1,562 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/matplotlib/tests/test_anchored_artists.py b/lib/matplotlib/tests/test_anchored_artists.py new file mode 100644 index 000000000000..9ea6a771ba95 --- /dev/null +++ b/lib/matplotlib/tests/test_anchored_artists.py @@ -0,0 +1,13 @@ +import matplotlib.pyplot as plt +import numpy as np +from mpl_toolkits.axes_grid1 import anchored_artists +from matplotlib.testing.decorators import image_comparison +import matplotlib.font_manager as fm + +@image_comparison(baseline_images=['anchored_sizebar']) +def test_anchored_sizebar(): + fig, ax = plt.subplots() + ax.imshow(np.arange(100).reshape(10,10)) + fontprops = fm.FontProperties(size=20, family='monospace') + bar = anchored_artists.AnchoredSizeBar(ax.transData, 3, '3 units', 4, pad=0.5, sep=5, borderpad=0.5, frameon=False, size_vertical=0.5, color='white', fontprops=fontprops) + ax.add_artist(bar) diff --git a/lib/mpl_toolkits/axes_grid1/anchored_artists.py b/lib/mpl_toolkits/axes_grid1/anchored_artists.py index ce783339034b..0abadbc4dd3f 100644 --- a/lib/mpl_toolkits/axes_grid1/anchored_artists.py +++ b/lib/mpl_toolkits/axes_grid1/anchored_artists.py @@ -71,10 +71,10 @@ class AnchoredSizeBar(AnchoredOffsetbox): def __init__(self, transform, size, label, loc, pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True, size_vertical=0, color='black', - label_top=False, + label_top=False, fontprops=None, **kwargs): """ - Draw a horizontal bar with the size in data coordinate of the give axes. + Draw a horizontal bar with the size in data coordinate of the given axes. A label will be drawn underneath (center-aligned). Parameters: @@ -97,25 +97,45 @@ def __init__(self, transform, size, label, loc, color : str, optional color for the size bar and label label_top : bool, optional - if true, the label will be over the rectangle + if True, the label will be over the rectangle + fontprops: a matplotlib.font_manager.FontProperties instance, optional + sets the font properties for the label text + + Returns: + -------- + AnchoredSizeBar object Example: -------- - >>>> import matplotlib.pyplot as plt - >>>> import numpy as np - >>>> from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar - >>>> fig, ax = plt.subplots() - >>>> ax = imshow(np.random.random((10,10))) - >>>> bar = AnchoredSizeBar(ax.transData, 3, '3 units', pad=0.5, loc=4, sep=5, borderpad=0.5, frameon=False, size_vertical=0.5, color='white') - >>>> ax.add_artist(bar) - >>>> plt.show() + >>> import matplotlib.pyplot as plt + >>> import numpy as np + >>> from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar + >>> fig, ax = plt.subplots() + >>> ax.imshow(np.random.random((10,10))) + >>> bar = AnchoredSizeBar(ax.transData, 3, '3 units', 4) + >>> ax.add_artist(bar) + >>> fig.show() + + Using all the optional parameters + + >>> import matplotlib.font_manager as fm + >>> fontprops = fm.FontProperties(size=14, family='monospace') + >>> bar = AnchoredSizeBar(ax.transData, 3, '3 units', 4, pad=0.5, sep=5, borderpad=0.5, frameon=False, size_vertical=0.5, color='white', fontprops=fontprops) """ self.size_bar = AuxTransformBox(transform) self.size_bar.add_artist(Rectangle((0,0), size, size_vertical, fill=True, facecolor=color, edgecolor=color)) - self.txt_label = TextArea(label, minimumdescent=False) + if not fontprops: + textprops = {'color': color} + else: + textprops = {'color': color, 'fontproperties': fontprops} + + self.txt_label = TextArea( + label, + minimumdescent=False, + textprops=textprops) if label_top: _box_children = [self.txt_label, self.size_bar]