{
  "income_quintile": [
    {
      "quintile": "Lowest 20%",
      "housing": 40,
      "food": 16,
      "transportation": 15,
      "healthcare": 8,
      "entertainment": 5,
      "other": 16
    },
    {
      "quintile": "Second 20%",
      "housing": 35,
      "food": 16,
      "transportation": 18,
      "healthcare": 9,
      "entertainment": 6,
      "other": 16
    },
    {
      "quintile": "Middle 20%",
      "housing": 30,
      "food": 14,
      "transportation": 20,
      "healthcare": 10,
      "entertainment": 7,
      "other": 19
    },
    {
      "quintile": "Fourth 20%",
      "housing": 28,
      "food": 12,
      "transportation": 22,
      "healthcare": 11,
      "entertainment": 8,
      "other": 19
    },
    {
      "quintile": "Highest 20%",
      "housing": 25,
      "food": 10,
      "transportation": 20,
      "healthcare": 12,
      "entertainment": 10,
      "other": 23
    }
  ]
}
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
from io import BytesIO
from PIL import Image

def generate_plot(data):
    # Create DataFrame from data
    quintiles = data['income_quintile']
    df = pd.DataFrame(quintiles)
    
    # Set the seaborn theme
    sns.set_theme(style="whitegrid")
    
    # Plot the data
    fig, ax = plt.subplots(figsize=(10, 6))
    colors = sns.color_palette("husl", 6)
    
    df.set_index('quintile').T.plot(kind='bar', stacked=True, color=colors, ax=ax)
    
    # Customize the plot
    ax.set_title('Consumer Spending Patterns by Income Quintile', fontsize=16, fontweight='bold')
    ax.set_ylabel('Percentage (%)', fontsize=12)
    ax.set_xlabel('Income Quintile', fontsize=12)
    ax.legend(title='Category', bbox_to_anchor=(1.05, 1), loc='upper left')
    
    plt.xticks(rotation=45)
    plt.tight_layout()
    
    # Save the plot to a BytesIO object
    buf = BytesIO()
    plt.savefig(buf, format='png')
    buf.seek(0)
    
    # Convert the BytesIO object to a PIL Image and return it
    image = Image.open(buf)
    
    return image
The figure, titled "Consumer Spending Patterns by Income Quintile," presents a stacked bar chart comparing the spending percentages of different income quintiles on various household categories. The chart categorizes consumer spending into six segments: housing, food, transportation, healthcare, entertainment, and other.

Each bar in the chart represents a different income quintile, labeled as "Lowest 20%", "Second 20%", "Middle 20%", "Fourth 20%", and "Highest 20%". The categories are color-coded, making it easy to differentiate between them. 

The "Lowest 20%" quintile, indicated by the leftmost bar, has the highest percentage expenditure on housing at 40%, followed by food and other expenses each at 16%. Transportation makes up 15% while healthcare comprises 8%, and entertainment is the smallest segment at 5%.

Moving to the "Second 20%" quintile, housing expenditure decreases to 35%. Food remains constant at 16%, while transportation expenditure increases to 18%. Healthcare and other expenses also increase to 9% each, and entertainment rises to 6%.

In the "Middle 20%" quintile, the trends continue with housing dropping further to 30%. Food expenses reduce to 14%, but transportation rises to 20%. Healthcare makes up 10%, entertainment is 7%, and "other" expenses increase to 19%.

For the "Fourth 20%" quintile, housing expenditure is 28%, and food drops further to 12%. Transportation is the highest in this group at 22%. Healthcare takes up 11%, entertainment is 8%, and "other" expenses remain at 19%.

Finally, the "Highest 20%" quintile shows the least proportion of spending on housing at 25% and food at 10%. Transportation remains significant at 20%. Healthcare is at its highest amongst other categories at 12%, entertainment increases to 10%, and "other" expenses peak at 23%.

The figure illustrates a clear trend: as income increases, the percentage of expenditure on housing and food generally decreases. In contrast, spending on healthcare, entertainment, and other categories tends to increase with higher income quintiles. Transportation costs are relatively more volatile but largely remain significant across all income groups. This visually underscores the changing priorities and spending capabilities across different income levels.
[
    {
        "question": "Which income quintile spends the highest percentage on housing?",
        "answer": "Lowest 20%",
        "explanation": "From the figure, the 'Lowest 20%' income quintile spends 40% on housing, the highest among all quintiles."
    },
    {
        "question": "What percentage of spending does the 'Highest 20%' income quintile allocate to food?",
        "answer": "10%",
        "explanation": "In the figure, the bar corresponding to the 'Highest 20%' income quintile shows 10% is allocated to food."
    },
    {
        "question": "How does spending on transportation change as income quintile increases?",
        "answer": "It generally increases until the fourth quintile, then slightly decreases.",
        "explanation": "The figure shows transportation spending is 15% for 'Lowest 20%', 18% for 'Second 20%', 20% for 'Middle 20%', 22% for 'Fourth 20%', and 20% for 'Highest 20%'."
    },
    {
        "question": "Which spending category has the least difference between the 'Lowest 20%' and 'Highest 20%' quintiles?",
        "answer": "Food",
        "explanation": "In the figure, food spending is 16% for the 'Lowest 20%' and 10% for the 'Highest 20%', a difference of 6%. Other categories have larger differences."
    },
    {
        "question": "Which income quintile spends the most on entertainment?",
        "answer": "Highest 20%",
        "explanation": "The 'Highest 20%' income quintile spends 10% on entertainment, which is the highest percentage among all quintiles in the figure."
    },
    {
        "question": "Do higher income quintiles spend a higher or lower percentage on healthcare compared to lower income quintiles?",
        "answer": "Higher",
        "explanation": "The figure shows rising percentages for healthcare spending as income quintile increases: 8% for 'Lowest 20%' to 12% for 'Highest 20%'."
    },
    {
        "question": "What is the total percentage of spending on housing and transportation for the 'Middle 20%' quintile?",
        "answer": "50%",
        "explanation": "From the figure, housing is 30% and transportation is 20% for the 'Middle 20%', totaling 30 + 20 = 50%."
    },
    {
        "question": "Which income quintile allocates the highest percentage to 'other' categories?",
        "answer": "Highest 20%",
        "explanation": "The figure shows the 'Highest 20%' quintile allocates 23% to 'other' categories, more than any other quintile."
    },
    {
        "question": "What is the approximate difference in transportation spending between the 'Second 20%' and 'Lowest 20%' quintiles?",
        "answer": "3%",
        "explanation": "The figure shows transportation spending is 15% for 'Lowest 20%' and 18% for 'Second 20%', a difference of 18 - 15 = 3%."
    },
    {
        "question": "In terms of healthcare spending, by how much does the 'Fourth 20%' quintile exceed the 'Second 20%' quintile?",
        "answer": "2%",
        "explanation": "Healthcare spending is 11% for 'Fourth 20%' and 9% for 'Second 20%', a difference of 11 - 9 = 2%."
    }
]