Note

Go to the endto download the full example code.

transforms.offset_copy#

This illustrates the use oftransforms.offset_copy tomake a transform that positions a drawing element such asa text string at a specified offset in screen coordinates(dots or inches) relative to a location given in anycoordinates.

Every Artist (Text, Line2D, etc.) has a transform that can beset when the Artist is created, such as by the correspondingpyplot function. By default, this is usually the Axes.transDatatransform, going from data units to screen pixels. We canuse theoffset_copy function to make a modified copy ofthis transform, where the modification consists of anoffset.

transoffset
importmatplotlib.pyplotaspltimportnumpyasnpimportmatplotlib.transformsasmtransformsxs=np.arange(7)ys=xs**2fig=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')forx,yinzip(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')forx,yinzip(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()

Gallery generated by Sphinx-Gallery