如何在 Python 中使用 For 循环在子图中制作组合图表

How to make combo charts inside subplots using a For loop in Python

我正在尝试在 Python 中使用 For 循环以编程方式在一系列 subplot 中创建 lineplotbarbplot 的组合。

这是我迄今为止尝试过的代码的可重现版本:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df_q = pd.DataFrame.from_dict ({'Year': [2020,2021,2022,2020,2021,2022,2020,2021,2022], 
                                'Category': ['A','A','A','B','B','B','C','C','C'], 
                                'Requested': [5,8,3,15,6,9,11,14,9],
                                'Allowed':   [5,6,2,10,6,7,5,11,9]})
                                
df_a = pd.DataFrame.from_dict ({'Year': [2020,2021,2022,2020,2021,2022,2020,2021,2022], 
                                'Category': ['A','A','A','B','B','B','C','C','C'], 
                                'Amount': [4,10,3,12,6,11,7,5,12]})
    
df_qt = df_q.melt(id_vars=['Year', 'Category'], 
                   value_vars=['Requested', 'Allowed' ], ignore_index=True)   
 
catgs = df_a['Category'].unique()
    
fig, ax = plt.subplots(nrows=len(catgs), figsize=(20,len(catgs)*6))
  
for i, cat in enumerate(catgs):
    filt_q = df_qt.loc [df_qt['Category'] == cat]
    filt_a= df_a.loc [df_a['Category'] == cat]
      
    sns.barplot(data = filt_q, x='Year', y='value', hue= 'variable', alpha=0.7, ax=ax[i])
    
    sns.lineplot(data =filt_a['Amount'], linewidth = 1.5, markers=True,
                 marker="o",markersize=10, color='darkred', label='Amount', ax=ax[i])
    
    ax[i].set_title('Category: {}'.format(cat), size=25)

但是,从第二个子图开始,lineplotbarplot 之间似乎存在延迟。请参阅下面的屏幕截图。

知道我做错了什么吗?

试试这个:

filt_a= df_a.loc[df_a['Category'] == cat].reset_index()

您在类别 B 和 C 中看到漂移的原因是由于条形图的 x-coordinate:

# The x-coordinates are not 2020, 2021, 2022.
# They are 0, 1, 2. The *x-labels* are 2020, 2021, 2022
sns.barplot(data=filt_q, x='Year', y='value', ...)

# The x-coordinates are the index of the filt_a dataframe
# Calling reset_index resets it to 0, 1, 2, ...
sns.lineplot(data=filt_a['Amount'], ...)