It's easier to ask this with a figure. At the moment i obtain the following boxplot graph using matplotlib:
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.
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.
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.
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.
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.
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])
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With