【问题标题】:How to put Seaborn graphs into subplots? [duplicate]如何将 Seaborn 图放入子图中? [复制]
【发布时间】:2020-05-25 14:44:21
【问题描述】:

我想使用以下代码将海生图放入子图中。有人可以提出正确的方法吗?

plt.figure(figsize=(15,5)) 
plt.subplot(1,2,1)
sns.lmplot(data= df, x='Attack', y= 'Defense') 
plt.subplot(1,2,2) 
sns.jointplot(data= df, x='Attack', y= 'Defense', kind='scatter',color='purple') 
plt.show()

【问题讨论】:

  • 您需要ax1 = plt.subplot(1,2,1)sns.lmplot(...., ax=ax1)。与 ax2 相同。

标签: python matplotlib seaborn data-science


【解决方案1】:

目前你不能以简单的方式做到这一点。 seaborn 中的一些图在 figure-level 中定义(例如 lmplot、factorplot、jointplot 等),它们不接受轴参数。

如果你真的想创建子图,你可以按照这个答案here,其中包括一个支持类 (SeabornFig2Grid) 来完全满足你的需要。下面是该类的使用示例:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import seaborn as sns; sns.set()


df = pd.DataFrame(dict(
    Attack = np.random.random(20),
    Defense = np.random.random(20)))

g0 = sns.lmplot(data= df, x='Attack', y= 'Defense') 
g1 = sns.jointplot(data= df, x='Attack', y= 'Defense', kind='scatter',color='purple') 
fig = plt.figure(figsize=(8,4))
gs = gridspec.GridSpec(1, 2)

mg0 = SeabornFig2Grid(g0, fig, gs[0])
mg1 = SeabornFig2Grid(g1, fig, gs[1])

gs.tight_layout(fig)

【讨论】:

    猜你喜欢
    • 2019-04-07
    • 2011-02-01
    • 2015-09-23
    • 2013-03-25
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    相关资源
    最近更新 更多