Loading [MathJax]/extensions/tex2jax.js

News

New paper!

Tuesday, July 18, 2023

Boxplot with jitter plot in matplotlib example

def jitterplot(x, data, ax):
# x: position on x-axis
# data: datapoins in the box
# ax: specified ax
np.random.seed(0)
for c in range(len(data)):
ax.plot(np.random.normal(x-0.08,0.08), data[c], '.',color='grey',markersize=12, markeredgecolor='k',label=labels[c])
data = # array of list of datapoints. each list corresponds to a box.
names = # box names
fig, ax = plt.subplots(1,1)
_= ax.boxplot(data,showfliers=False)
_= ax.set_xticks(range(1,len(data)+1))
_= ax.set_xticklabels(names)
plt.setp(ax.get_xticklabels(), rotation=60, ha="right",rotation_mode="anchor")
for i,name in enumerate(names): # loop across boxes
jitterplot(i+1,data[i],ax)
ax.legend()