This code creates a 2x2 grid of line charts. Each chart shows sales over months for one product. This helps compare sales trends side by side.
import matplotlib.pyplot as plt
import pandas as pd
# Sample data: sales for 4 products over 4 months
sales_data = pd.DataFrame({
'Month': ['Jan', 'Jan', 'Jan', 'Jan', 'Feb', 'Feb', 'Feb', 'Feb', 'Mar', 'Mar', 'Mar', 'Mar', 'Apr', 'Apr', 'Apr', 'Apr'],
'Product': ['A', 'B', 'C', 'D'] * 4,
'Sales': [10, 15, 7, 12, 12, 18, 9, 14, 14, 20, 11, 16, 13, 22, 12, 18]
})
# Prepare figure with 2 rows and 2 columns
fig, axes = plt.subplots(2, 2, figsize=(10, 6))
# Get unique products
products = sales_data['Product'].unique()
# Plot sales for each product in its own subplot
for ax, product in zip(axes.flat, products):
data = sales_data[sales_data['Product'] == product]
ax.plot(data['Month'], data['Sales'], marker='o')
ax.set_title(f'Product {product}')
ax.set_xlabel('Month')
ax.set_ylabel('Sales')
plt.tight_layout()
plt.show()