小编Haf*_*udi的帖子

使用 matplotlib 和 seaborn 的两列子图

我需要添加两列和多行的子图。行不会固定,但对于一列,我想从一个数据集创建seaborn线图,对于第二列,我想为另一个数据集创建seaborn线图。

我已经尝试过以下但不起作用。

tips = sns.load_dataset("tips")
dataset2=tips


days = list(tips.drop_duplicates('day')['day'])
ggpec = gridspec.GridSpec(len(days ), 2)
axs = []
for i,j in zip(days,range(1,len(days)+1)):
    fig = plt.figure(figsize=(20,4),dpi=200)
    palette = sns.color_palette("magma", 2)
    chart = sns.lineplot(x="time", y="total_bill",
                      hue="sex",style='sex',
                      palette=palette, data=tips[tips['day']==i])
    chart.set_xticklabels(
        chart.get_xticklabels(), 
        rotation=90, 
        minor=True,
        verticalalignment=True,
        horizontalalignment='right',
        fontweight='light',
        fontsize='large'
    )

    plt.title("Title 1",fontsize=18, fontweight='bold')

    fig2 = plt.figure(figsize=(20,5),dpi=200)
    palette = sns.color_palette("magma", 2)
    chart = sns.lineplot(x="time", y="total_bill",
                      hue="sex",style='sex',
                      palette=palette, data=dataset2[dataset2['day']==i])
    chart.set_xticklabels(
        chart.get_xticklabels(), 
        rotation=90, 
        minor=True,
        verticalalignment=True,
        horizontalalignment='right',
        fontweight='light',
        fontsize='large'
    )
    plt.title("Title 2",fontsize=18, fontweight='bold')
plt.show()
Run Code Online (Sandbox Code Playgroud)

python subplot

0
推荐指数
1
解决办法
6694
查看次数

标签 统计

python ×1

subplot ×1