gpt4 book ai didi

python - (如何)我可以将 seaborn despine 函数应用于单个子图?

转载 作者:行者123 更新时间:2023-12-04 14:19:30 26 4
gpt4 key购买 nike

我正在尝试绘制一系列仅在左侧图上具有可见 y 轴的盒须子图。我正在遍历 pandas 数据帧来执行此操作。但是,我用来删除坐标轴的 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

最佳答案

对于 future 偶然发现这个问题的答案寻求者,您可以使用 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)

关于python - (如何)我可以将 seaborn despine 函数应用于单个子图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56427466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com