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

Bar Chart with Error Bars in plotnine (Python)

The same plot in 14 other libraries — Python: Altair, Bokeh, lets-plot, Matplotlib, Plotly, 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 plotnine

Renders

Python source (plotnine)

""" anyplot.ai
bar-error: Bar Chart with Error Bars
Library: plotnine 0.15.8 | Python 3.14.10
Quality: 84/100 | Updated: 2026-11-09
"""

import os

import pandas as pd
from plotnine import (
    aes,
    element_blank,
    element_line,
    element_rect,
    element_text,
    geom_col,
    geom_errorbar,
    ggplot,
    labs,
    scale_x_discrete,
    scale_y_continuous,
    theme,
    theme_minimal,
)


# Theme tokens (see prompts/default-style-guide.md "Background" + "Theme-adaptive Chrome")
THEME = os.getenv("ANYPLOT_THEME", "light")
PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17"
INK = "#1A1A17" if THEME == "light" else "#F0EFE8"
INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0"
INK_MUTED = "#6B6A63" if THEME == "light" else "#A8A79F"
BRAND = "#009E73"  # Imprint palette position 1 — a single-series plot uses it alone

# Columns: set these to your table's column names
SURVEY_CATEGORY = "Survey category"  # role: category
SATISFACTION = "Satisfaction score (1–5)"  # role: value
MARGIN = "95% CI half-width"  # role: error

# Data: replace this block with your own table, for example df = pd.read_csv("survey.csv")
df = pd.DataFrame(
    {
        SURVEY_CATEGORY: [
            "Product Quality",
            "Customer Service",
            "Delivery Speed",
            "Price Value",
            "Website UX",
            "Return Policy",
        ],
        SATISFACTION: [4.2, 3.8, 4.5, 3.5, 4.0, 4.3],
        MARGIN: [0.3, 0.4, 0.2, 0.5, 0.35, 0.25],
    }
)

# Plot: reads only df and the constants above
bounds = df.assign(lower=df[SATISFACTION] - df[MARGIN], upper=df[SATISFACTION] + df[MARGIN])
order = df.sort_values(SATISFACTION, ascending=False)[SURVEY_CATEGORY].tolist()

plot = (
    ggplot(bounds, aes(x=SURVEY_CATEGORY, y=SATISFACTION))
    + geom_col(fill=BRAND, width=0.65)
    + geom_errorbar(aes(ymin="lower", ymax="upper"), width=0.22, size=0.8, color=INK)
    + scale_x_discrete(limits=order)
    + scale_y_continuous(limits=(0, None), expand=(0, 0, 0.06, 0))
    + labs(title="bar-error · python · plotnine · anyplot.ai", caption=f"Error bars: ± {MARGIN}")
    + theme_minimal()
    + theme(
        figure_size=(8, 4.5),
        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),
        panel_background=element_rect(fill=PAGE_BG, color=PAGE_BG),
        panel_grid_major_x=element_blank(),
        panel_grid_minor=element_blank(),
        panel_grid_major_y=element_line(color=INK, size=0.3, alpha=0.12),
        axis_line_x=element_line(color=INK_SOFT, size=0.5),
        axis_ticks=element_blank(),
        plot_title=element_text(size=12, weight="bold", color=INK),
        axis_title=element_text(size=10, color=INK),
        axis_text=element_text(size=8, color=INK_SOFT),
        plot_caption=element_text(size=8, style="italic", color=INK_MUTED),
    )
)

# Save
plot.save(f"plot-{THEME}.png", dpi=400, width=8, height=4.5, units="in", verbose=False)

Retrieve this implementation

Runnable source as JSON, for any HTTP client: https://api.anyplot.ai/specs/bar-error/plotnine/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": "plotnine",
  "page": "https://anyplot.ai/bar-error/python/plotnine",
  "hub": "https://anyplot.ai/bar-error",
  "code_json": "https://api.anyplot.ai/specs/bar-error/plotnine/code",
  "spec_json": "https://api.anyplot.ai/specs/bar-error",
  "render_light_png": "https://storage.googleapis.com/anyplot-images/plots/bar-error/python/plotnine/plot-light.png",
  "render_dark_png": "https://storage.googleapis.com/anyplot-images/plots/bar-error/python/plotnine/plot-dark.png",
  "quality_score": 84.0,
  "license": "MIT",
  "guide": "https://anyplot.ai/llms.txt"
}

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

Other implementations