Skip to content

Commit 59e4ada

Browse files
committed
Merge pull request matplotlib#2158 from magnunor/anchored_size_bar_height
Changes to anchored_artists.AnchoredSizeBar
2 parents 48b6c13 + 929e823 commit 59e4ada

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

lib/mpl_toolkits/axes_grid1/anchored_artists.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,60 @@ def __init__(self, transform, width, height, angle, loc,
6565

6666
class AnchoredSizeBar(AnchoredOffsetbox):
6767
def __init__(self, transform, size, label, loc,
68-
pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True,
68+
pad=0.1, borderpad=0.1, sep=2, prop=None,
69+
frameon=True, size_vertical=0, color='black',
70+
label_top=False,
6971
**kwargs):
7072
"""
7173
Draw a horizontal bar with the size in data coordinate of the give axes.
7274
A label will be drawn underneath (center-aligned).
7375
74-
pad, borderpad in fraction of the legend font size (or prop)
75-
sep in points.
76+
Parameters:
77+
-----------
78+
transform : matplotlib transformation object
79+
size : int or float
80+
horizontal length of the size bar, given in data coordinates
81+
label : str
82+
loc : int
83+
pad : int or float, optional
84+
in fraction of the legend font size (or prop)
85+
borderpad : int or float, optional
86+
in fraction of the legend font size (or prop)
87+
sep : int or float, optional
88+
in points
89+
frameon : bool, optional
90+
if True, will draw a box around the horizontal bar and label
91+
size_vertical : int or float, optional
92+
vertical length of the size bar, given in data coordinates
93+
color : str, optional
94+
color for the size bar and label
95+
label_top : bool, optional
96+
if true, the label will be over the rectangle
97+
98+
Example:
99+
--------
100+
>>>> import matplotlib.pyplot as plt
101+
>>>> import numpy as np
102+
>>>> from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
103+
>>>> fig, ax = plt.subplots()
104+
>>>> ax = imshow(np.random.random((10,10)))
105+
>>>> 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')
106+
>>>> ax.add_artist(bar)
107+
>>>> plt.show()
108+
76109
"""
110+
77111
self.size_bar = AuxTransformBox(transform)
78-
self.size_bar.add_artist(Rectangle((0,0), size, 0, fc="none"))
112+
self.size_bar.add_artist(Rectangle((0,0), size, size_vertical, fill=True, facecolor=color, edgecolor=color))
79113

80114
self.txt_label = TextArea(label, minimumdescent=False)
81115

82-
self._box = VPacker(children=[self.size_bar, self.txt_label],
116+
if label_top:
117+
_box_children = [self.txt_label, self.size_bar]
118+
else:
119+
_box_children = [self.size_bar, self.txt_label]
120+
121+
self._box = VPacker(children=_box_children,
83122
align="center",
84123
pad=0, sep=sep)
85124

0 commit comments

Comments
 (0)