Skip to main content

A Python toolkit for producing publication-quality microeconomics diagrams.

Project description

Econ-Viz

PyPI Python License Tests Coverage

A Python toolkit for producing publication-quality microeconomics diagrams. Define utility functions declaratively, solve for consumer equilibria, and export figures as raster images or LaTeX/TikZ source — all in a few lines of code.

Installation

pip install econ-viz

Requires Python 3.12 or later.

Quick Start

import numpy as np
from econ_viz import Canvas, levels, solve
from econ_viz.models import CobbDouglas

model = CobbDouglas(alpha=0.5, beta=0.5)
eq    = solve(model, px=2.0, py=3.0, income=30.0)
lvls  = levels.around(eq.utility, n=5)

cvs = Canvas(x_max=20, y_max=15, x_label="x", y_label="y",
             title="Cobb-Douglas  $x^{0.5} y^{0.5}$")
cvs.add_utility(model, levels=lvls)
cvs.add_budget(2.0, 3.0, 30.0, fill=True)
cvs.add_equilibrium(eq, show_ray=True)
cvs.save("cobb_douglas.png")

Cobb-Douglas indifference map with budget line and equilibrium point

CLI

econ-viz ships with a command-line interface for generating diagrams without writing Python.

Commands

Command Description
econ-viz help [<command>] Show help for the CLI or a specific command
econ-viz models List all supported utility models
econ-viz plot ... Generate and export a diagram

Examples

# Cobb-Douglas with equilibrium and budget line
econ-viz plot --model cobb-douglas --alpha 0.5 --beta 0.5 \
              --px 2 --py 3 --income 30 \
              --output cobb_douglas.png

# Parse a LaTeX expression directly
econ-viz plot --latex "x^{0.4} y^{0.6}" \
              --px 2 --py 3 --income 30 \
              --output cd_latex.png

# Leontief with Nord theme and expansion-path ray
econ-viz plot --model leontief --a 1 --b 2 \
              --px 2 --py 3 --income 30 \
              --theme nord --show-ray \
              --output leontief.png

# CES — indifference curves only, no budget or equilibrium
econ-viz plot --model ces --rho -0.5 \
              --x-max 20 --y-max 15 --n-curves 6 \
              --no-budget --no-equilibrium \
              --output ces.png

# Omit --output to open an interactive window
econ-viz plot --model cobb-douglas --px 2 --py 3 --income 30

plot options

Flag Default Description
--model, -m Model name (see econ-viz models)
--latex, -l LaTeX expression (Cobb-Douglas / Leontief / Perfect Substitutes)
--px, --py, --income Prices and budget
--alpha, --beta 0.5 Cobb-Douglas / CES share parameters
--a, --b 1.0 Leontief / Perfect Substitutes / Satiation coefficients
--rho 0.5 CES substitution parameter
--bliss-x, --bliss-y 5.0 Satiation bliss point
--x-max, --y-max 10 Canvas axis limits
--x-label, --y-label x, y Axis labels
--title Figure title
--theme default Colour theme: default, nord
--n-curves 5 Number of indifference curves
--dpi 300 Raster output resolution
--fill off Shade feasible set below the budget line
--show-ray off Draw expansion-path ray through the optimum
--no-budget off Omit the budget line
--no-equilibrium off Omit the equilibrium point
--no-curves off Omit indifference curves
--output, -o Output file; omit to open an interactive window

Utility Models

Cobb-Douglas

from econ_viz.models import CobbDouglas

model = CobbDouglas(alpha=0.3, beta=0.7)

Cobb-Douglas indifference curves

Leontief (Perfect Complements)

from econ_viz.models import Leontief

model = Leontief(a=1.0, b=1.0)   # U = min(ax, by)

Leontief indifference curves

Perfect Substitutes

from econ_viz.models import PerfectSubstitutes

model = PerfectSubstitutes(a=1.0, b=2.0)   # U = ax + by

Perfect substitutes indifference curves

CES

from econ_viz.models import CES

model = CES(rho=-0.5, alpha=0.5)   # elasticity of substitution = 1/(1+rho)

CES indifference curves

Satiation (Bliss Point)

