|
| 1 | +""" pyplots.ai |
| 2 | +area-stacked-percent: 100% Stacked Area Chart |
| 3 | +Library: letsplot 4.8.2 | Python 3.13.11 |
| 4 | +Quality: 91/100 | Created: 2025-12-30 |
| 5 | +""" |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import pandas as pd |
| 9 | +from lets_plot import ( |
| 10 | + LetsPlot, |
| 11 | + aes, |
| 12 | + element_blank, |
| 13 | + element_line, |
| 14 | + element_rect, |
| 15 | + element_text, |
| 16 | + geom_area, |
| 17 | + ggplot, |
| 18 | + ggsize, |
| 19 | + labs, |
| 20 | + scale_fill_manual, |
| 21 | + scale_x_continuous, |
| 22 | + scale_y_continuous, |
| 23 | + theme, |
| 24 | + theme_minimal, |
| 25 | +) |
| 26 | +from lets_plot.export import ggsave |
| 27 | + |
| 28 | + |
| 29 | +LetsPlot.setup_html() |
| 30 | + |
| 31 | +# Data - Market share evolution over 8 years |
| 32 | +np.random.seed(42) |
| 33 | + |
| 34 | +years = list(range(2016, 2024)) |
| 35 | + |
| 36 | +# Simulate market share trends (values will be normalized to 100%) |
| 37 | +company_a = [40, 38, 42, 45, 48, 52, 55, 58] # Growing leader |
| 38 | +company_b = [35, 36, 33, 30, 28, 25, 23, 22] # Declining |
| 39 | +company_c = [15, 16, 15, 16, 15, 14, 13, 12] # Stable small player |
| 40 | +company_d = [10, 10, 10, 9, 9, 9, 9, 8] # Smallest, slight decline |
| 41 | + |
| 42 | +# Normalize to 100% |
| 43 | +totals = [a + b + c + d for a, b, c, d in zip(company_a, company_b, company_c, company_d)] |
| 44 | +company_a_pct = [a / t * 100 for a, t in zip(company_a, totals)] |
| 45 | +company_b_pct = [b / t * 100 for b, t in zip(company_b, totals)] |
| 46 | +company_c_pct = [c / t * 100 for c, t in zip(company_c, totals)] |
| 47 | +company_d_pct = [d / t * 100 for d, t in zip(company_d, totals)] |
| 48 | + |
| 49 | +# Create long-format dataframe for lets-plot |
| 50 | +df = pd.DataFrame( |
| 51 | + { |
| 52 | + "Year": years * 4, |
| 53 | + "Share": company_a_pct + company_b_pct + company_c_pct + company_d_pct, |
| 54 | + "Company": ["Company A"] * 8 + ["Company B"] * 8 + ["Company C"] * 8 + ["Company D"] * 8, |
| 55 | + } |
| 56 | +) |
| 57 | + |
| 58 | +# Set category order for proper stacking |
| 59 | +df["Company"] = pd.Categorical( |
| 60 | + df["Company"], categories=["Company D", "Company C", "Company B", "Company A"], ordered=True |
| 61 | +) |
| 62 | + |
| 63 | +# Plot |
| 64 | +plot = ( |
| 65 | + ggplot(df, aes(x="Year", y="Share", fill="Company")) |
| 66 | + + geom_area(position="fill", alpha=0.85) |
| 67 | + + scale_fill_manual(values=["#9B59B6", "#2ECC71", "#FFD43B", "#306998"]) |
| 68 | + + scale_x_continuous(breaks=list(range(2016, 2024))) |
| 69 | + + scale_y_continuous(format=".0%") |
| 70 | + + labs(x="Year", y="Market Share (%)", title="area-stacked-percent · letsplot · pyplots.ai") |
| 71 | + + theme_minimal() |
| 72 | + + theme( |
| 73 | + plot_title=element_text(size=26), |
| 74 | + axis_title=element_text(size=22), |
| 75 | + axis_text=element_text(size=18), |
| 76 | + legend_title=element_text(size=18), |
| 77 | + legend_text=element_text(size=16), |
| 78 | + panel_grid_major=element_line(color="#DDDDDD", size=0.3), |
| 79 | + panel_grid_minor=element_blank(), |
| 80 | + panel_background=element_rect(fill="#FAFAFA"), |
| 81 | + ) |
| 82 | + + ggsize(1600, 900) |
| 83 | +) |
| 84 | + |
| 85 | +# Save PNG (scale=3 gives 4800x2700) |
| 86 | +ggsave(plot, "plot.png", path=".", scale=3) |
| 87 | + |
| 88 | +# Save HTML for interactivity |
| 89 | +ggsave(plot, "plot.html", path=".") |
0 commit comments