Sets up space between drawers in Python Graphs by creating nested field diagrams using Seaborn?

I am trying to set the space between the boxes (between the green and orange fields) created using the Python Seaborn module sns.boxplot() . Please see the attached graph that the green and orange subtitles are stuck to each other, which makes it visually not the most attractive.

It is impossible to find a way to do this, can anyone find a way (the code is attached)?

Seaborn boxplots

 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 boxtot Seaborn Seaborn is here: http://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.boxplot.html

+6
source share
1 answer

Having run the danger that it is no longer needed, I found a solution to this problem. When drawing boxplots directly with matplotlib, the location of the boxes can be controlled using the width and position keywords. However, passing the positions keyword to sns.factorplot(kind='box',...) you get

 TypeError: boxplot() got multiple values for keyword argument 'positions' 

To get around this, you can set the field width "manually" after creating a boxplot. This is a bit tedious because the boxes are stored as PatchPatches in separate instances of the Axes FacedGrid that are returned by sns.factorplot . Instead of the simple syntax (x,y,width,height) that Rects has, PathPatches use vertices to define angles, which requires a bit more computation when you need to adjust the margins. Above all else, the PathPatches returned by matplotlib.boxplot contains an additional (ignored) vertex for the Path.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.

Below I define a function that adjusts the width of the fields created by the OP example code (note the additional import):

 from matplotlib.patches import PathPatch def adjust_box_widths(g, fac): """ Adjust the withs of a seaborn-generated boxplot. """ ##iterating through Axes instances for ax in g.axes.flatten(): ##iterating through axes artists: for c in ax.get_children(): ##searching for PathPatches if isinstance(c, PathPatch): ##getting current width of box: p = c.get_path() verts = p.vertices verts_sub = verts[:-1] xmin = np.min(verts_sub[:,0]) xmax = np.max(verts_sub[:,0]) xmid = 0.5*(xmin+xmax) xhalf = 0.5*(xmax - xmin) ##setting new width of box xmin_new = xmid-fac*xhalf xmax_new = xmid+fac*xhalf verts_sub[verts_sub[:,0] == xmin,0] = xmin_new verts_sub[verts_sub[:,0] == xmax,0] = xmax_new ##setting new width of median line for l in ax.lines: if np.all(l.get_xdata() == [xmin,xmax]): l.set_xdata([xmin_new,xmax_new]) 

calling this function with

 adjust_box_widths(g, 0.9) 

gives the following result:

seaborn boxplot with set field width

+1
source

All Articles