Bar Chart

Python · Data Visualization

Theory

A bar chart compares a number across categories, with bar length proportional to the value. ax.bar(categories, values) draws vertical bars and ax.barh draws horizontal ones, which suit long category names or ranked lists. Bars must start at zero, otherwise their lengths exaggerate differences.

To compare two series per category, draw a grouped chart: put categories at positions x = np.arange(n), then draw one series at x - width/2 and the other at x + width/2. A stacked chart shows parts of a total: draw the first series, then pass bottom=first_series to the second so it starts where the first ends. ax.bar_label(bars) writes the value on each bar. Sorting bars by value usually makes the chart much easier to read.

Why we use it

Comparing amounts across groups (sales by city, headcount by department) is the most common business question, and lengths from a shared baseline are the most accurately read visual encoding.

When to use it

Use bars for categorical comparisons and barh for long labels or rankings. Use grouped bars to compare series side by side and stacked bars when the total matters as much as the parts. For trends over time, a line plot is usually better.

How it works

  1. Categories and values
  2. More than one series per category? Yes: Choose grouped or stacked. No: Single ax.bar or ax.barh.
  3. Is the total the main message? Yes: Stacked: bottom=previous series. No: Grouped: offset x by width/2.
  4. Sort bars and add ax.bar_label for exact values
  5. Bar chart that compares categories

Syntax

ax.bar(x, height, width=0.8, color=...)
ax.barh(y, width)
ax.bar(x - w/2, a, w); ax.bar(x + w/2, b, w)   # grouped
ax.bar(x, a); ax.bar(x, b, bottom=a)          # stacked
ax.bar_label(bars)

Example

fig, ax = plt.subplots()
bars = ax.bar(['Jaipur', 'Pune', 'Delhi'], [100, 90, 150])
ax.bar_label(bars)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic