Created
August 26, 2013 01:45
-
-
Save tacaswell/6337457 to your computer and use it in GitHub Desktop.
gist for working on annotation range function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_range_annotation(ax, start, end, txt_str, y_height=.5, txt_kwargs=None, arrow_kwargs=None): | |
""" | |
Adds horizontal arrow annotation with text in the middle | |
Parameters | |
---------- | |
ax : matplotlib.Axes | |
The axes to draw to | |
start : float | |
start of line | |
end : float | |
end of line | |
txt_str : string | |
The text to add | |
y_height : float | |
The height of the line | |
txt_kwargs : dict or None | |
Extra kwargs to pass to the text | |
arrow_kwargs : dict or None | |
Extra kwargs to pass to the annotate | |
Returns | |
------- | |
tuple | |
(annotation, text) | |
""" | |
if txt_kwargs is None: | |
txt_kwargs = {} | |
if arrow_kwargs is None: | |
# default to your arrowprops | |
arrow_kwargs = {'arrowprops':dict(arrowstyle="<->", | |
connectionstyle="bar", | |
ec="k", | |
shrinkA=5, shrinkB=5, | |
)} | |
trans = ax.get_xaxis_transform() | |
ann = ax.annotate('', xy=(start, y_height), | |
xytext=(end, y_height), | |
transform=trans, | |
**arrow_kwargs) | |
txt = ax.text((start + end) / 2, | |
y_height + .05, | |
txt_str, | |
**txt_kwargs) | |
if plt.isinteractive(): | |
plt.draw() | |
return ann, txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
and I think if you pass in different
arrowprops
intoarrow_kwargs
you can do some of what you want.