Skip to main content

English · 简体中文

realviz — Real Analysis Visualizations

See Riemann and Lebesgue integration side by side. Measure theory for the eyes.

CI Python License: MIT Tests

Real analysis is abstract until you see it. realviz draws the two great theories of integration on the same function — so the difference finally clicks.

Riemann vs Lebesgue on f(x)=x^2

What's the big idea?

Every calculus student learns Riemann integration first: chop the x-axis into vertical strips, add up f(xᵢ)·Δx. Lebesgue integration flips it around — chop the y-axis instead, group points by their function value, and weigh each slice by the measure of its preimage: Σ yₖ·m(Eₖ).

Riemann partitions the x-axis Lebesgue partitions the y-axis
Riemann sum: vertical strips Lebesgue sum: horizontal strips
Same-width vertical strips Same-height horizontal slices
Sum: Σ f(xᵢ) · Δx Sum: Σ yₖ · m(Eₖ)
Strip color = x-position Slice color = y-value

Same total area. Completely different philosophy.

Where Riemann breaks: the Dirichlet function

D(x) = 1 if x is rational, 0 otherwise. Every sub-interval of [0,1] contains both a rational and an irrational — so the upper sum is always 1, the lower sum always 0, and the gap never closes. Riemann gives up.

Lebesgue measures the irrationals (1) and the rationals (0), and gets a clean answer: ∫D = 0. This single function motivated a whole field.

Dirichlet function: Riemann fails, Lebesgue succeeds

Measure zero, yet uncountable: the Cantor set

Remove the middle third of [0, 1], then the middle third of every piece that remains, forever. Row k shows what is left after k removals; the right panel tracks the surviving measure. m(E_k) = (2/3)^k collapses to 0 exponentially — yet the limiting set has as many points as the real line.

Cantor set construction and measure decay

The Cantor function is the staircase this process grows into: continuous, non-decreasing, flat almost everywhere (F' = 0 a.e.), yet it climbs from 0 to 1 — the counterexample that kills the naive fundamental theorem of calculus.

Cantor function — the devil's staircase

Install

pip install realviz

Quick start

from realviz import compare_integrals, riemann_plot, lebesgue_plot

# Side-by-side comparison — the main event
compare_integrals(lambda x: x**2, 0, 1, n_partitions=15)

# Or inspect each method alone
riemann_plot(lambda x: x**2, 0, 1, n_partitions=10, method="midpoint")
lebesgue_plot(lambda x: x**2, 0, 1, n_partitions=10)

# The Dirichlet function — where Riemann fails, Lebesgue succeeds
from realviz import dirichlet_illustration
dirichlet_illustration()

# The Cantor set and the devil's staircase
from realviz import cantor_set, cantor_function
cantor_set(n_levels=4)     # construction + measure decay
cantor_function()          # F' = 0 a.e., yet F(1) - F(0) = 1

Try it yourself

python examples/demo_compare.py     # x² and sin(x), side by side
python examples/demo_dirichlet.py   # the Dirichlet illustration
python examples/demo_cantor.py      # Cantor set + devil's staircase

Design principles

  • Safe by default — strict input validation: functions are sampled defensively, partition counts are bounded, no eval/exec/pickle, no network, no file I/O.
  • Pedagogical color — every strip is colored by what is being partitioned, so the axis-choice is visible at a glance.
  • Zero magic — pure NumPy + Matplotlib, ~800 lines of core code, MIT licensed.

Roadmap

  • v0.1.0 — Riemann vs Lebesgue integral comparison + Dirichlet illustration
  • v0.2.0 — Cantor set construction + Cantor function (devil's staircase)
  • v0.3.0 — Simple-function approximation of measurable functions
  • v0.4.0 — Convergence modes (pointwise, uniform, a.e., in L¹)
  • v0.5.0 — Vitali set and non-measurable sets (conceptual)

Development

git clone https://github.com/HunterZ549/realviz.git
cd realviz
pip install -e ".[dev]"
pytest               # 86 tests, CI runs Python 3.9–3.12

License

MIT — see LICENSE.

Metadata

Release files for realviz 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for realviz 0.2.0
File Size Uploaded
realviz-0.2.0.tar.gz 655.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for realviz 0.2.0
File Interpreter ABI Platform
realviz-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 670.2 kB

Release files / realviz-0.2.0.tar.gz

Download URL realviz-0.2.0.tar.gz
Size 655.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b5e34a64eb59a87d2213aba8abc7bdaf0ba1178a473713574d7b1903631f8107
BLAKE2b-256 checksum
How to use checksums
e9cabd0122f79d3d9fd8dd061a36fc2df156ba35dfbbe6a21fcf935b8cc2fc7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release files / realviz-0.2.0-py3-none-any.whl

Download URL realviz-0.2.0-py3-none-any.whl
Size 15.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
655c18db47d69721f8915ede7f67474ec2abe6c3dd927cda3a27290c23e6a9c6
BLAKE2b-256 checksum
How to use checksums
85675e4c1e2a11d4efeab1e74fd796ee9555fa90b94628fcf5d54dbec303026e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release history Release notifications | RSS feed

0.5.0

2 release files

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page