(如何)我可以将Seapine Depine功能应用于各个子图?

时间:2019-06-03 12:32:45

标签: python python-3.x matplotlib seaborn

我正在尝试在左侧图上绘制一系列带有可见y轴的箱须图。我正在遍历熊猫数据框来做到这一点。但是,我用来删除轴的despine函数似乎在使用任何时候都适用于所有绘图。在这种情况下,最终子图应该没有y轴,但是该轴也从左侧图中删除了。有什么办法可以解决这个问题?

是否可以将每个子图与应用于其他子图的despine函数隔离?这似乎只发生在despine功能上。当前代码在下面,但是我还尝试在循环之前(使用[fig,axes = plt.subplots(ncols = 3,nrows = 4)])和循环内部(ax = plt.subplot(4, 3,q + 1),然后尝试在seaborn绘图函数中调用ax = ax。

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

sns.set(rc={"figure.figsize":(10,20)}, font_scale=1) # set size of plots and font
sns.set_style("ticks", {"font.sans-serif": "Arial"}) # set style for plots
sns.set_palette("cubehelix", n_colors=len(participants)) # set colour palette for individual datapoints

plt.figure
for q in range(11):
    plt.subplot(4,3,q+1)
    qplot = sns.swarmplot(data=eval("s" + str(q+1)), x="variable", y="value", hue="Participant", size=3) # plot individual participant data as points
    qplot.legend().set_visible(False) # hide the legend for individual participant data
    qplot = sns.boxplot(data=eval("s" + str(q+1)), x="variable", y="value", palette="Greys", linewidth=2, fliersize=0, width=0.85) # plot the boxplot
    qplot.set(ylim=(-3.5,3.5), xlabel="Condition", ylabel="Response") # set y axis values and label axes
    plt.title("S" + str(q+1)) # add a title
    if (q == 0) or (q == 3) or (q == 6):
        qplot.set(xticklabels=[], xlabel=None, xticks = []) # remove ticks and labels
        sns.despine(bottom = True, top=True, right=True, left=False, trim=True) # remove spines 
    if (q == 1) or (q == 2) or (q == 4) or (q == 5) or (q == 7):
        qplot.set(xticklabels=[], xlabel=None, xticks = [], yticklabels=[], ylabel = None, yticks = []) # remove ticks and labels
        sns.despine(bottom = True, top=True, right=True, left=True, trim=True) # remove spines
    if (q == 9):
        sns.despine(top=True, right = True,trim=True) # remove spines
    if (q == 8) or (q == 10):
        qplot.set(yticks = [], yticklabels=[], ylabel = None) # remove ticks and labels
        sns.despine(bottom=True, top=True, left=True, right=True, trim=True) # remove spines
    for axis in ["top","bottom","left","right"]: 
        qplot.spines[axis].set_linewidth(2) # set linewidth of axes
        qplot.tick_params(axis = "x", width=0) # set linewidth of x ticks to zero
        qplot.tick_params(axis = "y", width=2) # set linewidth of y ticks

Figure output

1 个答案:

答案 0 :(得分:0)

对于将来会迷惑于此问题的寻求答案的人,您可以使用sns.despine()进行如下操作以在轴的网格中显示/隐藏刺:

fig, axs = plt.subplots(4,3)
for ax in axs.flat:
    if ax.is_first_col():
        if ax.is_last_row():
            sns.despine(bottom=False, left=False, ax=ax)
        else:
            sns.despine(bottom=True, left=False, ax=ax)
    elif ax.is_last_row():
        sns.despine(bottom=False, left=True, ax=ax)
    else:
        sns.despine(bottom=True, left=True, ax=ax)

enter image description here

@PaulH足以重构上面的代码。他的版本更紧凑,更易于阅读:

fig, axs = plt.subplots(4,3)
for ax in axs.flat:
    sns.despine(bottom=not ax.is_last_row(), left=not ax.is_first_col(), ax=ax)