def plot_alphadf(alphasdf, col_order, labeldict, metric='alpha'):
"""
Plot faceted alpha diversity.
Parameters
----------
alphasdf : pandas DataFrame
columns ['study', 'alpha', 'DiseaseState']
col_order : list
dataset IDs in the order they should be plotted
labeldict : dict
dictionary with {dataset: label}
mteric : str
alpha diversity metric, to use in labeling y axis
Returns
-------
fig : Figure
"""
sns.set_style('white')
g = sns.FacetGrid(alphasdf, col='study', col_wrap=6,
col_order=col_order, sharex=False, sharey=False)
g = g.map(sns.boxplot, "DiseaseState", "alpha")
g = g.map(sns.stripplot, "DiseaseState", "alpha", split=True, jitter=True,
size=5, linewidth=0.6)
fig = plt.gcf()
fig.set_size_inches(14.2, 9)
# Fix y-axis gridlines
axs = g.axes
for i in range(len(axs)):
ax = axs[i]
yticks = ax.get_yticks()
# If bottom limit is between 0 and 1 (i.e. not simpson)
if not (yticks[0] < 1 and yticks[0] > 0):
ax.set_ylim(floor(yticks[0]), floor(yticks[-1]))
if yticks[0] < 0:
ax.set_ylim(0, floor(yticks[-1]))
yticks = ax.get_yticks()
if (yticks[0] < 1 and yticks[0] > 0):
ax.set_yticks(yticks[1::2])
else:
ax.set_yticks(yticks[::2])
# Need some space on the y-axis for p-values
ax.set_ylim(ax.get_ylim()[0], 1.2*ax.get_ylim()[1])
# Update title
oldtitle = ax.get_title()
newtitle = labeldict[oldtitle.split('=')[1].strip()]
ax.set_title(newtitle)
# Update y label
if i % 6 == 0:
ax.set_ylabel(metric)
plt.tight_layout()
return fig
评论列表
文章目录