from econ_viz.models import Satiation

model = Satiation(bliss_x=6.0, bliss_y=4.0, a=1.0, b=1.0)

Satiation indifference curves

Quasi-Linear

import numpy as np
from econ_viz.models import QuasiLinear

model = QuasiLinear(v_func=np.log, linear_in="y")   # U = log(x) + y

Quasi-linear indifference curves

LaTeX Input

Parse standard LaTeX math expressions directly into model instances:

from econ_viz import parse_latex

cd  = parse_latex(r"x^{0.4} y^{0.6}")
leo = parse_latex(r"\min(2x, 3y)")
ps  = parse_latex(r"2x + 3y")

The parser accepts common preambles such as U(x,y) =, U =, and bare expressions. Unrecognised patterns raise ParseError.

Parsed Cobb-Douglas from LaTeX

Advanced Models

Custom Utility

Wrap any vectorised Python callable as a first-class model. The callable is validated at construction time against a random NumPy mesh-grid.

import numpy as np
from econ_viz.models import CustomUtility

model = CustomUtility(func=lambda x, y: np.log(x) + np.log(y), name="log+log")

Custom utility indifference map

Multi-Good Cobb-Douglas

Model preferences over N goods and project to a 2-D canvas via freeze():

from econ_viz.models import MultiGoodCD

m3   = MultiGoodCD({'x': 0.3, 'y': 0.3, 'z': 0.4})
flat = m3.freeze(z=10.0)   # returns a CustomUtility ready for Canvas
from econ_viz import Canvas, levels, solve

eq   = solve(flat, px=2.0, py=3.0, income=30.0)
lvls = levels.around(eq.utility, n=5)

cvs = Canvas(x_max=20, y_max=15, title=r"MultiGoodCD  $z=10$")
cvs.add_utility(flat, levels=lvls)
cvs.add_budget(2.0, 3.0, 30.0, fill=True)
cvs.add_equilibrium(eq)
cvs.save("multigood.png")

Multi-good Cobb-Douglas frozen slice

Solving for Equilibrium

solve() returns an Equilibrium named tuple with fields x, y, and utility:

from econ_viz import solve
from econ_viz.models import CobbDouglas

eq = solve(CobbDouglas(), px=2.0, py=3.0, income=30.0)
print(eq.x, eq.y, eq.utility)

Themes

from econ_viz import Canvas, themes

cvs = Canvas(x_max=20, y_max=15)                    # default theme
cvs = Canvas(x_max=20, y_max=15, theme=themes.nord) # nord theme
Default Nord
Default theme Nord theme

Export

cvs.save("figure.png")    # raster (DPI controlled by Canvas(dpi=300))
cvs.save("figure.tikz")   # TikZ/PGFPlots source for LaTeX

License

MIT © Anthony Sung

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

econ_viz-0.1.3.tar.gz (28.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

econ_viz-0.1.3-py3-none-any.whl (37.6 kB view details)

Uploaded Python 3

File details

Details for the file econ_viz-0.1.3.tar.gz.

File metadata

  • Download URL: econ_viz-0.1.3.tar.gz
  • Upload date:
  • Size: 28.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.12 Linux/6.17.0-19-generic

File hashes

Hashes for econ_viz-0.1.3.tar.gz
Algorithm Hash digest
SHA256 8280876bfb13ac5888e2f1dd8a516bbe9b92188c06e6427039ca095788fe7077
MD5 f5e65b950aa9c209d212d91453b2807c
BLAKE2b-256 93e9bd6c2c6e69b8f89ee5007103aaa07869d958b0d729f58ff61fbdc9dad2bd

See more details on using hashes here.

File details

Details for the file econ_viz-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: econ_viz-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 37.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.12 Linux/6.17.0-19-generic

File hashes

Hashes for econ_viz-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 01a65b38b8a3a5d55071c2c91c502981b7336bc4c0c1ec23f2dd3caaecf657ed
MD5 859a6beb75dbb8ac05ddcb6ad4239c89
BLAKE2b-256 1103829b713dead0e3dea845b70613d503f4f91eb71668c2b4a9fc6d61c79798

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page