|
1 | 1 | """ pyplots.ai |
2 | 2 | pie-basic: Basic Pie Chart |
3 | | -Library: letsplot 4.8.1 | Python 3.13.11 |
4 | | -Quality: 91/100 | Created: 2025-12-23 |
| 3 | +Library: letsplot 4.8.2 | Python 3.14.0 |
| 4 | +Quality: 82/100 | Created: 2025-12-23 |
5 | 5 | """ |
6 | 6 |
|
7 | | -import pandas as pd |
8 | 7 | from lets_plot import * # noqa: F403 |
9 | | -from lets_plot.export import ggsave as export_ggsave |
| 8 | +from lets_plot import ggsave |
10 | 9 |
|
11 | 10 |
|
12 | 11 | LetsPlot.setup_html() # noqa: F405 |
13 | 12 |
|
14 | | -# Data - Market share by company |
15 | | -categories = ["Company A", "Company B", "Company C", "Company D", "Company E"] |
16 | | -values = [35, 25, 20, 12, 8] |
| 13 | +# Data - Global smartphone market share (2024) |
| 14 | +data = { |
| 15 | + "company": ["Apple", "Samsung", "Xiaomi", "OPPO", "Others"], |
| 16 | + "share": [23.1, 19.4, 13.7, 8.8, 35.0], |
| 17 | + "explode": [0.0, 0.0, 0.0, 0.12, 0.0], |
| 18 | +} |
17 | 19 |
|
18 | | -df = pd.DataFrame({"category": categories, "value": values}) |
| 20 | +# Colors - Python Blue first, then colorblind-safe palette |
| 21 | +colors = ["#306998", "#FFD43B", "#4CAF50", "#AB47BC", "#90A4AE"] |
19 | 22 |
|
20 | | -# Calculate percentages for labels |
21 | | -total = sum(values) |
22 | | -df["pct"] = df["value"] / total * 100 |
23 | | - |
24 | | -# Preserve category order |
25 | | -df["category"] = pd.Categorical(df["category"], categories=categories, ordered=True) |
26 | | - |
27 | | -# Define colors - Python Blue first, then colorblind-safe palette |
28 | | -colors = ["#306998", "#FFD43B", "#4CAF50", "#FF7043", "#AB47BC"] |
29 | | - |
30 | | -# Plot |
| 23 | +# Plot — square canvas fills space evenly for circular pie charts |
31 | 24 | plot = ( |
32 | | - ggplot(df) # noqa: F405 |
| 25 | + ggplot(data) # noqa: F405 |
33 | 26 | + geom_pie( # noqa: F405 |
34 | | - aes(slice="value", fill="category"), # noqa: F405 |
| 27 | + aes(slice="share", fill="company", explode="explode"), # noqa: F405 |
35 | 28 | stat="identity", |
36 | | - size=20, # Large size for visibility at 4800x2700 |
37 | | - hole=0, # Full pie (not donut) |
| 29 | + size=75, |
| 30 | + hole=0, |
| 31 | + stroke=2, |
| 32 | + stroke_side="both", |
| 33 | + color="white", |
| 34 | + spacer_width=1.5, |
| 35 | + spacer_color="white", |
38 | 36 | labels=layer_labels() # noqa: F405 |
39 | | - .line("@pct") |
40 | | - .format("pct", "{.1f}%") |
41 | | - .size(14), |
| 37 | + .line("@{share}") |
| 38 | + .format("share", "{.1f}%") |
| 39 | + .size(18), |
42 | 40 | ) |
43 | 41 | + scale_fill_manual(values=colors) # noqa: F405 |
44 | 42 | + labs( # noqa: F405 |
45 | | - title="pie-basic · letsplot · pyplots.ai", fill="Category" |
| 43 | + title="Global Smartphone Market Share · pie-basic · letsplot · pyplots.ai", |
| 44 | + subtitle="OPPO's 8.8% slice is the smallest — 'Others' dominate at 35%", |
| 45 | + fill="Brand", |
46 | 46 | ) |
47 | | - + ggsize(1600, 900) # noqa: F405 |
| 47 | + + ggsize(1200, 1200) # noqa: F405 |
48 | 48 | + theme_void() # noqa: F405 |
49 | 49 | + theme( # noqa: F405 |
50 | | - plot_title=element_text(size=24, hjust=0.5), # noqa: F405 |
51 | | - legend_title=element_text(size=18), # noqa: F405 |
52 | | - legend_text=element_text(size=16), # noqa: F405 |
| 50 | + plot_title=element_text(size=26, hjust=0.5, face="bold"), # noqa: F405 |
| 51 | + plot_subtitle=element_text(size=16, hjust=0.5, color="#555555"), # noqa: F405 |
| 52 | + legend_title=element_text(size=20), # noqa: F405 |
| 53 | + legend_text=element_text(size=18), # noqa: F405 |
| 54 | + plot_margin=[30, 20, 30, 20], |
| 55 | + legend_position="bottom", |
| 56 | + legend_direction="horizontal", |
53 | 57 | ) |
54 | 58 | ) |
55 | 59 |
|
56 | 60 | # Save |
57 | | -export_ggsave(plot, filename="plot.png", path=".", scale=3) |
58 | | -export_ggsave(plot, filename="plot.html", path=".") |
| 61 | +ggsave(plot, filename="plot.png", path=".", scale=3) |
| 62 | +ggsave(plot, filename="plot.html", path=".") |
0 commit comments