I am trying to set a space between the boxplots (between the green and orange boxes) created with Python Seaborn
module's sns.boxplot()
. Please see attached the graph, that the green and orange subplot boxes are stuck to each other, making it visually not the most appealing.
Can't find a way to do that, anyone could find a way (code attached)?
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset("tips")
sns.set(style="ticks", palette='Set2', font='Roboto Condensed')
sns.set_context("paper", font_scale=1.1, rc={"lines.linewidth": 1.1})
g=sns.factorplot(x="time", y="total_bill", hue="smoker",
col="day", data=tips, kind="box", size=4, aspect=0.5,
width=0.8,fliersize=2.5,linewidth=1.1, notch=False,orient="v")
sns.despine(trim=True)
g.savefig('test6.png', format='png', dpi=600)
The Seaborn
boxplot documentation is here: http://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.boxplot.html
Running the danger that this is not needed anymore, I found a solution to this problem. When drawing
boxplots
directly with matplotlib, the arrangement of the boxes can be controlled with thewidth
andposition
keywords. However, when passing thepositions
keyword tosns.factorplot(kind='box',...)
, one gets aTo get around this, one can set the widths of the boxes 'manually' after the boxplot has been created. This is a bit tedious, because the boxes are stored as
PatchPatches
within the individualAxes
instances of theFacedGrid
that is returned bysns.factorplot
. Instead of the simple(x,y,width,height)
syntax thatRects
have,PathPatches
use vertices to define the corners, which involves slightly more computation when one wants to adjust the boxes. On top of everything else, thePathPatches
returned bymatplotlib.boxplot
contain an extra (ignored) vertex for thePath.CLOSEPOLY
code, which is set to(0,0)
and is best ignored. In addition to the box, the horizontal line that marks the median is now too wide and needs to be adjusted as well.Below I define a function that adjusts widths of the boxes generated by the OP's example code(note the extra import):
calling this function with
gives the following output: