25

It's easier to ask this with a figure. At the moment i obtain the following boxplot graph using matplotlib: enter image description here

Is there a way to obtain a figure like that, but with each box in a position coherent with the corresponding x-axis number (like in a normal scatter plot, but with boxes instead of points)?

At the moment the numbers on the x-axis are added by means of the labels= argument.

user
  • 5,370
  • 8
  • 47
  • 75
FMarazzi
  • 583
  • 1
  • 5
  • 14

1 Answers1

38

You need to specify the positions argument to the boxplot constructor.

from matplotlib import pyplot as plt

plt.boxplot([[1,4],[2,5],[3,6]], positions=[2,4,5.5])

enter image description here

By default it uses the values [1, 2, ..., n] but you can specify a different x position for each bar and the xticks will be updated automatically.

Daniel
  • 11,332
  • 9
  • 44
  • 72
Suever
  • 64,497
  • 14
  • 82
  • 101