8000 Merge pull request #2712 from pelson/anchored_sizebar_fontprop · OceanWolf/matplotlib@e3e55fd · GitHub
[go: up one dir, main page]

Skip to content

Commit e3e55fd

Browse files
committed
Merge pull request matplotlib#2712 from pelson/anchored_sizebar_fontprop
Anchored sizebar fontprop
2 parents 113c352 + c09cf41 commit e3e55fd

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

lib/mpl_toolkits/axes_grid1/anchored_artists.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ class AnchoredSizeBar(AnchoredOffsetbox):
7171
def __init__(self, transform, size, label, loc,
7272
pad=0.1, borderpad=0.1, sep=2, prop=None,
7373
frameon=True, size_vertical=0, color='black',
74-
label_top=False,
74+
label_top=False, fontproperties=None,
7575
**kwargs):
7676
"""
77-
Draw a horizontal bar with the size in data coordinate of the give axes.
77+
Draw a horizontal bar with the size in data coordinate of the given axes.
7878
A label will be drawn underneath (center-aligned).
7979
8080
Parameters:
@@ -97,25 +97,45 @@ def __init__(self, transform, size, label, loc,
9797< 8000 /code>
color : str, optional
9898
color for the size bar and label
9999
label_top : bool, optional
100-
if true, the label will be over the rectangle
100+
if True, the label will be over the rectangle
101+
fontproperties: a matplotlib.font_manager.FontProperties instance, optional
102+
sets the font properties for the label text
103+
104+
Returns:
105+
--------
106+
AnchoredSizeBar object
101107
102108
Example:
103109
--------
104-
>>>> import matplotlib.pyplot as plt
105-
>>>> import numpy as np
106-
>>>> from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
107-
>>>> fig, ax = plt.subplots()
108-
>>>> ax = imshow(np.random.random((10,10)))
109-
>>>> 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')
110-
>>>> ax.add_artist(bar)
111-
>>>> plt.show()
110+
>>> import matplotlib.pyplot as plt
111 8000 +
>>> import numpy as np
112+
>>> from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
113+
>>> fig, ax = plt.subplots()
114+
>>> ax.imshow(np.random.random((10,10)))
115+
>>> bar = AnchoredSizeBar(ax.transData, 3, '3 units', 4)
116+
>>> ax.add_artist(bar)
117+
>>> fig.show()
118+
119+
Using all the optional parameters
120+
121+
>>> import matplotlib.font_manager as fm
122+
>>> fontprops = fm.FontProperties(size=14, family='monospace')
123+
>>> 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)
112124
113125
"""
114126

115127
self.size_bar = AuxTransformBox(transform)
116128
self.size_bar.add_artist(Rectangle((0,0), size, size_vertical, fill=True, facecolor=color, edgecolor=color))
117129

118-
self.txt_label = TextArea(label, minimumdescent=False)
130+
if not fontproperties:
131+
textprops = {'color': color}
132+
else:
133+
textprops = {'color': color, 'fontproperties': fontproperties}
134+
135+
self.txt_label = TextArea(
136+
label,
137+
minimumdescent=False,
138+
textprops=textprops)
119139

120140
if label_top:
121141
_box_children = [self.txt_label, self.size_bar]

0 commit comments

Comments
 (0)
0