我一直在尝试在sns.boxplot
上使用plt.subplot
,并在for循环中使用sns.stripplot
。然而,只显示了一个子图。你知道我该怎么解决这个问题吗?代码如下:
for Rab, pRab in zipped:
sns.set(font_scale = 2)
with sns.axes_style(style='ticks'):
fig, axes = plt.subplots(1,2, figsize =(18,6))
plt.tight_layout(pad=3.0)
sns.stripplot(
ax = axes[0],
data = unphospho_ratio,
y = Rab,
x = 'disease state',
color = 'black',
s = 8)
sns.boxplot(
ax = axes[0],
data = unphospho_ratio,
y = Rab,
x = 'disease state')
sns.despine(offset = 10, trim = True)
plt.show()
sns.stripplot(
ax = axes[1],
data = phospho_ratio,
y = pRab,
x = 'disease state',
color = 'black',
s = 8)
sns.boxplot(
ax = axes[1],
data = phospho_ratio,
y = pRab,
x = 'disease state')
sns.despine(offset = 10, trim = True)
plt.show()
这是它看起来是什么样的图像:
提前感谢您的帮助!
发布于 2021-07-30 20:39:09
您一直在相同的轴上绘图,即对于第一个箱线图和条形图,您使用的是axes[0]
。
假设数据是这样的:
unphospho_ratio = pd.DataFrame({'disease state':np.random.choice(['PD','Ctrl'],20),
'y1a':np.random.uniform(0,1,20),
'y2a':np.random.uniform(1,2,20)})
phospho_ratio = pd.DataFrame({'disease state':np.random.choice(['PD','Ctrl'],20),
'y1b':np.random.uniform(0,1,20),
'y2b':np.random.uniform(1,2,20)})
zipped = zip(['y1a','y2a'],['y1b','y2b'])
然后这将会起作用:
for Rab, pRab in zipped:
fig, axes = plt.subplots(1,2, figsize =(18,6))
plt.tight_layout(pad=3.0)
sns.stripplot(
ax = axes[0],
data = unphospho_ratio,
y = Rab,
x = 'disease state',
color = 'black',
s = 8)
sns.boxplot(
ax = axes[1],
data = unphospho_ratio,
y = Rab,
x = 'disease state')
sns.despine(offset = 10, trim = True)
plt.show()
sns.stripplot(
ax = axes[0],
data = phospho_ratio,
y = pRab,
x = 'disease state',
color = 'black',
s = 8)
sns.boxplot(
ax = axes[1],
data = phospho_ratio,
y = pRab,
x = 'disease state')
sns.despine(offset = 10, trim = True)
plt.show()
如果你想要叠加点和箱形图(从你的问题中看不清楚),你不想看绘图设备plt
,它应该在调用所有函数后是fig
:
for Rab, pRab in zipped:
fig, axes = plt.subplots(1,2, figsize =(18,6))
plt.tight_layout(pad=3.0)
sns.stripplot(
ax = axes[0],
data = unphospho_ratio,
y = Rab,
x = 'disease state',
color = 'black',
s = 8)
sns.boxplot(
ax = axes[0],
data = unphospho_ratio,
y = Rab,
x = 'disease state')
sns.despine(offset = 10, trim = True)
sns.stripplot(
ax = axes[1],
data = phospho_ratio,
y = pRab,
x = 'disease state',
color = 'black',
s = 8)
sns.boxplot(
ax = axes[1],
data = phospho_ratio,
y = pRab,
x = 'disease state')
sns.despine(offset = 10, trim = True)
fig
https://stackoverflow.com/questions/68590233
复制相似问题