python - 在 for 循环中对 sns.boxplot 和 sns.scatterplot 使用 plt.subplot
问题描述
我一直在尝试使用plt.subplot
onsns.boxplot
和sns.stripplot
for 循环。然而,只显示了一个子图。你知道我该如何解决这个问题吗?这是代码:
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()
这是它的样子:
我在这里先向您的帮助表示感谢!
解决方案
您一直在相同的轴上绘图,即第一个箱线图和条形图,您正在使用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
推荐阅读
- javascript - 多个文件检查扩展名是 Pdf
- histogram - ROOT:将根文件与 hadd 和权重相加
- java - 尝试在 Android Studio 中执行单元测试时出现“JUnit 版本 3.8 或更高版本”错误
- node.js - 如何模拟导入?
- c# - 确认电子邮件时 ASP.NET Core 令牌无效,但令牌似乎有效
- r - 将字符向量转换为不带引号的对象名称
- json - java.lang.NoSuchMethodError:com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder;
- java - 对齐按钮中的 SWT 问题
- php - 如何在 laravel 中从数据库中传递值
- java - 带有 ModelMapper 的 LazyInitializationException (Spring + Hibernate)