How to annotate point on a scatter automatically placed arrow

Basically, no, there isn’t. Layout engines that handle placing map labels similar to this are surprisingly complex and beyond the scope of matplotlib. (Bounding box intersections are actually a rather poor way of deciding where to place labels. What’s the point in writing a ton of code for something that will only work in one … Read more

How to put text outside of plots

It’s probably best to define the position in figure coordinates instead of data coordinates as you’d probably not want the text to change its position when changing the data. Using figure coordinates can be done either by specifying the figure transform (fig.transFigure) plt.text(0.02, 0.5, textstr, fontsize=14, transform=plt.gcf().transFigure) or by using the text method of the … Read more

Text box with line wrapping

The contents of this answer were merged into mpl master in https://github.com/matplotlib/matplotlib/pull/4342 and will be in the next feature release. Wow… This is a thorny problem… (And it exposes a lot of limitations in matplotlib’s text rendering…) This should (i.m.o.) be something that matplotlib has built-in, but it doesn’t. There have been a few threads … Read more

How to improve the label placement in scatter plot

Another option using my library adjustText, written specially for this purpose (https://github.com/Phlya/adjustText). from adjustText import adjust_text np.random.seed(2016) N = 50 scatter_data = np.random.rand(N, 3) fig, ax = plt.subplots() ax.scatter(scatter_data[:, 0], scatter_data[:, 1], c=scatter_data[:, 2], s=scatter_data[:, 2] * 150) labels = [‘ano_{}’.format(i) for i in range(N)] texts = [] for x, y, text in zip(scatter_data[:, 0], … Read more

How to add bold annotated text to a plot

Adding annotations / text also works in seaborn axes-level plots with the same methods. For seaborn figure-level plots, you must iterate through each axes, which isn’t shown. Bold text can be specified with .text or .annotate matplotlib.pyplot.text Use the weight or fontweight parameter. matplotlib.pyplot.annotate, which uses the same kwargs as .text. Note: If LaTex fonts, … Read more

Different font sizes in the same annotation of matplotlib

Make your plot first, then use ax.annotate, iterating over your x coordinates, y coordinates, labels and fontsizes. import matplotlib.pyplot as plt X = [1,2,3,4,5] Y = [1,1,1,1,1] labels=”ABCDE” sizes = [10, 15, 20, 25, 30] fig, ax = plt.subplots() ax.scatter(X, Y) for x, y, label, size in zip(X, Y, labels, sizes): ax.annotate(label, (x, y), fontsize=size) … Read more

unique plot marker for each plot

itertools.cycle will iterate over a list or tuple indefinitely. This is preferable to a function which randomly picks markers for you. Python 2.x import itertools marker = itertools.cycle((‘,’, ‘+’, ‘.’, ‘o’, ‘*’)) for n in y: plt.plot(x,n, marker = marker.next(), linestyle=””) Python 3.x import itertools marker = itertools.cycle((‘,’, ‘+’, ‘.’, ‘o’, ‘*’)) for n in … Read more