Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                

Bar Chart with Error Bars in lets-plot (Python)

The same plot in 14 other libraries — Python: Altair, Bokeh, Matplotlib, Plotly, plotnine, Pygal, Seaborn; R: ggplot2; Julia: Makie.jl; JavaScript: Chart.js, D3.js, Apache ECharts, Highcharts, MUI X Charts. Compare all 15 side by side: Bar Chart with Error Bars in Python, R, Julia and JavaScript.

A bar chart with error bars displays categorical data as rectangular bars with vertical (or horizontal) lines extending from each bar to indicate uncertainty or variability. Error bars typically represent standard deviation, standard error, confidence intervals, or min/max ranges. This visualization is essential for comparing group means while communicating the reliability and precision of each measurement.

Bar Chart with Error Bars rendered with lets-plot

Renders

Python source (lets-plot)

""" anyplot.ai
bar-error: Bar Chart with Error Bars
Library: letsplot 4.11.0 | Python 3.14.10
Quality: 80/100 | Updated: 2026-11-20
"""

import os

import pandas as pd
from lets_plot import (
    LetsPlot,
    aes,
    element_line,
    element_rect,
    element_text,
    geom_bar,
    geom_errorbar,
    ggplot,
    ggsave,
    ggsize,
    labs,
    scale_fill_manual,
    theme,
)


LetsPlot.setup_html()

# Theme tokens
THEME = os.getenv("ANYPLOT_THEME", "light")
PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17"
ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420"
INK = "#1A1A17" if THEME == "light" else "#F0EFE8"
INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0"
GRID_COLOR = "rgba(26,26,23,0.08)" if THEME == "light" else "rgba(240,239,232,0.08)"

# Imprint palette - first series is brand green
IMPRINT_PALETTE = ["#009E73", "#C475FD", "#4467A3", "#BD8233", "#AE3030", "#2ABCCD", "#954477", "#99B314"]

# Columns: set these to your table's column names
GROUP = "Test group"  # role: category
RATE = "Conversion rate (%)"  # role: value
ERROR = "Lower error (%)"  # role: error
ERROR_UPPER = "Upper error (%)"  # role: extra

# Data: A/B test conversion rates with asymmetric 95% CI arms; replace with your own table, e.g. pd.read_csv("ab_test.csv")
df = pd.DataFrame(
    {
        GROUP: ["Control", "Variant A", "Variant B", "Variant C", "Variant D"],
        RATE: [12.3, 14.8, 11.2, 16.5, 13.9],
        ERROR: [1.2, 1.5, 1.0, 1.8, 1.4],
        ERROR_UPPER: [1.4, 1.6, 1.1, 2.0, 1.5],
    }
)

# Plot: reads only df and the constants above
ERR_MIN, ERR_MAX = "ymin", "ymax"
bounds = df.copy()
bounds[ERR_MIN] = df[RATE] - df[ERROR]
bounds[ERR_MAX] = df[RATE] + df[ERROR_UPPER]

plot = (
    ggplot(bounds, aes(x=GROUP, y=RATE, fill=GROUP))
    + geom_bar(stat="identity", width=0.68, show_legend=False, alpha=0.92)
    + geom_errorbar(aes(ymin=ERR_MIN, ymax=ERR_MAX), width=0.23, size=1.0, color=INK_SOFT)
    + scale_fill_manual(values=IMPRINT_PALETTE)
    + labs(title="bar-error · python · letsplot · anyplot.ai", caption="Error bars show the 95% confidence interval")
    + theme(
        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),
        panel_background=element_rect(fill=PAGE_BG),
        panel_grid_major_y=element_line(color=GRID_COLOR, size=0.22),
        panel_grid_major_x=element_line(color="transparent"),
        panel_grid_minor=element_line(color="transparent"),
        axis_title=element_text(size=12, color=INK),
        axis_text=element_text(size=10, color=INK_SOFT),
        plot_title=element_text(size=16, color=INK),
        plot_caption=element_text(size=10, color=INK_SOFT),
    )
    + ggsize(800, 450)
)

# Save as PNG and HTML with theme suffix
ggsave(plot, f"plot-{THEME}.png", path=".", scale=4)
ggsave(plot, f"plot-{THEME}.html", path=".")

Retrieve this implementation

Runnable source as JSON, for any HTTP client: https://api.anyplot.ai/specs/bar-error/letsplot/code. Any spec id and library id listed in llms-full.txt fit the same URL shape; every URL below is complete and callable.

{
  "spec_id": "bar-error",
  "language": "python",
  "library": "letsplot",
  "page": "https://anyplot.ai/bar-error/python/letsplot",
  "hub": "https://anyplot.ai/bar-error",
  "code_json": "https://api.anyplot.ai/specs/bar-error/letsplot/code",
  "spec_json": "https://api.anyplot.ai/specs/bar-error",
  "render_light_png": "https://storage.googleapis.com/anyplot-images/plots/bar-error/python/letsplot/plot-light.png",
  "render_dark_png": "https://storage.googleapis.com/anyplot-images/plots/bar-error/python/letsplot/plot-dark.png",
  "interactive_light_html": "https://storage.googleapis.com/anyplot-images/plots/bar-error/python/letsplot/plot-light.html",
  "interactive_dark_html": "https://storage.googleapis.com/anyplot-images/plots/bar-error/python/letsplot/plot-dark.html",
  "quality_score": 80.0,
  "license": "MIT",
  "guide": "https://anyplot.ai/llms.txt"
}

Part of Bar Chart with Error Bars on anyplot.ai.

Other implementations