Module unit¶
The unit module provides classes and functions for specifying, scaling,
and converting lengths in PyX. Every length in PyX combines a numerical value,
a measurement unit ("m", "cm", "mm", "inch", or "pt"), and a
combination of five semantic length categories:
"t"(true length)Unscaled physical length. True lengths ignore all user scaling factors and are returned by PyX measurement methods such as
path.path.arclength()orbbox.bbox.width()."u"(user length)The default category for geometric coordinates, positions, and path dimensions. Plain numbers passed to non-
_ptPyX classes and methods are automatically interpreted as user lengths in the current default unit."v"(visual length)Used for the size of visual ornaments such as arrowheads, graph symbols, and axis tick marks. Scaling visual lengths changes the size of markers and labels without altering the overall geometry of a diagram.
"w"(width length)Used for line widths (
style.linewidth). Scaling width lengths lets you thicken or thin all strokes globally (for example, when preparing a figure for a presentation or high-DPI print)."x"(TeX length)Used to scale all TeX/LaTeX and
text.UnicodeEnginetext output.
Because a length instance stores all five components independently, you
can freely add lengths of different types—such as 1 * unit.u_cm + 2 * unit.v_mm—and
PyX will resolve the combined length using the active scaling factors when
drawing.
Configuring Units and Scaling Factors¶
Unless specified otherwise, plain numbers in PyX are interpreted in terms of the
default unit, which is "cm" (1 centimeter). You can change the default unit
and the scaling factors for the four scalable length categories using
unit.set():
- unit.set(uscale=None, vscale=None, wscale=None, xscale=None, defaultunit=None)¶
Updates the scaling factors for user (uscale), visual (vscale), width (wscale), and TeX (xscale) lengths, or changes defaultunit (which must be one of
"m","cm","mm","inch", or"pt"). Any argument left asNoneretains its current setting.
For example, to double all line widths and increase text size by 20% for a slide deck while keeping the figure dimensions unchanged:
from pyx import *
unit.set(wscale=2.0, xscale=1.2)
Or to work in inches instead of centimeters throughout your script:
unit.set(defaultunit="inch")
Class length¶
- class unit.length(f=0, type='u', unit=None)¶
Constructs a length with numerical factor f, length category type (one of
"u","v","w","x", or"t"; default"u"), and measurement unit (one of"m","cm","mm","inch", or"pt"; defaults to the active defaultunit).lengthinstances support full arithmetic and comparison operations:Addition and subtraction (
l1 + l2,l1 - l2): Sums or subtracts anotherlengthcomponent-wise. If a plain number is added or subtracted, it is first converted to a user length in the current default unit.Multiplication and division by a scalar (
c * l,l / c): Scales all five components oflby the numberc, returning a newlength.Division of two lengths (
l1 / l2): Converts both lengths to meters using the current scaling factors and returns their dimensionlessfloatratio.Comparisons (
l1 == l2,l1 < l2, etc.): Evaluates both lengths in meters using the current scaling factors and compares the resulting values.
Predefined Length Constants¶
Rather than instantiating length directly, it is usually most
convenient to multiply a number by one of the predefined unit constants in
unit—for example, 5 * unit.w_pt or 1.5 * unit.v_cm:
Constant |
Type |
Unit |
|---|---|---|
|
user |
meters |
|
user |
centimeters |
|
user |
millimeters |
|
user |
inches |
|
user |
points (1/72”) |
|
true |
meters |
|
true |
centimeters |
|
true |
millimeters |
|
true |
inches |
|
true |
points (1/72”) |
|
user |
meters |
|
user |
centimeters |
|
user |
millimeters |
|
user |
inches |
|
user |
points (1/72”) |
|
visual |
meters |
|
visual |
centimeters |
|
visual |
millimeters |
|
visual |
inches |
|
visual |
points (1/72”) |
|
width |
meters |
|
width |
centimeters |
|
width |
millimeters |
|
width |
inches |
|
width |
points (1/72”) |
|
TeX |
meters |
|
TeX |
centimeters |
|
TeX |
millimeters |
|
TeX |
inches |
|
TeX |
points (1/72”) |
Conversion Functions¶
To evaluate a length (or a plain number, which is treated as a user
length in the current default unit) as a float in a specific physical unit
taking the current scaling factors into account, use the following conversion
functions:
Function |
Return value ( |
|---|---|
|
Value of |
|
Value of |
|
Value of |
|
Value of |
|
Value of |
Note
PyX internals use topt() extensively at the boundary between user-facing
methods and low-level _pt classes.