.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/misc/transoffset.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_misc_transoffset.py: ====================== transforms.offset_copy ====================== This illustrates the use of `.transforms.offset_copy` to make a transform that positions a drawing element such as a text string at a specified offset in screen coordinates (dots or inches) relative to a location given in any coordinates. Every Artist (Text, Line2D, etc.) has a transform that can be set when the Artist is created, such as by the corresponding pyplot function. By default, this is usually the Axes.transData transform, going from data units to screen pixels. We can use the `.offset_copy` function to make a modified copy of this transform, where the modification consists of an offset. .. GENERATED FROM PYTHON SOURCE LINES 20-58 .. image-sg:: /gallery/misc/images/sphx_glr_transoffset_001.png :alt: transoffset :srcset: /gallery/misc/images/sphx_glr_transoffset_001.png, /gallery/misc/images/sphx_glr_transoffset_001_2_00x.png 2.00x :class: sphx-glr-single-img .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import matplotlib.transforms as mtransforms xs = np.arange(7) ys = xs**2 fig = plt.figure(figsize=(5, 10)) ax = plt.subplot(2, 1, 1) # If we want the same offset for each text instance, # we only need to make one transform. To get the # transform argument to offset_copy, we need to make the axes # first; the subplot function above is one way to do this. trans_offset = mtransforms.offset_copy(ax.transData, fig=fig, x=0.05, y=0.10, units='inches') for x, y in zip(xs, ys): plt.plot(x, y, 'ro') plt.text(x, y, '%d, %d' % (int(x), int(y)), transform=trans_offset) # offset_copy works for polar plots also. ax = plt.subplot(2, 1, 2, projection='polar') trans_offset = mtransforms.offset_copy(ax.transData, fig=fig, y=6, units='dots') for x, y in zip(xs, ys): plt.polar(x, y, 'ro') plt.text(x, y, '%d, %d' % (int(x), int(y)), transform=trans_offset, horizontalalignment='center', verticalalignment='bottom') plt.show() .. _sphx_glr_download_gallery_misc_transoffset.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: transoffset.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: transoffset.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_