{"spec_id":"histogram-stepwise","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nhistogram-stepwise: Step Histogram\nLibrary: plotnine 0.15.4 | Python 3.13.13\nQuality: 87/100 | Updated: 2026-05-12\n\"\"\"\n\nimport os\nimport sys\n\n\n# Remove script directory from path to avoid plotnine.py shadowing the plotnine package\nscript_dir = os.path.dirname(os.path.abspath(__file__))\nsys.path = [p for p in sys.path if os.path.abspath(p) != script_dir and p not in (\"\", \".\")]\n\nimport numpy as np\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    element_line,\n    element_rect,\n    element_text,\n    geom_step,\n    ggplot,\n    labs,\n    scale_color_manual,\n    theme,\n)\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\n# Okabe-Ito palette (first series is always #009E73)\nIMPRINT = [\"#009E73\", \"#C475FD\"]\n\n# Data - Response times (ms) for two server configurations\nnp.random.seed(42)\n\n# Configuration A: Standard server setup\nconfig_a = np.random.normal(loc=250, scale=60, size=400)\n\n# Configuration B: Optimized server setup (faster, tighter distribution)\nconfig_b = np.random.normal(loc=180, scale=40, size=400)\n\n# Calculate histograms with shared bins for comparison\nn_bins = 30\nall_data = np.concatenate([config_a, config_b])\nbin_edges = np.linspace(all_data.min() - 10, all_data.max() + 10, n_bins + 1)\n\n# Compute histogram counts for each configuration\ncounts_a, _ = np.histogram(config_a, bins=bin_edges)\ncounts_b, _ = np.histogram(config_b, bins=bin_edges)\n\n# Create step data: for step histogram, use bin edges for x values\nstep_x_a = np.repeat(bin_edges, 2)[1:-1]\nstep_y_a = np.repeat(counts_a, 2)\n\nstep_x_b = np.repeat(bin_edges, 2)[1:-1]\nstep_y_b = np.repeat(counts_b, 2)\n\n# Create DataFrames\ndf_a = pd.DataFrame({\"x\": step_x_a, \"count\": step_y_a, \"config\": \"Standard Setup\"})\ndf_b = pd.DataFrame({\"x\": step_x_b, \"count\": step_y_b, \"config\": \"Optimized Setup\"})\ndf = pd.concat([df_a, df_b], ignore_index=True)\n\n# Plot\nplot = (\n    ggplot(df, aes(x=\"x\", y=\"count\", color=\"config\"))\n    + geom_step(size=2, alpha=0.9)\n    + labs(\n        x=\"Response Time (ms)\", y=\"Frequency\", title=\"histogram-stepwise · plotnine · anyplot.ai\", color=\"Configuration\"\n    )\n    + scale_color_manual(values=IMPRINT)\n    + theme(\n        figure_size=(16, 9),\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n        panel_grid_major=element_line(color=INK, size=0.3, alpha=0.10),\n        panel_grid_minor=element_line(color=INK, size=0.2, alpha=0.05),\n        panel_border=element_rect(color=INK_SOFT, fill=None),\n        axis_title=element_text(size=20, color=INK),\n        axis_text=element_text(size=16, color=INK_SOFT),\n        axis_line=element_line(color=INK_SOFT),\n        plot_title=element_text(size=24, color=INK),\n        legend_position=\"bottom\",\n        legend_background=element_rect(fill=PAGE_BG, color=INK_SOFT),\n        legend_text=element_text(size=16, color=INK_SOFT),\n        legend_title=element_text(size=18, color=INK),\n        text=element_text(size=14),\n    )\n)\n\n# Save in the script's directory\noutput_dir = os.path.dirname(os.path.abspath(__file__))\nplot.save(os.path.join(output_dir, f\"plot-{THEME}.png\"), dpi=300, verbose=False)\n"}