From cf9cd634695c006afa0b7a27a4edf097ad4f1686 Mon Sep 17 00:00:00 2001 From: Magnus Nord Date: Fri, 8 Nov 2013 16:12:35 +0100 Subject: [PATCH 1/3] Improvements to anchored_artists.AnchoredSizeBar Fix: color argument was not setting the color of the textlabel Add: setting the font-size through the fontsize argument --- lib/mpl_toolkits/axes_grid1/anchored_artists.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/mpl_toolkits/axes_grid1/anchored_artists.py b/lib/mpl_toolkits/axes_grid1/anchored_artists.py index ce783339034b..9cfb756adbe0 100644 --- a/lib/mpl_toolkits/axes_grid1/anchored_artists.py +++ b/lib/mpl_toolkits/axes_grid1/anchored_artists.py @@ -71,7 +71,7 @@ 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, fontsize=12, **kwargs): """ Draw a horizontal bar with the size in data coordinate of the give axes. @@ -97,7 +97,9 @@ 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 + fontsize : int, optional + sets the fontsize for the label Example: -------- @@ -106,16 +108,19 @@ def __init__(self, transform, size, label, loc, >>>> 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') + >>>> 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', fontsize=20) >>>> ax.add_artist(bar) - >>>> plt.show() + >>>> fig.show() """ 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) + self.txt_label = TextArea( + label, + minimumdescent=False, + textprops=dict(color=color, fontsize=fontsize)) if label_top: _box_children = [self.txt_label, self.size_bar] From a693d000463dea7a9decc6a6d6d6711e15a6934c Mon Sep 17 00:00:00 2001 From: Magnus Nord Date: Mon, 6 Jan 2014 13:53:56 +0100 Subject: [PATCH 2/3] Add fontprop parameter to anchored_artists.AnchoredSizeBar Removed the old fontsize parameter. And fix: some typos in the example in the docstring --- .../axes_grid1/anchored_artists.py | 41 +++++++++++++------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/lib/mpl_toolkits/axes_grid1/anchored_artists.py b/lib/mpl_toolkits/axes_grid1/anchored_artists.py index 9cfb756adbe0..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, fontsize=12, + 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: @@ -98,29 +98,44 @@ def __init__(self, transform, size, label, loc, color for the size bar and label label_top : bool, optional if True, the label will be over the rectangle - fontsize : int, optional - sets the fontsize for the label + 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', fontsize=20) - >>>> ax.add_artist(bar) - >>>> fig.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)) + if not fontprops: + textprops = {'color': color} + else: + textprops = {'color': color, 'fontproperties': fontprops} + self.txt_label = TextArea( label, minimumdescent=False, - textprops=dict(color=color, fontsize=fontsize)) + textprops=textprops) if label_top: _box_children = [self.txt_label, self.size_bar] From a1e29898139bc263ae9013bf20787291538bfd71 Mon Sep 17 00:00:00 2001 From: Magnus Nord Date: Fri, 10 Jan 2014 15:15:52 +0100 Subject: [PATCH 3/3] Modify AnchoredSizeBar: change fontprops argument to fontproperties --- lib/mpl_toolkits/axes_grid1/anchored_artists.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/mpl_toolkits/axes_grid1/anchored_artists.py b/lib/mpl_toolkits/axes_grid1/anchored_artists.py index 0abadbc4dd3f..fc56ec00d831 100644 --- a/lib/mpl_toolkits/axes_grid1/anchored_artists.py +++ b/lib/mpl_toolkits/axes_grid1/anchored_artists.py @@ -71,7 +71,7 @@ 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, fontprops=None, + label_top=False, fontproperties=None, **kwargs): """ Draw a horizontal bar with the size in data coordinate of the given axes. @@ -98,7 +98,7 @@ def __init__(self, transform, size, label, loc, color for the size bar and label label_top : bool, optional if True, the label will be over the rectangle - fontprops: a matplotlib.font_manager.FontProperties instance, optional + fontproperties: a matplotlib.font_manager.FontProperties instance, optional sets the font properties for the label text Returns: @@ -120,17 +120,17 @@ def __init__(self, transform, size, label, loc, >>> 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) + >>> bar = AnchoredSizeBar(ax.transData, 3, '3 units', 4, pad=0.5, sep=5, borderpad=0.5, frameon=False, size_vertical=0.5, color='white', fontproperties=fontprops) """ self.size_bar = AuxTransformBox(transform) self.size_bar.add_artist(Rectangle((0,0), size, size_vertical, fill=True, facecolor=color, edgecolor=color)) - if not fontprops: + if not fontproperties: textprops = {'color': color} else: - textprops = {'color': color, 'fontproperties': fontprops} + textprops = {'color': color, 'fontproperties': fontproperties} self.txt_label = TextArea( label,