Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reducing the distance between two boxplots

Tags:

matplotlib

I'm drawing the bloxplot shown below using python and matplotlib. Is there any way I can reduce the distance between the two boxplots on the X axis?

enter image description here

This is the code that I'm using to get the figure above:

import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['ytick.direction'] = 'out'
rcParams['xtick.direction'] = 'out'

fig = plt.figure()
xlabels = ["CG", "EG"]
ax = fig.add_subplot(111)
ax.boxplot([values_cg, values_eg])
ax.set_xticks(np.arange(len(xlabels))+1)
ax.set_xticklabels(xlabels, rotation=45, ha='right')
fig.subplots_adjust(bottom=0.3)
ylabels = yticks = np.linspace(0, 20, 5)
ax.set_yticks(yticks)
ax.set_yticklabels(ylabels)

ax.tick_params(axis='x', pad=10)
ax.tick_params(axis='y', pad=10)

plt.savefig(os.path.join(output_dir, "output.pdf"))

And this is an example closer to what I'd like to get visually (although I wouldn't mind if the boxplots were even a bit closer to each other):

enter image description here

like image 555
Filipe Correia Avatar asked Apr 30 '13 14:04

Filipe Correia


People also ask

How do you change the size of a Boxplot?

Steps. Set the figure size and adjust the padding between and around the subplots. Make a Pandas dataframe, i.e., two-dimensional, size-mutable, potentially heterogeneous tabular data. Make a box and whisker plot, using boxplot() method with width tuple to adjust the box in boxplot.

How do you know if a Boxplot is skewed?

When the median is in the middle of the box, and the whiskers are about the same on both sides of the box, then the distribution is symmetric. When the median is closer to the bottom of the box, and if the whisker is shorter on the lower end of the box, then the distribution is positively skewed (skewed right).

Which of the following parameter is used to change the position of Boxplots?

conf_intervals : This parameter is also an array or sequence whose first dimension is compatible with x and whose second dimension is 2. positions : This parameter is used to sets the positions of the boxes.


2 Answers

You can either change the aspect ratio of plot or use the widths kwarg (doc) as such:

ax.boxplot([values_cg, values_eg], widths=1)

to make the boxes wider.

like image 110
tacaswell Avatar answered Oct 24 '22 18:10

tacaswell


Try changing the aspect ratio using

ax.set_aspect(1.5) # or some other float

The larger then number, the narrower (and taller) the plot should be:

a circle will be stretched such that the height is num times the width. aspect=1 is the same as aspect=’equal’.

http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_aspect

like image 41
askewchan Avatar answered Oct 24 '22 20:10

askewchan