Histogram

Python · Data Visualization

Theory

A histogram shows the distribution of one numeric variable. It splits the range of values into bins (intervals) and draws a bar for each bin whose height is how many values fall inside it. That reveals the shape of the data: where most values sit, how spread out they are, whether there is a long tail and whether there are outliers.

ax.hist(values, bins=10) chooses 10 equal-width bins between the minimum and maximum; you can also pass explicit edges such as bins=[0, 15, 30, 45, 60]. Each bin includes its left edge, and the last bin also includes the maximum. The number of bins changes the story: too few hide detail and too many look noisy. ax.hist returns the counts and bin edges, so you can print exactly what the chart shows; density=True scales heights so the total area is 1.

Why we use it

Averages hide shape. A histogram shows whether delivery times are tightly clustered or have a long tail of late orders, which tells you whether the mean is a fair summary and whether outliers need attention.

When to use it

Use a histogram for one continuous variable such as amounts, times or ages. Try a few bin counts before deciding. To compare distributions across groups, use box plots or overlapping histograms with alpha; for categories, use a bar chart instead.

How it works

  1. One numeric column
  2. Choose bins: a number or a list of edges
  3. Matplotlib counts values falling in each interval
  4. Does the shape look too blocky or too noisy? Yes: Change the number of bins and redraw. No: Keep the chart.
  5. Distribution: centre, spread, skew, outliers

Syntax

counts, edges, patches = ax.hist(values, bins=10)
ax.hist(values, bins=[0, 15, 30, 45, 60])
ax.hist(values, bins=20, density=True, alpha=0.6)

Example

times = [18, 22, 25, 31, 34, 41, 55]
fig, ax = plt.subplots()
ax.hist(times, bins=4, edgecolor='white')

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