From 28ec5bd29ff14705dda1288bd81d5b8522065246 Mon Sep 17 00:00:00 2001 From: "Adrien F. Vincent" Date: Tue, 25 Oct 2016 13:45:20 +0200 Subject: [PATCH] Fix annotation position --- examples/lines_bars_and_markers/linestyles.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/examples/lines_bars_and_markers/linestyles.py b/examples/lines_bars_and_markers/linestyles.py index c61276a6d6f5..524134dab6fd 100644 --- a/examples/lines_bars_and_markers/linestyles.py +++ b/examples/lines_bars_and_markers/linestyles.py @@ -4,6 +4,7 @@ import numpy as np import matplotlib.pyplot as plt from collections import OrderedDict +from matplotlib.transforms import blended_transform_factory linestyles = OrderedDict( [('solid', (0, ())), @@ -35,9 +36,13 @@ plt.yticks(np.arange(len(linestyles)), linestyles.keys()) plt.xticks([]) +# For each line style, add a text annotation with a small offset from +# the reference point (0 in Axes coords, y tick value in Data coords). +reference_transform = blended_transform_factory(ax.transAxes, ax.transData) for i, (name, linestyle) in enumerate(linestyles.items()): - ax.text(-0.5, i-0.4, str(linestyle), fontsize=8, ha="right", - color="blue", family="monospace") + ax.annotate(str(linestyle), xy=(0.0, i), xycoords=reference_transform, + xytext=(-6, -12), textcoords='offset points', color="blue", + fontsize=8, ha="right", family="monospace") plt.tight_layout() plt.show()