|
| 1 | +""" pyplots.ai |
| 2 | +pie-drilldown: Drilldown Pie Chart with Click Navigation |
| 3 | +Library: seaborn 0.13.2 | Python 3.13.11 |
| 4 | +Quality: 82/100 | Created: 2025-12-31 |
| 5 | +""" |
| 6 | + |
| 7 | +import matplotlib.pyplot as plt |
| 8 | +import numpy as np |
| 9 | +import seaborn as sns |
| 10 | + |
| 11 | + |
| 12 | +np.random.seed(42) |
| 13 | + |
| 14 | +# Set seaborn style for consistent theming |
| 15 | +sns.set_theme(style="whitegrid", palette="colorblind") |
| 16 | + |
| 17 | +# Data - Sales hierarchy: Region -> Category breakdown |
| 18 | +# Main level (regions) |
| 19 | +main_labels = ["North America", "Europe", "Asia Pacific", "Latin America"] |
| 20 | +main_values = [45000, 32000, 28000, 15000] |
| 21 | +main_total = sum(main_values) |
| 22 | + |
| 23 | +# Drilldown data for "North America" (shown as example of drill-down view) |
| 24 | +drill_labels = ["Electronics", "Clothing", "Home & Garden", "Sports", "Books"] |
| 25 | +drill_values = [18000, 12000, 8000, 4500, 2500] |
| 26 | +drill_total = sum(drill_values) |
| 27 | + |
| 28 | +# Use seaborn color palette |
| 29 | +main_colors = sns.color_palette("colorblind", n_colors=len(main_labels)) |
| 30 | +drill_colors = sns.color_palette("Blues_r", n_colors=len(drill_labels)) |
| 31 | + |
| 32 | +# Create figure with two subplots showing drilldown concept |
| 33 | +fig, axes = plt.subplots(1, 2, figsize=(16, 9)) |
| 34 | + |
| 35 | +# Left chart - Main level pie chart (all regions) |
| 36 | +ax1 = axes[0] |
| 37 | + |
| 38 | +# Explode the first slice to show it's selected for drilldown |
| 39 | +explode_main = [0.08, 0, 0, 0] |
| 40 | + |
| 41 | +_, _, autotexts1 = ax1.pie( |
| 42 | + main_values, |
| 43 | + labels=main_labels, |
| 44 | + colors=main_colors, |
| 45 | + autopct=lambda pct: f"{pct:.1f}%\n(${int(pct * main_total / 100):,})", |
| 46 | + explode=explode_main, |
| 47 | + startangle=140, |
| 48 | + pctdistance=0.55, |
| 49 | + labeldistance=1.18, |
| 50 | + wedgeprops={"edgecolor": "white", "linewidth": 2}, |
| 51 | + textprops={"fontsize": 14}, |
| 52 | +) |
| 53 | + |
| 54 | +# Style the percentage labels |
| 55 | +for autotext in autotexts1: |
| 56 | + autotext.set_fontsize(12) |
| 57 | + autotext.set_fontweight("bold") |
| 58 | + autotext.set_color("white") |
| 59 | + |
| 60 | +# Add click indicator annotation pointing to North America |
| 61 | +ax1.annotate( |
| 62 | + "Click to\ndrill down", |
| 63 | + xy=(0.25, 0.45), |
| 64 | + xycoords="axes fraction", |
| 65 | + fontsize=12, |
| 66 | + ha="center", |
| 67 | + fontweight="bold", |
| 68 | + bbox={"boxstyle": "round,pad=0.4", "facecolor": "#FFD43B", "edgecolor": "#306998", "alpha": 0.9}, |
| 69 | + arrowprops={"arrowstyle": "->", "color": "#306998", "lw": 2, "connectionstyle": "arc3,rad=0.2"}, |
| 70 | + xytext=(0.05, 0.25), |
| 71 | +) |
| 72 | + |
| 73 | +ax1.set_title("All Regions", fontsize=22, fontweight="bold", pad=15) |
| 74 | + |
| 75 | +# Right chart - Drilldown pie chart (North America breakdown) |
| 76 | +ax2 = axes[1] |
| 77 | + |
| 78 | +_, _, autotexts2 = ax2.pie( |
| 79 | + drill_values, |
| 80 | + labels=drill_labels, |
| 81 | + colors=drill_colors, |
| 82 | + autopct=lambda pct: f"{pct:.1f}%\n(${int(pct * drill_total / 100):,})", |
| 83 | + startangle=45, |
| 84 | + pctdistance=0.55, |
| 85 | + labeldistance=1.20, |
| 86 | + wedgeprops={"edgecolor": "white", "linewidth": 2}, |
| 87 | + textprops={"fontsize": 14}, |
| 88 | +) |
| 89 | + |
| 90 | +# Style the percentage labels |
| 91 | +for autotext in autotexts2: |
| 92 | + autotext.set_fontsize(11) |
| 93 | + autotext.set_fontweight("bold") |
| 94 | + autotext.set_color("white") |
| 95 | + |
| 96 | +ax2.set_title("North America Breakdown", fontsize=22, fontweight="bold", pad=15) |
| 97 | + |
| 98 | +# Add breadcrumb trail above drilldown chart |
| 99 | +ax2.annotate( |
| 100 | + "All > North America", |
| 101 | + xy=(0.5, 1.08), |
| 102 | + xycoords="axes fraction", |
| 103 | + fontsize=14, |
| 104 | + ha="center", |
| 105 | + bbox={"boxstyle": "round,pad=0.4", "facecolor": "#f0f0f0", "edgecolor": "#306998", "alpha": 0.9}, |
| 106 | +) |
| 107 | + |
| 108 | +# Add back button indicator |
| 109 | +ax2.annotate( |
| 110 | + "← Back", |
| 111 | + xy=(0.05, 1.08), |
| 112 | + xycoords="axes fraction", |
| 113 | + fontsize=13, |
| 114 | + ha="left", |
| 115 | + color="#306998", |
| 116 | + fontweight="bold", |
| 117 | + bbox={"boxstyle": "round,pad=0.3", "facecolor": "white", "edgecolor": "#306998"}, |
| 118 | +) |
| 119 | + |
| 120 | +# Add arrow between charts to show drilldown flow |
| 121 | +fig.patches.extend( |
| 122 | + [plt.Arrow(0.48, 0.5, 0.04, 0, width=0.06, transform=fig.transFigure, facecolor="#306998", edgecolor="white")] |
| 123 | +) |
| 124 | + |
| 125 | +# Main title |
| 126 | +fig.suptitle("pie-drilldown · seaborn · pyplots.ai", fontsize=26, fontweight="bold", y=0.98) |
| 127 | + |
| 128 | +# Subtitle explaining the visualization |
| 129 | +fig.text( |
| 130 | + 0.5, |
| 131 | + 0.02, |
| 132 | + "Static representation of drilldown: Click any slice to explore subcategories", |
| 133 | + ha="center", |
| 134 | + fontsize=14, |
| 135 | + style="italic", |
| 136 | + color="#666666", |
| 137 | +) |
| 138 | + |
| 139 | +plt.tight_layout(rect=[0, 0.05, 1, 0.93]) |
| 140 | +plt.savefig("plot.png", dpi=300, bbox_inches="tight") |
0 commit comments