Figures
Every figure in this manual is a MarketFigure, or a mosaickit
canvas built by a function such as ppf_canvas(). Both have a square plot
area and arrowed axes with no ticks or grid. Curves are named at their ends,
areas inside them and values on the axes, and no text covers a line, a
point, an area or other text.
MarketFigure
MarketFigure
Colours and line widths (Palettes and themes).
Shorthand for PlotTheme.from_palette(palette); theme takes precedence when both are given.
Overrides for built-in labels, by layer id (Labels and layers).
Shows or hides layers, by layer id (Labels and layers).
A market diagram on a mosaickit canvas, with axes from 0 to
x_max and y_max. Axis titles are LaTeX math unless they are words; an
empty title draws none.
MarketFigure.add_curves(
demand,
supply,
q_max,
demand_label="$D$",
supply_label="$S$"
)
MarketFigure.add_equilibrium(
result,
label="$e^*$",
color=None
)
Draw demand and supply from \(Q = 0\) to q_max, each named at its end, and
mark an equilibrium with a filled point and its label.
Each topic adds its own methods, described in its chapter (see
Topic methods of MarketFigure).
finalize() hides guide lines that would cut a shaded welfare region in
two (the value stays marked on its axis) and, only with legend=True,
adds a legend. save() writes PNG, SVG or PDF according to the extension
and creates missing directories. close() is a no-op kept for
compatibility.
MarketFigure.add_layer(layer)
MarketFigure.add_layers(layers)
Add any mosaickit layer, for annotations the topic methods do not
provide. An axis mark replaces an earlier mark at the same value. The
finished scene is available as fig.scene.
MarketFigure.add_metrics(
values,
*,
title=None,
location="upper right"
)
A text box of name--value pairs, for notebooks and debugging. Teaching figures leave it out: the numbers belong in the text.
Labels and layers
Every line, point, area and piece of text in a figure is a layer with a
stable id, such as market.demand, market.demand.label or
market.welfare.dwl. The ids follow the structure of the figure:
axes.* for the axes, market.<part> for each element, and a .label
suffix for the text that names it. fig.layer_ids lists every layer;
fig.label_ids lists the configurable text (labels, axis marks and
braces).
Label(
text=None,
visible=None,
offset=None,
top=None,
bottom=None,
left=None,
right=None
)
An override for one built-in label. Fields left as None keep the
package's default. An offset in points opts the label out of automatic
placement and moves it by exactly that much. top, bottom, left and
right instead move the label after it has been placed, so principle-viz
still picks the spot and you only fine-tune it.
MarketFigure.configure_label(
layer id,
label=None,
*,
text=None,
visible=None,
offset=None,
top=None,
bottom=None,
left=None,
right=None
)
MarketFigure.configure_layer(
layer id,
*,
visible
)
Rename, hide or move one label (the id may omit its final .label), or
show or hide any layer. hide(*ids) and show(*ids) toggle several at
once. Each returns the figure.
The same overrides can be passed when the figure is created, as labels=
and visibility= mappings; they then apply to layers added later as well.
Aggregation figures and the canvas functions (ppf_canvas(),
public_good_canvas(), ...) accept the same two arguments. See
Renamed curves and equilibrium:
from principle_viz import Label, MarketFigure
fig = MarketFigure(
x_max=12,
y_max=12,
labels={
"market.demand.label": Label(text="Demand"),
"market.supply.label": Label(text="Supply"),
},
)
fig.add_curves(demand, supply, q_max=10)
fig.add_equilibrium(eq)
fig.configure_label("market.equilibrium", text="$e^*$")
fig.hide("axes.origin.label")
fig.finalize()
Renamed curves and equilibrium
Nudging a label
A length is a number of points or a string with a unit: "10pt", "0.5em"
(the label's font size) or "2mm". top and bottom combine, as do
right and left. A nudge applies after automatic placement, or after an
offset; later labels still avoid the moved one, and figures without
nudges are unchanged. Axis tick labels do not take nudges. The equilibrium
label's id is market.equilibrium:
from principle_viz import Label, MarketFigure
fig = MarketFigure(
labels={"market.equilibrium": Label(top="4pt")}
)
fig.configure_label("market.supply", Label(right="8pt"))
fig.configure_label("market.demand", text="$D_0$", left="0.5em")
Palettes and themes
A ColorModel assigns a colour to each economic role; a PlotTheme adds line
widths and axis options and compiles both into a mosaickit theme.
The built-in colour models are the themes every EconViz package shares
(Built-in colour models): the same name gives the same look in utility-viz and
principle-viz.
ColorModel(
name,
axis_color,
label_color,
demand_color,
supply_color,
...
)
Named colour roles: axis_color, label_color, demand_color,
supply_color, baseline_color, shifted_color, tax_color,
control_color, cs_color, ps_color, tax_revenue_color, dwl_color
and arrow_color. Each is "#hex" or a mosaickit palette name
such as "blue".
The greys of the EconViz manuals: black axes, supply and arrows, a grey demand curve, grey surplus regions. For print and handouts
Demand blue, supply green, deadweight loss red, taxes and controls orange; the surpluses follow the curves. The everyday choice
The colours of default under an explicit name
Black curves and a grey ladder for the regions, for black-and-white printing
Cool colours of the Nord palette, for screens and slides
Muted colours, for papers
Brighter colours, with lines 1.5 and the equilibrium dot 1.3 times as large, for projection
A shared theme gives a colour to each role, and principle_viz.visuals.roles
says which role each element takes: the axis, labels, baseline and arrows
take ink, demand primary, supply secondary, shifted curves tertiary, taxes
and controls intervention, and consumer surplus, producer surplus,
deadweight loss and tax revenue the light, mid, dark and faint fills. The
theme's line and marker scales multiply principle-viz's own line widths and
equilibrium dot. There is no dark theme.
list_color_models() returns the names and get_color_model(name) the
model; DEFAULT_COLOR_MODEL, COLORBLIND_COLOR_MODEL, NORD_COLOR_MODEL
and MONOCHROME_COLOR_MODEL are also exported.
PlotTheme(
color_model=...,
demand_linewidth=3.5,
supply_linewidth=3.5,
...
)
PlotTheme.from_palette(name, **options)
A colour model with line widths (demand_linewidth,
supply_linewidth, shifted_linewidth, tax_linewidth,
arrow_linewidth, dashed_linewidth), equilibrium_marker_size, and
the switches show_grid, show_ticks, show_axis_arrows and
show_origin_label. from_palette() starts from a built-in colour model
and sets any of these fields.
Fonts take three fields. font is "sans" (the default), "serif" or
"mono"; an installed family name such as "Arial" is an error. fonts is a
mosaickit.TextFonts that replaces any of the three with font files, the rest
keeping mosaickit's DejaVu defaults. math_font is a MathFont or
the path of an OpenType math font, and sets $...$ through glyphkit.
font_roles maps a style role to a generic font, for example
{"axes": "mono"}. Nothing searches the system for fonts, so pass the paths.
from mosaickit import TextFonts
from principle_viz import MarketFigure, PlotTheme
theme = PlotTheme(
font="serif",
fonts=TextFonts(
serif=[
"fonts/Pagella-Regular.otf",
"fonts/Pagella-Bold.otf",
]
),
math_font="fonts/Pagella-Math.otf",
)
fig = MarketFigure(x_max=12, y_max=12, theme=theme)
The palette argument selects a colour model (see The monochrome palette):
fig = MarketFigure(x_max=12, y_max=12, palette="monochrome")
The monochrome palette
Settings file
principle-viz reads econ-viz.toml, the settings file every EconViz package
shares; mosaickit reads and checks it. The top level sets what every
package shares: the starting theme (base, one of Built-in colour models), the fonts
and the axis and label colours. principle-viz's own settings live in the
[principle] table; the tables of other packages, such as [utility], are
skipped.
base = "manual" # a shared theme: manual, default, colorblind,
# monochrome, nord, paper or presentation
[font]
text = "serif"
math = "fonts/texgyrepagella-math.otf"
[fonts]
serif = ["fonts/NotoSerifTC-Regular.otf", "fonts/NotoSerifTC-Bold.otf"]
[principle]
language = "zh-TW" # built-in figure text
[principle.color] # a colour-model role, without "_color"
demand = "#8c8c8c"
[principle.stroke] # line widths in points
demand = 3.5
[principle.marker]
equilibrium = 6.5 # equilibrium dot diameter in points
base = "manual" gives the black-and-white look of this manual, so its
colours and widths need not be spelled out. [principle] palette still
sets the colours for principle-viz alone and wins over base;
[principle.color] sets one element. Relative paths are read from the
folder of the file.
Config.load
Config.use
default_theme
Config.load() reads the file and use() makes it the default: figures
made without theme= or palette= then use it, and default_theme()
returns the theme they get. A theme= or palette= passed to a figure
still wins. mosaickit.econ_toml.use("econ-viz.toml") loads the file for
every installed EconViz package at once.
A mistake raises mosaickit.ConfigurationError naming the file and the
table, with the closest key when one is close:
Label defaults
[principle.label.<group>] moves every label of a group by default, so one
setting in the file replaces a Label(top=...) on each figure. The five
groups follow what a label names (Label groups); each takes top,
bottom, left and right.
| Group | Labels |
|---|---|
point |
Equilibrium points (e_0, e^*, e_1, e_t, L^*, the discrete Q^* = ..., p^* = ...) and the points of a production possibilities frontier |
curve |
Curves named at their end: D, S, D_1, S + t, MSB, MSC, MC, \Sigma MB, PPF, PPF_0, PPF_1 |
region |
Names inside filled regions, callouts with a leader line included: consumer and producer surplus, deadweight loss, tax revenue, subsidy cost, tariff revenue |
mark |
Price lines and policy marks: p_c, p_f, w_min, p_w, p_w + t, and the t = ..., s = ... and f = ... wedges |
annotation |
Other text: brace labels (Shortage, Tax, Subsidy, Imports, Unemployment), price intervals, metrics and the elasticity zone names |
Axis labels (the axis names, the origin, the marks on the axes such as Q_0
and p_1, and their notes) belong to no group and cannot be nudged. A length
is a number of points or a string with the unit pt, mm, cm or in;
em is rejected, because the file has no font size. top and bottom
combine, as do right and left. A key that is not set means no move, and
a figure without [principle.label.*] settings is drawn as before:
[principle.label.point]
top = "2pt" # equilibrium labels 2 pt higher
[principle.label.curve]
right = "4pt" # curve names 4 pt to the right
[principle.label.region]
bottom = "1mm"
left = "0.5mm"
A label given its own top, bottom, left or right (through
Label(top=...), labels= or configure_label()) is moved by those values
and not by its group's default: the per-label nudge replaces the
default; the two do not add up. A Label that only renames, hides or sets an
offset keeps the default nudge. A callout's leader line keeps pointing at
the moved label.
Each default is a mosaickit.Nudge in PlotTheme.label_nudges, keyed by
group name, and principle_viz.label_groups.label_group(layer) returns the
group of a label layer, or None for one that takes no default.
Canvases
Canvas
The mosaickit Canvas with the same labels=, visibility=,
configure_label(), configure_layer(), hide(), show(),
layer_ids and label_ids as MarketFigure. The canvas functions of
the PPF, public-good and revenue chapters return it; save it with
save(), or combine several with mosaickit.CanvasGrid.