Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib boxplot x axis

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.

like image 561
FMarazzi Avatar asked Mar 08 '16 15:03

FMarazzi


People also ask

How do you change the X axis values in a boxplot MatPlotLib?

You need to specify the positions argument to the boxplot constructor. 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.

How do I plot a boxplot in MatPlotLib?

Creating Box Plotpyplot module of matplotlib library provides boxplot() function with the help of which we can create box plots. The data values given to the ax. boxplot() method can be a Numpy array or Python list or Tuple of arrays. Let us create the box plot by using numpy.

How do you change the Xticks in a boxplot?

Draw a boxplot using boxplot() method that returns the axis. Now, set the xticks using set_xticks() method, pass xticks. Set xticklabels and pass a list of labels and rotate them by passing rotation=45, using set_xticklabels() method. To display the figure, use show() method.

How do I make a horizontal box plot in Python?

Horizontal Box plots We can turn the boxplot into a horizontal boxplot by two methods first, we need to switch x and y attributes and pass it to the boxplot( ) method, and the other is to use the orient=”h” option and pass it to the boxplot() method.


1 Answers

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.

like image 50
Suever Avatar answered Oct 14 '22 17:10

Suever