

P = plt.Rectangle((left, bottom), width, height, fill=False)Īx.text(left, 0.5 * (bottom + top), 'right center',Īx.text(left, 0.5 * (bottom + top), 'left center',Īx.text(0.5 * (left + right), 0.5 * (bottom + top), 'middle',Īx.text(right, 0.5 * (bottom + top), 'centered',Īx.text(left, top, 'rotated\nwith newlines', You can precisely layout text in data or axes coordinates.Tested in python 3.10, matplotlib 3.5.1, seaborn 0.11.2.


This answer is relevant to seaborn, which is a high-level api for matplotlib.matplotlib is somewhat different from when the original answer was posted.ha='left' is a good set to prevent a long text to go out of the left axis when the frame is reduced (made narrower) manually. The ha and va parameters set the alignment of your text relative to the insertion point. 99, 'matplotlib', ha='left', va='top', transform=ax.transAxes)
#Add text to upper x axis matlab python free#
In this way you will always have this free space for the text.ĮDIT: here you have an example: from matplotlib import pyplot as plt The easier method is to set y_axis (ymax in ylim((ymin,ymax))) to a value a bit higher than the max y-coordinate of your points. To prevent the text to interfere with any point of your scatter is more difficult afaik. The example below places text in the center The default transform specifies that text is in data coords,Īlternatively, you can specify text in axis coords (0,0 is lower-leftĪnd 1,1 is upper-right). Text coordinates can be given relative to the axis, so the position of your text will be independent of the size of the plot:
