如何在Python中并排放置2个带有子图的图表?

问题描述 投票:0回答:1

我想在同一网格上绘制每个类别的正面和负面单词图表。为了实现这一目标,我正在使用

plt.subplot()
。我正在使用 for 循环来绘制所有类别的图表。我的问题是:如何将两个类别的图表(同一网格上每个类别的正图表和负图表)放在 1 行中?

for category in results_df['Category'].unique():
    plt.figure(figsize=(6, 4))

    plt.subplot(1, 2, 1)
    subset_positive = results_df[(results_df['Category'] == category) & (results_df['Sentiment'] == 'Positive')]
    subset_positive = subset_positive.sort_values(by='Frequency', ascending=False)
    ax = sns.barplot(x='Frequency', y='Word', data=subset_positive, color=sns.color_palette('muted')[2])
    ax.set(ylabel = '', xlabel='')
    plt.title('Positive Words')

    plt.subplot(1, 2, 2)
    subset_negative = results_df[(results_df['Category'] == category) & (results_df['Sentiment'] == 'Negative')]
    subset_negative = subset_negative.sort_values(by='Frequency', ascending=False)
    ax = sns.barplot(x='Frequency', y='Word', data=subset_negative, color=sns.color_palette('muted')[0])
    ax.set(ylabel = '', xlabel='')
    plt.title('Negative Words')

    plt.subplots_adjust(wspace=0.5, top = 0.85)
    plt.suptitle(f'Most Frequent Words in Comments for {category}')
    plt.show()

我尝试了以下代码

categories = results_df['Category'].unique()

fig, axs = plt.subplots(7, 2, figsize=(20, 35))
axs = axs.flatten()

for i, category in enumerate(categories):
    subset_positive = results_df[(results_df['Category'] == category) & (results_df['Sentiment'] == 'Positive')]
    subset_positive = subset_positive.sort_values(by='Frequency', ascending=False)
    ax = axs[i * 2]
    sns.barplot(x='Frequency', y='Word', data=subset_positive, color=sns.color_palette('muted')[2], ax=ax)
    ax.set_title(f'Positive Words - {category}')

    subset_negative = results_df[(results_df['Category'] == category) & (results_df['Sentiment'] == 'Negative')]
    subset_negative = subset_negative.sort_values(by='Frequency', ascending=False)
    ax = axs[i * 2 + 1]
    sns.barplot(x='Frequency', y='Word', data=subset_negative, color=sns.color_palette('muted')[0], ax=ax)
    ax.set_title(f'Negative Words - {category}')

plt.tight_layout()
plt.show()

但这会在同一个图表上创建所有单独的图表。是否可以将图表分开但并排放置(2 个放在一排)?

python for-loop graph subplot
1个回答
0
投票

您可以使用

plt.subplots
创建子图网格。

import matplotlib.pyplot as plt
import seaborn as sns

categories = results_df['Category'].unique()
num_categories = len(categories)

# Create subplots with `num_categories` rows and 2 columns
fig, axs = plt.subplots(num_categories, 2, figsize=(20, num_categories * 5))

for i, category in enumerate(categories):
    # Positive words subplot
    subset_positive = results_df[(results_df['Category'] == category) & (results_df['Sentiment'] == 'Positive')]
    subset_positive = subset_positive.sort_values(by='Frequency', ascending=False)
    sns.barplot(x='Frequency', y='Word', data=subset_positive, color=sns.color_palette('muted')[2], ax=axs[i, 0])
    axs[i, 0].set_title(f'Positive Words - {category}')
    axs[i, 0].set_ylabel('')
    axs[i, 0].set_xlabel('')

    # Negative words subplot
    subset_negative = results_df[(results_df['Category'] == category) & (results_df['Sentiment'] == 'Negative')]
    subset_negative = subset_negative.sort_values(by='Frequency', ascending=False)
    sns.barplot(x='Frequency', y='Word', data=subset_negative, color=sns.color_palette('muted')[0], ax=axs[i, 1])
    axs[i, 1].set_title(f'Negative Words - {category}')
    axs[i, 1].set_ylabel('')
    axs[i, 1].set_xlabel('')

plt.tight_layout()
plt.subplots_adjust(top=0.95)
fig.suptitle('Most Frequent Words in Comments for Each Category', fontsize=16)
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.