How to add a footnote under the x-axis of a plot
You would be just use: plt.figtext(0.5, 0.01, “one text and next text”, ha=”center”, fontsize=18, bbox={“facecolor”:”orange”, “alpha”:0.5, “pad”:5})
You would be just use: plt.figtext(0.5, 0.01, “one text and next text”, ha=”center”, fontsize=18, bbox={“facecolor”:”orange”, “alpha”:0.5, “pad”:5})
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
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
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
There isn’t a clear and quick answer to this at the top of search engine results so I provide simple examples here: .1e = scientific notation with 1 decimal point (standard form) .2f = 2 decimal places .3g = 3 significant figures .4% = percentage with 4 decimal places A more detailed explanation on the … Read more
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
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
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
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
I’ve written a quick solution, which checks each annotation position against default bounding boxes for all the other annotations. If there is a collision it changes its position to the next available collision free place. It also puts in nice arrows. For a fairly extreme example, it will produce this (none of the numbers overlap): … Read more