How does one insert statistical annotations (stars or p-values)

A brace / bracket can be plotted direct with matplotlib.pyplot.plot or matplotlib.axes.Axes.plot, and annotations can be added with matplotlib.pyplot.text or matplotlib.axes.Axes.text. seaborn categorical plots are 0 indexed, whereas box plots, by default, with matplotlib and pandas, start at range(1, N+1), which can be adjusted with the positions parameter. seaborn is a high-level API for matplotlib, … Read more

automatically position text box in plot

Just use annotate and specify axis coordinates. For example, “upper left” would be: plt.annotate(‘Something’, xy=(0.05, 0.95), xycoords=”axes fraction”) You could also get fancier and specify a constant offset in points: plt.annotate(‘Something’, xy=(0, 1), xytext=(12, -12), va=”top” xycoords=”axes fraction”, textcoords=”offset points”) For more explanation see the examples here and the more detailed examples here.

How to place inline labels in a line plot

Update: User cphyc has kindly created a Github repository for the code in this answer (see here), and bundled the code into a package which may be installed using pip install matplotlib-label-lines. Pretty Picture: In matplotlib it’s pretty easy to label contour plots (either automatically or by manually placing labels with mouse clicks). There does … Read more

How to display the value on horizontal bars

Update: there’s a built in method for this now! Scroll down a couple answers to “New in matplotlib 3.4.0”. If you can’t upgrade that far, it doesn’t take much code. Add: for i, v in enumerate(y): ax.text(v + 3, i + .25, str(v), color=”blue”, fontweight=”bold”) result: The y-values v are both the x-location and the … Read more

How to avoid overlapping of labels & autopct in a pie chart

Alternatively you can put the legends beside the pie graph: import matplotlib.pyplot as plt import numpy as np x = np.char.array([‘Jan’,’Feb’,’Mar’,’Apr’,’May’,’Jun’,’Jul’,’Aug’,’Sep’,’Oct’, ‘Nov’,’Dec’]) y = np.array([234, 64, 54,10, 0, 1, 0, 9, 2, 1, 7, 7]) colors = [‘yellowgreen’,’red’,’gold’,’lightskyblue’,’white’,’lightcoral’,’blue’,’pink’, ‘darkgreen’,’yellow’,’grey’,’violet’,’magenta’,’cyan’] porcent = 100.*y/y.sum() patches, texts = plt.pie(y, colors=colors, startangle=90, radius=1.2) labels = [‘{0} – {1:1.2f} %’.format(i,j) … Read more

How to add percentages on top of grouped bars

The seaborn.catplot organizing function returns a FacetGrid, which gives you access to the fig, the ax, and its patches. If you add the labels when nothing else has been plotted you know which bar-patches came from which variables. From @LordZsolt’s answer I picked up the order argument to catplot: I like making that explicit because … Read more

How do I use matplotlib autopct?

autopct enables you to display the percent value using Python string formatting. For example, if autopct=”%.2f”, then for each pie wedge, the format string is ‘%.2f’ and the numerical percent value for that wedge is pct, so the wedge label is set to the string ‘%.2f’%pct. import matplotlib.pyplot as plt plt.figure() values = [3, 12, … Read more