|
| 1 | +""" |
| 2 | +area-basic: Basic Area Chart |
| 3 | +Implementation for: seaborn |
| 4 | +Variant: default |
| 5 | +Python: 3.10+ |
| 6 | +
|
| 7 | +Note: Seaborn does not have a native area chart function. This implementation |
| 8 | +uses matplotlib's fill_between with seaborn's styling for a consistent look. |
| 9 | +""" |
| 10 | + |
| 11 | +from typing import TYPE_CHECKING, Optional |
| 12 | + |
| 13 | +import matplotlib.pyplot as plt |
| 14 | +import pandas as pd |
| 15 | +import seaborn as sns |
| 16 | + |
| 17 | + |
| 18 | +if TYPE_CHECKING: |
| 19 | + from matplotlib.figure import Figure |
| 20 | + |
| 21 | + |
| 22 | +def create_plot( |
| 23 | + data: pd.DataFrame, |
| 24 | + x: str, |
| 25 | + y: str, |
| 26 | + color: str = "steelblue", |
| 27 | + alpha: float = 0.4, |
| 28 | + line_alpha: float = 1.0, |
| 29 | + title: Optional[str] = None, |
| 30 | + xlabel: Optional[str] = None, |
| 31 | + ylabel: Optional[str] = None, |
| 32 | + figsize: tuple[float, float] = (16, 9), |
| 33 | + **kwargs, |
| 34 | +) -> "Figure": |
| 35 | + """ |
| 36 | + Create a basic filled area chart using seaborn styling with matplotlib. |
| 37 | +
|
| 38 | + Args: |
| 39 | + data: Input DataFrame with required columns |
| 40 | + x: Column name for x-axis values (sequential: datetime, numeric, or categorical) |
| 41 | + y: Column name for y-axis values (numeric) |
| 42 | + color: Fill and line color (default: "steelblue") |
| 43 | + alpha: Transparency level for area fill 0.0-1.0 (default: 0.4) |
| 44 | + line_alpha: Transparency level for edge line 0.0-1.0 (default: 1.0) |
| 45 | + title: Plot title (default: None) |
| 46 | + xlabel: Custom x-axis label (default: column name) |
| 47 | + ylabel: Custom y-axis label (default: column name) |
| 48 | + figsize: Figure size as (width, height) (default: (16, 9)) |
| 49 | + **kwargs: Additional parameters passed to fill_between() |
| 50 | +
|
| 51 | + Returns: |
| 52 | + Matplotlib Figure object |
| 53 | +
|
| 54 | + Raises: |
| 55 | + ValueError: If data is empty |
| 56 | + KeyError: If required columns not found |
| 57 | +
|
| 58 | + Example: |
| 59 | + >>> data = pd.DataFrame({'Month': range(1, 13), 'Revenue': [10, 15, 13, 17, 20, 25, 22, 26, 24, 28, 30, 35]}) |
| 60 | + >>> fig = create_plot(data, x='Month', y='Revenue') |
| 61 | + """ |
| 62 | + # Input validation |
| 63 | + if data.empty: |
| 64 | + raise ValueError("Data cannot be empty") |
| 65 | + |
| 66 | + # Check required columns |
| 67 | + for col in [x, y]: |
| 68 | + if col not in data.columns: |
| 69 | + available = ", ".join(data.columns) |
| 70 | + raise KeyError(f"Column '{col}' not found. Available: {available}") |
| 71 | + |
| 72 | + # Set seaborn style for consistent appearance |
| 73 | + sns.set_theme(style="whitegrid") |
| 74 | + |
| 75 | + # Create figure |
| 76 | + fig, ax = plt.subplots(figsize=figsize) |
| 77 | + |
| 78 | + # Get x and y values |
| 79 | + x_vals = data[x] |
| 80 | + y_vals = data[y] |
| 81 | + |
| 82 | + # Plot the edge line first |
| 83 | + ax.plot(x_vals, y_vals, color=color, alpha=line_alpha, linewidth=2) |
| 84 | + |
| 85 | + # Fill the area between the line and baseline (y=0) |
| 86 | + ax.fill_between(x_vals, y_vals, alpha=alpha, color=color, **kwargs) |
| 87 | + |
| 88 | + # Apply styling |
| 89 | + ax.set_xlabel(xlabel or x, fontsize=12) |
| 90 | + ax.set_ylabel(ylabel or y, fontsize=12) |
| 91 | + ax.grid(True, alpha=0.3, linestyle="--") |
| 92 | + |
| 93 | + # Set y-axis to start from 0 for area charts (standard practice) |
| 94 | + y_min = min(0, y_vals.min()) |
| 95 | + y_max = y_vals.max() |
| 96 | + y_padding = (y_max - y_min) * 0.05 |
| 97 | + ax.set_ylim(y_min, y_max + y_padding) |
| 98 | + |
| 99 | + # Title |
| 100 | + if title: |
| 101 | + ax.set_title(title, fontsize=14, fontweight="bold") |
| 102 | + |
| 103 | + # Tight layout to avoid label clipping |
| 104 | + plt.tight_layout() |
| 105 | + |
| 106 | + return fig |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + # Sample data for testing - monthly revenue over a year |
| 111 | + data = pd.DataFrame( |
| 112 | + { |
| 113 | + "Month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], |
| 114 | + "Revenue": [12000, 15000, 13500, 17000, 20000, 25000, 22000, 26000, 24000, 28000, 30000, 35000], |
| 115 | + } |
| 116 | + ) |
| 117 | + |
| 118 | + # Create plot |
| 119 | + fig = create_plot(data, x="Month", y="Revenue", title="Monthly Revenue Growth", ylabel="Revenue ($)") |
| 120 | + |
| 121 | + # Save for inspection |
| 122 | + plt.savefig("plot.png", dpi=300, bbox_inches="tight") |
| 123 | + print("Plot saved to plot.png") |
0 commit comments