|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | bar-diverging: Diverging Bar Chart |
3 | | -Library: plotnine 0.15.2 | Python 3.13.11 |
4 | | -Quality: 94/100 | Created: 2025-12-25 |
| 3 | +Library: plotnine 0.15.4 | Python 3.13.13 |
| 4 | +Quality: 91/100 | Updated: 2026-05-08 |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import importlib.util |
| 8 | +import os |
| 9 | +import sys |
| 10 | + |
7 | 11 | import pandas as pd |
8 | | -from plotnine import ( |
9 | | - aes, |
10 | | - coord_flip, |
11 | | - element_line, |
12 | | - element_text, |
13 | | - geom_bar, |
14 | | - geom_hline, |
15 | | - ggplot, |
16 | | - labs, |
17 | | - scale_fill_manual, |
18 | | - theme, |
19 | | - theme_minimal, |
20 | | -) |
21 | 12 |
|
22 | 13 |
|
| 14 | +# Handle import conflicts: remove current dir and cache |
| 15 | +sys.path = [p for p in sys.path if not p.endswith("python")] |
| 16 | +if "plotnine" in sys.modules: |
| 17 | + del sys.modules["plotnine"] |
| 18 | + |
| 19 | +# Import plotnine explicitly from site-packages |
| 20 | +plotnine_spec = importlib.util.find_spec("plotnine") |
| 21 | +plotnine = importlib.util.module_from_spec(plotnine_spec) |
| 22 | +sys.modules["plotnine"] = plotnine |
| 23 | +plotnine_spec.loader.exec_module(plotnine) |
| 24 | + |
| 25 | +aes = plotnine.aes |
| 26 | +coord_flip = plotnine.coord_flip |
| 27 | +element_line = plotnine.element_line |
| 28 | +element_rect = plotnine.element_rect |
| 29 | +element_text = plotnine.element_text |
| 30 | +geom_bar = plotnine.geom_bar |
| 31 | +geom_hline = plotnine.geom_hline |
| 32 | +ggplot = plotnine.ggplot |
| 33 | +labs = plotnine.labs |
| 34 | +scale_fill_manual = plotnine.scale_fill_manual |
| 35 | +theme = plotnine.theme |
| 36 | +theme_minimal = plotnine.theme_minimal |
| 37 | + |
| 38 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 39 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 40 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 41 | +INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
| 42 | + |
23 | 43 | # Data - Customer satisfaction survey across product categories |
24 | | -# Net satisfaction score: % satisfied - % dissatisfied (range -100 to +100) |
25 | 44 | categories = [ |
26 | 45 | "Mobile App", |
27 | 46 | "Customer Service", |
|
47 | 66 | # Create ordered categorical for proper sorting in plot |
48 | 67 | df["category"] = pd.Categorical(df["category"], categories=df["category"], ordered=True) |
49 | 68 |
|
50 | | -# Color based on positive/negative |
| 69 | +# Color based on positive/negative (Okabe-Ito palette) |
51 | 70 | df["sentiment"] = df["value"].apply(lambda x: "Positive" if x >= 0 else "Negative") |
52 | 71 |
|
53 | 72 | # Plot |
54 | 73 | plot = ( |
55 | 74 | ggplot(df, aes(x="category", y="value", fill="sentiment")) |
56 | 75 | + geom_bar(stat="identity", width=0.7) |
57 | | - + geom_hline(yintercept=0, color="#333333", size=0.8) |
| 76 | + + geom_hline(yintercept=0, color=INK_SOFT, size=0.8) |
58 | 77 | + coord_flip() |
59 | | - + scale_fill_manual(values={"Positive": "#306998", "Negative": "#E74C3C"}) |
| 78 | + + scale_fill_manual(values={"Positive": "#009E73", "Negative": "#D55E00"}) |
60 | 79 | + labs( |
61 | 80 | x="Product Category", |
62 | 81 | y="Net Satisfaction Score (%)", |
63 | | - title="bar-diverging · plotnine · pyplots.ai", |
| 82 | + title="bar-diverging · plotnine · anyplot.ai", |
64 | 83 | fill="Sentiment", |
65 | 84 | ) |
66 | 85 | + theme_minimal() |
67 | 86 | + theme( |
| 87 | + plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG), |
| 88 | + panel_background=element_rect(fill=PAGE_BG), |
| 89 | + panel_grid_major_y=element_line(color=INK, size=0.3, alpha=0.10), |
| 90 | + panel_grid_minor=element_line(color=INK, size=0.2, alpha=0.05), |
| 91 | + axis_title=element_text(color=INK, size=20), |
| 92 | + axis_text=element_text(color=INK_SOFT, size=16), |
| 93 | + plot_title=element_text(color=INK, size=24), |
| 94 | + legend_background=element_rect(fill=PAGE_BG, color=INK_SOFT), |
| 95 | + legend_text=element_text(color=INK_SOFT, size=16), |
| 96 | + legend_title=element_text(color=INK, size=18), |
68 | 97 | figure_size=(16, 9), |
69 | | - text=element_text(size=14), |
70 | | - axis_title=element_text(size=20), |
71 | | - axis_text=element_text(size=16), |
72 | | - plot_title=element_text(size=24, ha="center"), |
73 | | - legend_text=element_text(size=16), |
74 | | - legend_title=element_text(size=18), |
75 | | - legend_position="right", |
76 | | - panel_grid_major_y=element_line(alpha=0.3), |
77 | | - panel_grid_minor=element_line(alpha=0), |
78 | 98 | ) |
79 | 99 | ) |
80 | 100 |
|
81 | 101 | # Save |
82 | | -plot.save("plot.png", dpi=300, verbose=False) |
| 102 | +plot.save(f"plot-{THEME}.png", dpi=300, verbose=False) |
0 commit comments