繁体   English   中英

使用 matplotlib 和 seaborn 的两列子图

[英]Two column subplot using matplotlib and seaborn

我需要添加具有两列和多行的子图。 The rows will not be fixed but for one column I want to create seaborn line plot from one data set and for second column i want to create seaborn line plot for another data set.

我尝试了以下但不工作。

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()

要创建具有 2 列和多行的多个图,您可以使用 subplot. 您在哪里定义当前要激活的行数、列数和子图。

import matplotlib.pyplot as plt
plt.subplot(3, 2, 1)   # Define 3 rows, 2 column, Activate subplot 1. 
plt.plot([1, 2, 3, 4, 5, 6, 7], [7, 8, 6, 5, 2, 2, 4], 'b*-', label='Plot 1')

plt.subplot(3, 2, 2)   # 3 rows, 2 column, Activate subplot 2.
# plot some data here
plt.plot([1, 2, 3, 4, 5, 6, 7], [7, 8, 6, 5, 2, 2, 4], 'b*-', label='Plot 2')

plt.subplot(3, 2, 3)   # 3 rows, 2 column, Activate subplot 3.
# plot some data here
plt.plot([1, 2, 3, 4, 5, 6, 7], [7, 8, 6, 5, 2, 2, 4], 'b*-', label='Plot 3')

# to Prevent subplots overlap
plt.tight_layout()  
plt.show()

您也可以在此概念的基础上绘制 seaborn 图。

f, axes = plt.subplots(3,2) # Divide the plot into 3 rows, 2 columns
# Draw the plot in first row second column
sns.lineplot(xData, yData, data=dataSource, ax=axes[0][1]) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM