Tutorial 07 — TikZ Backend

TikZ is the de-facto standard for drawing in LaTeX documents.
maxplotlib can render your figures as native TikZ code via the tikzfigure backend, which wraps the `tikzfigure <https://github.com/max-models/tikzfigure>`__ Python package.

This tutorial covers two complementary workflows:

Workflow

When to use

Canvas → TikZ

Quick way to turn data plots into LaTeX-ready TikZ code

``tikzfigure`` API directly

Full control — nodes, shapes, annotations, arcs, colours …

Prerequisites

pip install tikzfigure

To actually render the figure (not just generate code) you also need pdflatex installed on your system.

[1]:
import numpy as np
import tikzfigure as tz
from maxplotlib import Canvas

Part 1 — Canvas → TikZ

The fastest path: build a plot with the standard Canvas API, then pass backend='tikzfigure' to get a TikzFigure object back.

1.1 Basic usage

[2]:
x = np.linspace(0, 2 * np.pi, 60)

canvas = Canvas(width="10cm", ratio=0.6)
canvas.plot(x, np.sin(x), label="sin", color="steelblue", line_width=1.5)
canvas.plot(x, np.cos(x), label="cos", color="tomato", line_width=1.2)
canvas.set_xlabel("x")
canvas.set_ylabel("y")
canvas.set_title("Trigonometric functions")

# backend='tikzfigure' returns a TikzFigure object
tikz = canvas.render(backend="tikzfigure")
print(type(tikz))
<class 'tikzfigure.core.figure.TikzFigure'>

Plotly preview

Before exporting to TikZ, you can preview the same Canvas interactively in a notebook using the Plotly backend:

[3]:
canvas.show(backend="plotly")

Data type cannot be displayed: application/vnd.plotly.v1+json

[4]:
canvas.show(backend="tikzfigure")
warning: The `fitz` API is deprecated and will be removed in future. Use `import pymupdf` instead.
../_images/tutorials_tutorial_07_tikz_7_1.png

1.2 Inspecting the generated LaTeX

str(tikz) returns the raw LaTeX source string (and generate_tikz() remains available explicitly).
Each data line becomes a \draw command connecting coordinate pairs.
[5]:
print(tikz)
\begin{tikzpicture}
    \begin{groupplot}[group style={group size=1 by 1, horizontal sep=1.5cm}]
            \nextgroupplot[xlabel=x, ylabel=y, grid=false, height=6cm, title=Trigonometric functions, axis width=10cm]
            \addplot[color=steelblue] coordinates {(0.0,0.0) (0.10649466622338281,0.1062934856473654) (0.21298933244676563,0.21138262362962432) (0.31948399867014843,0.31407671202194876) (0.42597866489353126,0.41321218576837815) (0.5324733311169141,0.5076658003388399) (0.6389679973402969,0.5963673585385013) (0.7454626635636797,0.6783118362696161) (0.8519573297870625,0.7525707698561385) (0.9584519960104453,0.818302775908169) (1.0649466622338282,0.8747630845319613) (1.171441328457211,0.921311977870413) (1.2779359946805937,0.9574220383620055) (1.3844306609039765,0.9826841245925209) (1.4909253271273595,0.9968120070307501) (1.5974199933507423,0.9996456111234526) (1.703914659574125,0.9911528310040072) (1.8104093257975078,0.97142989326471) (1.9169039920208906,0.9407002666710333) (2.0233986582442736,0.8993121301712192) (2.1298933244676563,0.847734427889671) (2.236387990691039,0.7865515558026426) (2.342882656914422,0.7164567402983153) (2.4493773231378047,0.6382441836448203) (2.5558719893611874,0.5528000653611936) (2.66236665558457,0.4610925014493262) (2.768861321807953,0.3641605752528225) (2.875355988031336,0.26310256422752126) (2.981850654254719,0.1590634960190721) (3.0883453204781017,0.053222174842178754) (3.1948399867014845,-0.053222174842178505) (3.3013346529248673,-0.15906349601907185) (3.40782931914825,-0.26310256422752104) (3.514323985371633,-0.3641605752528219) (3.6208186515950156,-0.46109250144932556) (3.7273133178183984,-0.5528000653611931) (3.833807984041781,-0.6382441836448197) (3.940302650265164,-0.7164567402983147) (4.046797316488547,-0.7865515558026425) (4.15329198271193,-0.8477344278896709) (4.259786648935313,-0.8993121301712191) (4.366281315158695,-0.9407002666710332) (4.472775981382078,-0.9714298932647099) (4.579270647605461,-0.9911528310040071) (4.685765313828844,-0.9996456111234525) (4.7922599800522265,-0.9968120070307502) (4.898754646275609,-0.9826841245925211) (5.005249312498992,-0.9574220383620057) (5.111743978722375,-0.9213119778704132) (5.218238644945758,-0.8747630845319616) (5.32473331116914,-0.8183027759081695) (5.431227977392523,-0.752570769856139) (5.537722643615906,-0.6783118362696167) (5.644217309839289,-0.5963673585385021) (5.750711976062672,-0.50766580033884) (5.857206642286055,-0.4132121857683782) (5.963701308509438,-0.3140767120219489) (6.070195974732821,-0.21138262362962446) (6.1766906409562035,-0.1062934856473656) (6.283185307179586,-2.4492935982947064e-16)};
            \addplot[color=tomato] coordinates {(0.0,1.0) (0.10649466622338281,0.9943348002101371) (0.21298933244676563,0.9774033898178667) (0.31948399867014843,0.9493976084683813) (0.42597866489353126,0.9106347728549132) (0.5324733311169141,0.8615540813938061) (0.6389679973402969,0.8027116379309637) (0.7454626635636797,0.7347741508630673) (0.8519573297870625,0.6585113790650386) (0.9584519960104453,0.5747874102144069) (1.0649466622338282,0.4845508703326502) (1.171441328457211,0.38882417547332077) (1.2779359946805937,0.28869194733962117) (1.3844306609039765,0.18528872408711453) (1.4909253271273595,0.0797861055530831) (1.5974199933507423,-0.026620521437774693) (1.703914659574125,-0.13272552728372186) (1.8104093257975078,-0.23732669987111465) (1.9169039920208906,-0.33923886611803006) (2.0233986582442736,-0.43730732045885534) (2.1298933244676563,-0.5304209081197424) (2.236387990691039,-0.6175246149461918) (2.342882656914422,-0.6976315211349846) (2.4493773231378047,-0.769833983429906) (2.5558719893611874,-0.8333139190825148) (2.66236665558457,-0.8873520750565714) (2.768861321807953,-0.9313361774523383) (2.875355988031336,-0.9647678688145159) (2.981850654254719,-0.9872683547213446) (3.0883453204781017,-0.9985826956767619) (3.1948399867014845,-0.9985826956767619) (3.3013346529248673,-0.9872683547213446) (3.40782931914825,-0.964767868814516) (3.514323985371633,-0.9313361774523385) (3.6208186515950156,-0.8873520750565718) (3.7273133178183984,-0.8333139190825152) (3.833807984041781,-0.7698339834299065) (3.940302650265164,-0.697631521134985) (4.046797316488547,-0.617524614946192) (4.15329198271193,-0.5304209081197426) (4.259786648935313,-0.43730732045885556) (4.366281315158695,-0.3392388661180305) (4.472775981382078,-0.2373266998711151) (4.579270647605461,-0.1327255272837223) (4.685765313828844,-0.02662052143777516) (4.7922599800522265,0.07978610555308265) (4.898754646275609,0.18528872408711383) (5.005249312498992,0.2886919473396205) (5.111743978722375,0.3888241754733201) (5.218238644945758,0.4845508703326496) (5.32473331116914,0.5747874102144063) (5.431227977392523,0.6585113790650381) (5.537722643615906,0.7347741508630667) (5.644217309839289,0.8027116379309631) (5.750711976062672,0.8615540813938061) (5.857206642286055,0.9106347728549131) (5.963701308509438,0.9493976084683813) (6.070195974732821,0.9774033898178666) (6.1766906409562035,0.9943348002101371) (6.283185307179586,1.0)};

    \end{groupplot}
\end{tikzpicture}

1.2.1 Checking explicit width and height

When you set both width= and ratio=, the TikZ export now writes explicit pgfplots dimensions. This is useful when you want a tall figure for a column-sized layout in LaTeX.

[6]:
canvas_ratio2, ax_ratio2 = Canvas.subplots(width="10cm", ratio=2)
ax_ratio2.plot(x, np.exp(-x / np.pi), color="purple", line_width=1.5)
ax_ratio2.set_title("ratio = 2 export")

tikz_ratio2 = canvas_ratio2.render(backend="tikzfigure")
ratio2_code = tikz_ratio2.generate_tikz()

for line in ratio2_code.splitlines():
    if "nextgroupplot" in line:
        print(line.strip())
        break

# Expected: width=10cm and height=20cm in the \nextgroupplot options
\nextgroupplot[grid=false, height=20cm, title=ratio = 2 export, axis width=10cm]

1.3 TikZ-specific kwargs

The TikZ backend passes extra keyword arguments straight to tikzfigure.draw().
Use ``line_width=`` (not matplotlib’s linewidth=) to control stroke thickness.
[7]:
canvas2, ax2 = Canvas.subplots(width="10cm", ratio=0.5)
ax2.plot(x, np.sin(x), color="navy", line_width=0.5, label="thin")
ax2.plot(x, np.sin(x) + 0.5, color="steelblue", line_width=1.5, label="medium")
ax2.plot(x, np.sin(x) + 1.0, color="royalblue", line_width=3.0, label="thick")
ax2.set_xlabel("x")
ax2.set_title("Line width comparison")

tikz2 = canvas2.render(backend="tikzfigure")
print(tikz2.generate_tikz())
\begin{tikzpicture}
    \begin{groupplot}[group style={group size=1 by 1, horizontal sep=1.5cm}]
            \nextgroupplot[xlabel=x, grid=false, height=5cm, title=Line width comparison, axis width=10cm]
            \addplot[color=navy] coordinates {(0.0,0.0) (0.10649466622338281,0.1062934856473654) (0.21298933244676563,0.21138262362962432) (0.31948399867014843,0.31407671202194876) (0.42597866489353126,0.41321218576837815) (0.5324733311169141,0.5076658003388399) (0.6389679973402969,0.5963673585385013) (0.7454626635636797,0.6783118362696161) (0.8519573297870625,0.7525707698561385) (0.9584519960104453,0.818302775908169) (1.0649466622338282,0.8747630845319613) (1.171441328457211,0.921311977870413) (1.2779359946805937,0.9574220383620055) (1.3844306609039765,0.9826841245925209) (1.4909253271273595,0.9968120070307501) (1.5974199933507423,0.9996456111234526) (1.703914659574125,0.9911528310040072) (1.8104093257975078,0.97142989326471) (1.9169039920208906,0.9407002666710333) (2.0233986582442736,0.8993121301712192) (2.1298933244676563,0.847734427889671) (2.236387990691039,0.7865515558026426) (2.342882656914422,0.7164567402983153) (2.4493773231378047,0.6382441836448203) (2.5558719893611874,0.5528000653611936) (2.66236665558457,0.4610925014493262) (2.768861321807953,0.3641605752528225) (2.875355988031336,0.26310256422752126) (2.981850654254719,0.1590634960190721) (3.0883453204781017,0.053222174842178754) (3.1948399867014845,-0.053222174842178505) (3.3013346529248673,-0.15906349601907185) (3.40782931914825,-0.26310256422752104) (3.514323985371633,-0.3641605752528219) (3.6208186515950156,-0.46109250144932556) (3.7273133178183984,-0.5528000653611931) (3.833807984041781,-0.6382441836448197) (3.940302650265164,-0.7164567402983147) (4.046797316488547,-0.7865515558026425) (4.15329198271193,-0.8477344278896709) (4.259786648935313,-0.8993121301712191) (4.366281315158695,-0.9407002666710332) (4.472775981382078,-0.9714298932647099) (4.579270647605461,-0.9911528310040071) (4.685765313828844,-0.9996456111234525) (4.7922599800522265,-0.9968120070307502) (4.898754646275609,-0.9826841245925211) (5.005249312498992,-0.9574220383620057) (5.111743978722375,-0.9213119778704132) (5.218238644945758,-0.8747630845319616) (5.32473331116914,-0.8183027759081695) (5.431227977392523,-0.752570769856139) (5.537722643615906,-0.6783118362696167) (5.644217309839289,-0.5963673585385021) (5.750711976062672,-0.50766580033884) (5.857206642286055,-0.4132121857683782) (5.963701308509438,-0.3140767120219489) (6.070195974732821,-0.21138262362962446) (6.1766906409562035,-0.1062934856473656) (6.283185307179586,-2.4492935982947064e-16)};
            \addplot[color=steelblue] coordinates {(0.0,0.5) (0.10649466622338281,0.6062934856473654) (0.21298933244676563,0.7113826236296243) (0.31948399867014843,0.8140767120219488) (0.42597866489353126,0.9132121857683781) (0.5324733311169141,1.00766580033884) (0.6389679973402969,1.0963673585385014) (0.7454626635636797,1.178311836269616) (0.8519573297870625,1.2525707698561384) (0.9584519960104453,1.318302775908169) (1.0649466622338282,1.3747630845319612) (1.171441328457211,1.4213119778704129) (1.2779359946805937,1.4574220383620053) (1.3844306609039765,1.4826841245925209) (1.4909253271273595,1.49681200703075) (1.5974199933507423,1.4996456111234526) (1.703914659574125,1.4911528310040072) (1.8104093257975078,1.4714298932647099) (1.9169039920208906,1.4407002666710333) (2.0233986582442736,1.3993121301712192) (2.1298933244676563,1.347734427889671) (2.236387990691039,1.2865515558026426) (2.342882656914422,1.2164567402983153) (2.4493773231378047,1.1382441836448203) (2.5558719893611874,1.0528000653611937) (2.66236665558457,0.9610925014493261) (2.768861321807953,0.8641605752528225) (2.875355988031336,0.7631025642275213) (2.981850654254719,0.6590634960190721) (3.0883453204781017,0.5532221748421787) (3.1948399867014845,0.4467778251578215) (3.3013346529248673,0.3409365039809281) (3.40782931914825,0.23689743577247896) (3.514323985371633,0.13583942474717808) (3.6208186515950156,0.038907498550674435) (3.7273133178183984,-0.05280006536119308) (3.833807984041781,-0.13824418364481972) (3.940302650265164,-0.21645674029831474) (4.046797316488547,-0.28655155580264247) (4.15329198271193,-0.3477344278896709) (4.259786648935313,-0.39931213017121914) (4.366281315158695,-0.44070026667103324) (4.472775981382078,-0.47142989326470985) (4.579270647605461,-0.4911528310040071) (4.685765313828844,-0.4996456111234525) (4.7922599800522265,-0.49681200703075024) (4.898754646275609,-0.4826841245925211) (5.005249312498992,-0.4574220383620057) (5.111743978722375,-0.4213119778704132) (5.218238644945758,-0.3747630845319616) (5.32473331116914,-0.31830277590816947) (5.431227977392523,-0.252570769856139) (5.537722643615906,-0.17831183626961666) (5.644217309839289,-0.09636735853850209) (5.750711976062672,-0.007665800338840012) (5.857206642286055,0.0867878142316218) (5.963701308509438,0.18592328797805108) (6.070195974732821,0.28861737637037554) (6.1766906409562035,0.3937065143526344) (6.283185307179586,0.4999999999999998)};
            \addplot[color=royalblue] coordinates {(0.0,1.0) (0.10649466622338281,1.1062934856473654) (0.21298933244676563,1.2113826236296243) (0.31948399867014843,1.3140767120219488) (0.42597866489353126,1.4132121857683781) (0.5324733311169141,1.50766580033884) (0.6389679973402969,1.5963673585385014) (0.7454626635636797,1.678311836269616) (0.8519573297870625,1.7525707698561384) (0.9584519960104453,1.818302775908169) (1.0649466622338282,1.8747630845319612) (1.171441328457211,1.9213119778704129) (1.2779359946805937,1.9574220383620053) (1.3844306609039765,1.9826841245925209) (1.4909253271273595,1.99681200703075) (1.5974199933507423,1.9996456111234526) (1.703914659574125,1.9911528310040072) (1.8104093257975078,1.9714298932647099) (1.9169039920208906,1.9407002666710333) (2.0233986582442736,1.8993121301712192) (2.1298933244676563,1.847734427889671) (2.236387990691039,1.7865515558026426) (2.342882656914422,1.7164567402983153) (2.4493773231378047,1.6382441836448203) (2.5558719893611874,1.5528000653611937) (2.66236665558457,1.4610925014493261) (2.768861321807953,1.3641605752528225) (2.875355988031336,1.2631025642275213) (2.981850654254719,1.1590634960190722) (3.0883453204781017,1.0532221748421788) (3.1948399867014845,0.9467778251578215) (3.3013346529248673,0.8409365039809281) (3.40782931914825,0.7368974357724789) (3.514323985371633,0.6358394247471781) (3.6208186515950156,0.5389074985506744) (3.7273133178183984,0.4471999346388069) (3.833807984041781,0.3617558163551803) (3.940302650265164,0.28354325970168526) (4.046797316488547,0.21344844419735753) (4.15329198271193,0.1522655721103291) (4.259786648935313,0.10068786982878086) (4.366281315158695,0.059299733328966764) (4.472775981382078,0.028570106735290146) (4.579270647605461,0.008847168995992916) (4.685765313828844,0.0003543888765474845) (4.7922599800522265,0.0031879929692497644) (4.898754646275609,0.01731587540747892) (5.005249312498992,0.04257796163799432) (5.111743978722375,0.07868802212958681) (5.218238644945758,0.1252369154680384) (5.32473331116914,0.18169722409183053) (5.431227977392523,0.24742923014386098) (5.537722643615906,0.32168816373038334) (5.644217309839289,0.4036326414614979) (5.750711976062672,0.49233419966116) (5.857206642286055,0.5867878142316219) (5.963701308509438,0.685923287978051) (6.070195974732821,0.7886173763703755) (6.1766906409562035,0.8937065143526344) (6.283185307179586,0.9999999999999998)};

    \end{groupplot}
\end{tikzpicture}

1.4 Layer-aware TikZ output

Assign data to layers with layer=N.
The TikZ backend respects the layer filter — useful for generating incremental reveal figures (e.g. in Beamer).
[8]:
canvas3, ax3 = Canvas.subplots(width="10cm", ratio=0.55)
ax3.plot(x, np.sin(x), color="steelblue", line_width=1.5, layer=0, label="sin")
ax3.plot(x, np.cos(x), color="tomato", line_width=1.5, layer=1, label="cos")
ax3.plot(
    x, np.sin(x) * np.cos(x), color="seagreen", line_width=1.0, layer=2, label="sin·cos"
)

# All layers available on the canvas
print("Available layers:", canvas3.layers)

# Render only layer 0 — one \draw command
tikz_l0 = canvas3.render(backend="tikzfigure", layers=[0])
print("\n--- Layer 0 only ---")
print(f"\\draw count: {tikz_l0.generate_tikz().count(chr(92) + 'draw')}")

# Render layers 0 and 1
tikz_l01 = canvas3.render(backend="tikzfigure", layers=[0, 1])
print("\n--- Layers 0 & 1 ---")
print(f"\\draw count: {tikz_l01.generate_tikz().count(chr(92) + 'draw')}")
Available layers: [0, 1, 2]

--- Layer 0 only ---
\draw count: 0

--- Layers 0 & 1 ---
\draw count: 0

1.5 Saving TikZ code to a file

You can embed the generated code directly in a LaTeX document:

[9]:
tikz_all = canvas3.render(backend="tikzfigure")

with open("figure.tex", "w") as f:
    f.write(tikz_all.generate_tikz())

print("Saved figure.tex")

# In your LaTeX document:
# \input{figure.tex}
# or wrap it:
# \begin{figure}[h]
#   \centering
#   \input{figure.tex}
#   \caption{My caption}
# \end{figure}
Saved figure.tex

1.6 Rendering the figure (requires pdflatex)

If pdflatex is installed, tikz.show() compiles the code and opens the PDF:

[10]:
# Requires pdflatex:
tikz_all.show(transparent=False)
../_images/tutorials_tutorial_07_tikz_19_0.png

1.7 Canvas → TikZ limitations

Feature

Supported?

Line plots (canvas.plot)

Layer filtering

line_width= kwarg

Horizontal subplots (1×n)

canvas.scatter, canvas.bar, canvas.barh

canvas.fill_between, canvas.errorbar

Axis labels / titles

For unsupported primitives, the Canvas API raises NotImplementedError; use the direct tikzfigure API for advanced TikZ shapes (Part 2 below).


Part 2 — The tikzfigure API

The tikzfigure package gives you a full Python interface to TikZ primitives. You build figures by adding nodes, paths, shapes, and annotations, then call generate_tikz() (or show()) to obtain the output.

import tikzfigure as tz
tf = tz.TikzFigure()

2.1 Drawing paths with draw()

tf.draw(nodes, ...) produces a \draw path through a list of (x, y) coordinates.

[11]:
tf = tz.TikzFigure()

x = np.linspace(0, 2 * np.pi, 60)
sin_nodes = [(float(xi), float(np.sin(xi))) for xi in x]
cos_nodes = [(float(xi), float(np.cos(xi))) for xi in x]

tf.draw(sin_nodes, color="steelblue", line_width=1.5)
tf.draw(cos_nodes, color="tomato", line_width=1.2)

print(tf.generate_tikz())
% --------------------------------------------- %
% Tikzfigure generated by tikzfigure v0.3.0     %
% https://github.com/max-models/tikzfigure      %
% --------------------------------------------- %
\begin{tikzpicture}
    \draw[color=steelblue, line width=1.5] (0.0, 0.0)
        to
        (0.10649466622338281, 0.1062934856473654)
        to
        (0.21298933244676563, 0.21138262362962432)
        to
        (0.31948399867014843, 0.31407671202194876)
        to
        (0.42597866489353126, 0.41321218576837815)
        to
        (0.5324733311169141, 0.5076658003388399)
        to
        (0.6389679973402969, 0.5963673585385013)
        to
        (0.7454626635636797, 0.6783118362696161)
        to
        (0.8519573297870625, 0.7525707698561385)
        to
        (0.9584519960104453, 0.818302775908169)
        to
        (1.0649466622338282, 0.8747630845319613)
        to
        (1.171441328457211, 0.921311977870413)
        to
        (1.2779359946805937, 0.9574220383620055)
        to
        (1.3844306609039765, 0.9826841245925209)
        to
        (1.4909253271273595, 0.9968120070307501)
        to
        (1.5974199933507423, 0.9996456111234526)
        to
        (1.703914659574125, 0.9911528310040072)
        to
        (1.8104093257975078, 0.97142989326471)
        to
        (1.9169039920208906, 0.9407002666710333)
        to
        (2.0233986582442736, 0.8993121301712192)
        to
        (2.1298933244676563, 0.847734427889671)
        to
        (2.236387990691039, 0.7865515558026426)
        to
        (2.342882656914422, 0.7164567402983153)
        to
        (2.4493773231378047, 0.6382441836448203)
        to
        (2.5558719893611874, 0.5528000653611936)
        to
        (2.66236665558457, 0.4610925014493262)
        to
        (2.768861321807953, 0.3641605752528225)
        to
        (2.875355988031336, 0.26310256422752126)
        to
        (2.981850654254719, 0.1590634960190721)
        to
        (3.0883453204781017, 0.053222174842178754)
        to
        (3.1948399867014845, -0.053222174842178505)
        to
        (3.3013346529248673, -0.15906349601907185)
        to
        (3.40782931914825, -0.26310256422752104)
        to
        (3.514323985371633, -0.3641605752528219)
        to
        (3.6208186515950156, -0.46109250144932556)
        to
        (3.7273133178183984, -0.5528000653611931)
        to
        (3.833807984041781, -0.6382441836448197)
        to
        (3.940302650265164, -0.7164567402983147)
        to
        (4.046797316488547, -0.7865515558026425)
        to
        (4.15329198271193, -0.8477344278896709)
        to
        (4.259786648935313, -0.8993121301712191)
        to
        (4.366281315158695, -0.9407002666710332)
        to
        (4.472775981382078, -0.9714298932647099)
        to
        (4.579270647605461, -0.9911528310040071)
        to
        (4.685765313828844, -0.9996456111234525)
        to
        (4.7922599800522265, -0.9968120070307502)
        to
        (4.898754646275609, -0.9826841245925211)
        to
        (5.005249312498992, -0.9574220383620057)
        to
        (5.111743978722375, -0.9213119778704132)
        to
        (5.218238644945758, -0.8747630845319616)
        to
        (5.32473331116914, -0.8183027759081695)
        to
        (5.431227977392523, -0.752570769856139)
        to
        (5.537722643615906, -0.6783118362696167)
        to
        (5.644217309839289, -0.5963673585385021)
        to
        (5.750711976062672, -0.50766580033884)
        to
        (5.857206642286055, -0.4132121857683782)
        to
        (5.963701308509438, -0.3140767120219489)
        to
        (6.070195974732821, -0.21138262362962446)
        to
        (6.1766906409562035, -0.1062934856473656)
        to
        (6.283185307179586, -2.4492935982947064e-16);
    \draw[color=tomato, line width=1.2] (0.0, 1.0)
        to
        (0.10649466622338281, 0.9943348002101371)
        to
        (0.21298933244676563, 0.9774033898178667)
        to
        (0.31948399867014843, 0.9493976084683813)
        to
        (0.42597866489353126, 0.9106347728549132)
        to
        (0.5324733311169141, 0.8615540813938061)
        to
        (0.6389679973402969, 0.8027116379309637)
        to
        (0.7454626635636797, 0.7347741508630673)
        to
        (0.8519573297870625, 0.6585113790650386)
        to
        (0.9584519960104453, 0.5747874102144069)
        to
        (1.0649466622338282, 0.4845508703326502)
        to
        (1.171441328457211, 0.38882417547332077)
        to
        (1.2779359946805937, 0.28869194733962117)
        to
        (1.3844306609039765, 0.18528872408711453)
        to
        (1.4909253271273595, 0.0797861055530831)
        to
        (1.5974199933507423, -0.026620521437774693)
        to
        (1.703914659574125, -0.13272552728372186)
        to
        (1.8104093257975078, -0.23732669987111465)
        to
        (1.9169039920208906, -0.33923886611803006)
        to
        (2.0233986582442736, -0.43730732045885534)
        to
        (2.1298933244676563, -0.5304209081197424)
        to
        (2.236387990691039, -0.6175246149461918)
        to
        (2.342882656914422, -0.6976315211349846)
        to
        (2.4493773231378047, -0.769833983429906)
        to
        (2.5558719893611874, -0.8333139190825148)
        to
        (2.66236665558457, -0.8873520750565714)
        to
        (2.768861321807953, -0.9313361774523383)
        to
        (2.875355988031336, -0.9647678688145159)
        to
        (2.981850654254719, -0.9872683547213446)
        to
        (3.0883453204781017, -0.9985826956767619)
        to
        (3.1948399867014845, -0.9985826956767619)
        to
        (3.3013346529248673, -0.9872683547213446)
        to
        (3.40782931914825, -0.964767868814516)
        to
        (3.514323985371633, -0.9313361774523385)
        to
        (3.6208186515950156, -0.8873520750565718)
        to
        (3.7273133178183984, -0.8333139190825152)
        to
        (3.833807984041781, -0.7698339834299065)
        to
        (3.940302650265164, -0.697631521134985)
        to
        (4.046797316488547, -0.617524614946192)
        to
        (4.15329198271193, -0.5304209081197426)
        to
        (4.259786648935313, -0.43730732045885556)
        to
        (4.366281315158695, -0.3392388661180305)
        to
        (4.472775981382078, -0.2373266998711151)
        to
        (4.579270647605461, -0.1327255272837223)
        to
        (4.685765313828844, -0.02662052143777516)
        to
        (4.7922599800522265, 0.07978610555308265)
        to
        (4.898754646275609, 0.18528872408711383)
        to
        (5.005249312498992, 0.2886919473396205)
        to
        (5.111743978722375, 0.3888241754733201)
        to
        (5.218238644945758, 0.4845508703326496)
        to
        (5.32473331116914, 0.5747874102144063)
        to
        (5.431227977392523, 0.6585113790650381)
        to
        (5.537722643615906, 0.7347741508630667)
        to
        (5.644217309839289, 0.8027116379309631)
        to
        (5.750711976062672, 0.8615540813938061)
        to
        (5.857206642286055, 0.9106347728549131)
        to
        (5.963701308509438, 0.9493976084683813)
        to
        (6.070195974732821, 0.9774033898178666)
        to
        (6.1766906409562035, 0.9943348002101371)
        to
        (6.283185307179586, 1.0);
\end{tikzpicture}

2.2 Straight line segments with line()

tf.line(start, end, ...) is a convenience wrapper for a two-point path.
The arrows parameter adds arrowheads.
[12]:
tf2 = tz.TikzFigure()

# Baseline
tf2.line((0, 0), (2 * np.pi, 0), color="gray", dash_pattern="on 3pt off 3pt")

# Arrow showing direction
tf2.line((0, -1.2), (0, 1.2), color="black", arrows="->", line_width=0.8)
tf2.line((-0.2, 0), (2 * np.pi + 0.2, 0), color="black", arrows="->", line_width=0.8)

# The curve
tf2.draw(sin_nodes, color="steelblue", line_width=1.5)

print(tf2.generate_tikz())
% --------------------------------------------- %
% Tikzfigure generated by tikzfigure v0.3.0     %
% https://github.com/max-models/tikzfigure      %
% --------------------------------------------- %
\begin{tikzpicture}
    \draw[color=gray, dash pattern=on 3pt off 3pt] ({0}, {0}) -- ({6.283185307179586}, {0});
    \draw[->, color=black, line width=0.8] ({0}, {-1.2}) -- ({0}, {1.2});
    \draw[->, color=black, line width=0.8] ({-0.2}, {0}) -- ({6.483185307179586}, {0});
    \draw[color=steelblue, line width=1.5] (0.0, 0.0)
        to
        (0.10649466622338281, 0.1062934856473654)
        to
        (0.21298933244676563, 0.21138262362962432)
        to
        (0.31948399867014843, 0.31407671202194876)
        to
        (0.42597866489353126, 0.41321218576837815)
        to
        (0.5324733311169141, 0.5076658003388399)
        to
        (0.6389679973402969, 0.5963673585385013)
        to
        (0.7454626635636797, 0.6783118362696161)
        to
        (0.8519573297870625, 0.7525707698561385)
        to
        (0.9584519960104453, 0.818302775908169)
        to
        (1.0649466622338282, 0.8747630845319613)
        to
        (1.171441328457211, 0.921311977870413)
        to
        (1.2779359946805937, 0.9574220383620055)
        to
        (1.3844306609039765, 0.9826841245925209)
        to
        (1.4909253271273595, 0.9968120070307501)
        to
        (1.5974199933507423, 0.9996456111234526)
        to
        (1.703914659574125, 0.9911528310040072)
        to
        (1.8104093257975078, 0.97142989326471)
        to
        (1.9169039920208906, 0.9407002666710333)
        to
        (2.0233986582442736, 0.8993121301712192)
        to
        (2.1298933244676563, 0.847734427889671)
        to
        (2.236387990691039, 0.7865515558026426)
        to
        (2.342882656914422, 0.7164567402983153)
        to
        (2.4493773231378047, 0.6382441836448203)
        to
        (2.5558719893611874, 0.5528000653611936)
        to
        (2.66236665558457, 0.4610925014493262)
        to
        (2.768861321807953, 0.3641605752528225)
        to
        (2.875355988031336, 0.26310256422752126)
        to
        (2.981850654254719, 0.1590634960190721)
        to
        (3.0883453204781017, 0.053222174842178754)
        to
        (3.1948399867014845, -0.053222174842178505)
        to
        (3.3013346529248673, -0.15906349601907185)
        to
        (3.40782931914825, -0.26310256422752104)
        to
        (3.514323985371633, -0.3641605752528219)
        to
        (3.6208186515950156, -0.46109250144932556)
        to
        (3.7273133178183984, -0.5528000653611931)
        to
        (3.833807984041781, -0.6382441836448197)
        to
        (3.940302650265164, -0.7164567402983147)
        to
        (4.046797316488547, -0.7865515558026425)
        to
        (4.15329198271193, -0.8477344278896709)
        to
        (4.259786648935313, -0.8993121301712191)
        to
        (4.366281315158695, -0.9407002666710332)
        to
        (4.472775981382078, -0.9714298932647099)
        to
        (4.579270647605461, -0.9911528310040071)
        to
        (4.685765313828844, -0.9996456111234525)
        to
        (4.7922599800522265, -0.9968120070307502)
        to
        (4.898754646275609, -0.9826841245925211)
        to
        (5.005249312498992, -0.9574220383620057)
        to
        (5.111743978722375, -0.9213119778704132)
        to
        (5.218238644945758, -0.8747630845319616)
        to
        (5.32473331116914, -0.8183027759081695)
        to
        (5.431227977392523, -0.752570769856139)
        to
        (5.537722643615906, -0.6783118362696167)
        to
        (5.644217309839289, -0.5963673585385021)
        to
        (5.750711976062672, -0.50766580033884)
        to
        (5.857206642286055, -0.4132121857683782)
        to
        (5.963701308509438, -0.3140767120219489)
        to
        (6.070195974732821, -0.21138262362962446)
        to
        (6.1766906409562035, -0.1062934856473656)
        to
        (6.283185307179586, -2.4492935982947064e-16);
\end{tikzpicture}

2.3 Rectangles, circles, and arcs

[13]:
tf3 = tz.TikzFigure()

# Bounding rectangle (coordinate space for context)
tf3.rectangle((0, -1.2), (2 * np.pi, 1.2), draw="gray!40", fill="gray!5")

# Circle at the origin
tf3.circle((0, 0), radius=0.15, fill="red!60", draw="red")

# Circle at peak of sine
tf3.circle((np.pi / 2, 1.0), radius=0.12, fill="steelblue", draw="none")

# Arc (quarter circle)
tf3.arc(
    (0.4, 0),
    start_angle=0,
    end_angle=90,
    radius=0.4,
    draw="green!60!black",
    line_width=1.0,
)

# The curve on top
tf3.draw(sin_nodes, color="steelblue", line_width=1.5)

print(tf3.generate_tikz())
% --------------------------------------------- %
% Tikzfigure generated by tikzfigure v0.3.0     %
% https://github.com/max-models/tikzfigure      %
% --------------------------------------------- %
\begin{tikzpicture}
    \draw[fill=gray!5, draw=gray!40] ({0}, {-1.2}) rectangle ({6.283185307179586}, {1.2});
    \draw[fill=red!60, draw=red] ({0}, {0}) circle (0.15);
    \draw[fill=steelblue, draw=none] ({1.5707963267948966}, {1.0}) circle (0.12);
    \draw[draw=green!60!black, line width=1.0, start angle=0, end angle=90, radius=0.4] ({0.4}, {0}) arc;
    \draw[color=steelblue, line width=1.5] (0.0, 0.0)
        to
        (0.10649466622338281, 0.1062934856473654)
        to
        (0.21298933244676563, 0.21138262362962432)
        to
        (0.31948399867014843, 0.31407671202194876)
        to
        (0.42597866489353126, 0.41321218576837815)
        to
        (0.5324733311169141, 0.5076658003388399)
        to
        (0.6389679973402969, 0.5963673585385013)
        to
        (0.7454626635636797, 0.6783118362696161)
        to
        (0.8519573297870625, 0.7525707698561385)
        to
        (0.9584519960104453, 0.818302775908169)
        to
        (1.0649466622338282, 0.8747630845319613)
        to
        (1.171441328457211, 0.921311977870413)
        to
        (1.2779359946805937, 0.9574220383620055)
        to
        (1.3844306609039765, 0.9826841245925209)
        to
        (1.4909253271273595, 0.9968120070307501)
        to
        (1.5974199933507423, 0.9996456111234526)
        to
        (1.703914659574125, 0.9911528310040072)
        to
        (1.8104093257975078, 0.97142989326471)
        to
        (1.9169039920208906, 0.9407002666710333)
        to
        (2.0233986582442736, 0.8993121301712192)
        to
        (2.1298933244676563, 0.847734427889671)
        to
        (2.236387990691039, 0.7865515558026426)
        to
        (2.342882656914422, 0.7164567402983153)
        to
        (2.4493773231378047, 0.6382441836448203)
        to
        (2.5558719893611874, 0.5528000653611936)
        to
        (2.66236665558457, 0.4610925014493262)
        to
        (2.768861321807953, 0.3641605752528225)
        to
        (2.875355988031336, 0.26310256422752126)
        to
        (2.981850654254719, 0.1590634960190721)
        to
        (3.0883453204781017, 0.053222174842178754)
        to
        (3.1948399867014845, -0.053222174842178505)
        to
        (3.3013346529248673, -0.15906349601907185)
        to
        (3.40782931914825, -0.26310256422752104)
        to
        (3.514323985371633, -0.3641605752528219)
        to
        (3.6208186515950156, -0.46109250144932556)
        to
        (3.7273133178183984, -0.5528000653611931)
        to
        (3.833807984041781, -0.6382441836448197)
        to
        (3.940302650265164, -0.7164567402983147)
        to
        (4.046797316488547, -0.7865515558026425)
        to
        (4.15329198271193, -0.8477344278896709)
        to
        (4.259786648935313, -0.8993121301712191)
        to
        (4.366281315158695, -0.9407002666710332)
        to
        (4.472775981382078, -0.9714298932647099)
        to
        (4.579270647605461, -0.9911528310040071)
        to
        (4.685765313828844, -0.9996456111234525)
        to
        (4.7922599800522265, -0.9968120070307502)
        to
        (4.898754646275609, -0.9826841245925211)
        to
        (5.005249312498992, -0.9574220383620057)
        to
        (5.111743978722375, -0.9213119778704132)
        to
        (5.218238644945758, -0.8747630845319616)
        to
        (5.32473331116914, -0.8183027759081695)
        to
        (5.431227977392523, -0.752570769856139)
        to
        (5.537722643615906, -0.6783118362696167)
        to
        (5.644217309839289, -0.5963673585385021)
        to
        (5.750711976062672, -0.50766580033884)
        to
        (5.857206642286055, -0.4132121857683782)
        to
        (5.963701308509438, -0.3140767120219489)
        to
        (6.070195974732821, -0.21138262362962446)
        to
        (6.1766906409562035, -0.1062934856473656)
        to
        (6.283185307179586, -2.4492935982947064e-16);
\end{tikzpicture}

2.4 Nodes — text labels and markers

add_node() places a text label (optionally inside a shape) at an (x, y) position.

[14]:
tf4 = tz.TikzFigure()
tf4.draw(sin_nodes, color="steelblue", line_width=1.5)

# Plain text label
tf4.add_node(np.pi / 2, 1.15, content=r"$\max$", color="steelblue")

# Boxed label
tf4.add_node(
    3 * np.pi / 2,
    -1.15,
    content=r"$\min$",
    shape="rectangle",
    fill="tomato!20",
    draw="tomato",
    inner_sep="2pt",
)

# Circle marker at zero-crossing
tf4.add_node(
    np.pi, 0, shape="circle", fill="white", draw="steelblue", minimum_size="0.18cm"
)

print(tf4.generate_tikz())
% --------------------------------------------- %
% Tikzfigure generated by tikzfigure v0.3.0     %
% https://github.com/max-models/tikzfigure      %
% --------------------------------------------- %
\begin{tikzpicture}
    \draw[color=steelblue, line width=1.5] (0.0, 0.0)
        to
        (0.10649466622338281, 0.1062934856473654)
        to
        (0.21298933244676563, 0.21138262362962432)
        to
        (0.31948399867014843, 0.31407671202194876)
        to
        (0.42597866489353126, 0.41321218576837815)
        to
        (0.5324733311169141, 0.5076658003388399)
        to
        (0.6389679973402969, 0.5963673585385013)
        to
        (0.7454626635636797, 0.6783118362696161)
        to
        (0.8519573297870625, 0.7525707698561385)
        to
        (0.9584519960104453, 0.818302775908169)
        to
        (1.0649466622338282, 0.8747630845319613)
        to
        (1.171441328457211, 0.921311977870413)
        to
        (1.2779359946805937, 0.9574220383620055)
        to
        (1.3844306609039765, 0.9826841245925209)
        to
        (1.4909253271273595, 0.9968120070307501)
        to
        (1.5974199933507423, 0.9996456111234526)
        to
        (1.703914659574125, 0.9911528310040072)
        to
        (1.8104093257975078, 0.97142989326471)
        to
        (1.9169039920208906, 0.9407002666710333)
        to
        (2.0233986582442736, 0.8993121301712192)
        to
        (2.1298933244676563, 0.847734427889671)
        to
        (2.236387990691039, 0.7865515558026426)
        to
        (2.342882656914422, 0.7164567402983153)
        to
        (2.4493773231378047, 0.6382441836448203)
        to
        (2.5558719893611874, 0.5528000653611936)
        to
        (2.66236665558457, 0.4610925014493262)
        to
        (2.768861321807953, 0.3641605752528225)
        to
        (2.875355988031336, 0.26310256422752126)
        to
        (2.981850654254719, 0.1590634960190721)
        to
        (3.0883453204781017, 0.053222174842178754)
        to
        (3.1948399867014845, -0.053222174842178505)
        to
        (3.3013346529248673, -0.15906349601907185)
        to
        (3.40782931914825, -0.26310256422752104)
        to
        (3.514323985371633, -0.3641605752528219)
        to
        (3.6208186515950156, -0.46109250144932556)
        to
        (3.7273133178183984, -0.5528000653611931)
        to
        (3.833807984041781, -0.6382441836448197)
        to
        (3.940302650265164, -0.7164567402983147)
        to
        (4.046797316488547, -0.7865515558026425)
        to
        (4.15329198271193, -0.8477344278896709)
        to
        (4.259786648935313, -0.8993121301712191)
        to
        (4.366281315158695, -0.9407002666710332)
        to
        (4.472775981382078, -0.9714298932647099)
        to
        (4.579270647605461, -0.9911528310040071)
        to
        (4.685765313828844, -0.9996456111234525)
        to
        (4.7922599800522265, -0.9968120070307502)
        to
        (4.898754646275609, -0.9826841245925211)
        to
        (5.005249312498992, -0.9574220383620057)
        to
        (5.111743978722375, -0.9213119778704132)
        to
        (5.218238644945758, -0.8747630845319616)
        to
        (5.32473331116914, -0.8183027759081695)
        to
        (5.431227977392523, -0.752570769856139)
        to
        (5.537722643615906, -0.6783118362696167)
        to
        (5.644217309839289, -0.5963673585385021)
        to
        (5.750711976062672, -0.50766580033884)
        to
        (5.857206642286055, -0.4132121857683782)
        to
        (5.963701308509438, -0.3140767120219489)
        to
        (6.070195974732821, -0.21138262362962446)
        to
        (6.1766906409562035, -0.1062934856473656)
        to
        (6.283185307179586, -2.4492935982947064e-16);
    \node[color=steelblue] (node0) at ({1.5707963267948966}, {1.15}) {$\max$};
    \node[shape=rectangle, fill=tomato!20, draw=tomato, inner sep=2pt] (node1) at ({4.71238898038469}, {-1.15}) {$\min$};
    \node[shape=circle, fill=white, draw=steelblue, minimum size=0.18cm] (node2) at ({3.141592653589793}, {0}) {};
\end{tikzpicture}

2.5 Custom colours with colorlet()

TikZ colour mixing syntax (blue!70!white) lets you define reusable named colours.

[15]:
tf5 = tz.TikzFigure()

# Define named colours
tf5.colorlet("myblue", "blue!70!white")
tf5.colorlet("myred", "red!80!black")
tf5.colorlet("myfill", "blue!10!white")

# Use them in draw calls
tf5.draw(sin_nodes, color="myblue", line_width=1.5)
tf5.draw(cos_nodes, color="myred", line_width=1.5)

# Filled polygon using the fill colour
closed_nodes = sin_nodes + [(float(x[-1]), 0.0), (float(x[0]), 0.0)]
tf5.draw(closed_nodes, fill="myfill", draw="none", cycle=True)

print(tf5.generate_tikz())
% --------------------------------------------- %
% Tikzfigure generated by tikzfigure v0.3.0     %
% https://github.com/max-models/tikzfigure      %
% --------------------------------------------- %
\begin{tikzpicture}
    \colorlet{myblue}{blue!70!white}
    \colorlet{myred}{red!80!black}
    \colorlet{myfill}{blue!10!white}
    \draw[color=myblue, line width=1.5] (0.0, 0.0)
        to
        (0.10649466622338281, 0.1062934856473654)
        to
        (0.21298933244676563, 0.21138262362962432)
        to
        (0.31948399867014843, 0.31407671202194876)
        to
        (0.42597866489353126, 0.41321218576837815)
        to
        (0.5324733311169141, 0.5076658003388399)
        to
        (0.6389679973402969, 0.5963673585385013)
        to
        (0.7454626635636797, 0.6783118362696161)
        to
        (0.8519573297870625, 0.7525707698561385)
        to
        (0.9584519960104453, 0.818302775908169)
        to
        (1.0649466622338282, 0.8747630845319613)
        to
        (1.171441328457211, 0.921311977870413)
        to
        (1.2779359946805937, 0.9574220383620055)
        to
        (1.3844306609039765, 0.9826841245925209)
        to
        (1.4909253271273595, 0.9968120070307501)
        to
        (1.5974199933507423, 0.9996456111234526)
        to
        (1.703914659574125, 0.9911528310040072)
        to
        (1.8104093257975078, 0.97142989326471)
        to
        (1.9169039920208906, 0.9407002666710333)
        to
        (2.0233986582442736, 0.8993121301712192)
        to
        (2.1298933244676563, 0.847734427889671)
        to
        (2.236387990691039, 0.7865515558026426)
        to
        (2.342882656914422, 0.7164567402983153)
        to
        (2.4493773231378047, 0.6382441836448203)
        to
        (2.5558719893611874, 0.5528000653611936)
        to
        (2.66236665558457, 0.4610925014493262)
        to
        (2.768861321807953, 0.3641605752528225)
        to
        (2.875355988031336, 0.26310256422752126)
        to
        (2.981850654254719, 0.1590634960190721)
        to
        (3.0883453204781017, 0.053222174842178754)
        to
        (3.1948399867014845, -0.053222174842178505)
        to
        (3.3013346529248673, -0.15906349601907185)
        to
        (3.40782931914825, -0.26310256422752104)
        to
        (3.514323985371633, -0.3641605752528219)
        to
        (3.6208186515950156, -0.46109250144932556)
        to
        (3.7273133178183984, -0.5528000653611931)
        to
        (3.833807984041781, -0.6382441836448197)
        to
        (3.940302650265164, -0.7164567402983147)
        to
        (4.046797316488547, -0.7865515558026425)
        to
        (4.15329198271193, -0.8477344278896709)
        to
        (4.259786648935313, -0.8993121301712191)
        to
        (4.366281315158695, -0.9407002666710332)
        to
        (4.472775981382078, -0.9714298932647099)
        to
        (4.579270647605461, -0.9911528310040071)
        to
        (4.685765313828844, -0.9996456111234525)
        to
        (4.7922599800522265, -0.9968120070307502)
        to
        (4.898754646275609, -0.9826841245925211)
        to
        (5.005249312498992, -0.9574220383620057)
        to
        (5.111743978722375, -0.9213119778704132)
        to
        (5.218238644945758, -0.8747630845319616)
        to
        (5.32473331116914, -0.8183027759081695)
        to
        (5.431227977392523, -0.752570769856139)
        to
        (5.537722643615906, -0.6783118362696167)
        to
        (5.644217309839289, -0.5963673585385021)
        to
        (5.750711976062672, -0.50766580033884)
        to
        (5.857206642286055, -0.4132121857683782)
        to
        (5.963701308509438, -0.3140767120219489)
        to
        (6.070195974732821, -0.21138262362962446)
        to
        (6.1766906409562035, -0.1062934856473656)
        to
        (6.283185307179586, -2.4492935982947064e-16);
    \draw[color=myred, line width=1.5] (0.0, 1.0)
        to
        (0.10649466622338281, 0.9943348002101371)
        to
        (0.21298933244676563, 0.9774033898178667)
        to
        (0.31948399867014843, 0.9493976084683813)
        to
        (0.42597866489353126, 0.9106347728549132)
        to
        (0.5324733311169141, 0.8615540813938061)
        to
        (0.6389679973402969, 0.8027116379309637)
        to
        (0.7454626635636797, 0.7347741508630673)
        to
        (0.8519573297870625, 0.6585113790650386)
        to
        (0.9584519960104453, 0.5747874102144069)
        to
        (1.0649466622338282, 0.4845508703326502)
        to
        (1.171441328457211, 0.38882417547332077)
        to
        (1.2779359946805937, 0.28869194733962117)
        to
        (1.3844306609039765, 0.18528872408711453)
        to
        (1.4909253271273595, 0.0797861055530831)
        to
        (1.5974199933507423, -0.026620521437774693)
        to
        (1.703914659574125, -0.13272552728372186)
        to
        (1.8104093257975078, -0.23732669987111465)
        to
        (1.9169039920208906, -0.33923886611803006)
        to
        (2.0233986582442736, -0.43730732045885534)
        to
        (2.1298933244676563, -0.5304209081197424)
        to
        (2.236387990691039, -0.6175246149461918)
        to
        (2.342882656914422, -0.6976315211349846)
        to
        (2.4493773231378047, -0.769833983429906)
        to
        (2.5558719893611874, -0.8333139190825148)
        to
        (2.66236665558457, -0.8873520750565714)
        to
        (2.768861321807953, -0.9313361774523383)
        to
        (2.875355988031336, -0.9647678688145159)
        to
        (2.981850654254719, -0.9872683547213446)
        to
        (3.0883453204781017, -0.9985826956767619)
        to
        (3.1948399867014845, -0.9985826956767619)
        to
        (3.3013346529248673, -0.9872683547213446)
        to
        (3.40782931914825, -0.964767868814516)
        to
        (3.514323985371633, -0.9313361774523385)
        to
        (3.6208186515950156, -0.8873520750565718)
        to
        (3.7273133178183984, -0.8333139190825152)
        to
        (3.833807984041781, -0.7698339834299065)
        to
        (3.940302650265164, -0.697631521134985)
        to
        (4.046797316488547, -0.617524614946192)
        to
        (4.15329198271193, -0.5304209081197426)
        to
        (4.259786648935313, -0.43730732045885556)
        to
        (4.366281315158695, -0.3392388661180305)
        to
        (4.472775981382078, -0.2373266998711151)
        to
        (4.579270647605461, -0.1327255272837223)
        to
        (4.685765313828844, -0.02662052143777516)
        to
        (4.7922599800522265, 0.07978610555308265)
        to
        (4.898754646275609, 0.18528872408711383)
        to
        (5.005249312498992, 0.2886919473396205)
        to
        (5.111743978722375, 0.3888241754733201)
        to
        (5.218238644945758, 0.4845508703326496)
        to
        (5.32473331116914, 0.5747874102144063)
        to
        (5.431227977392523, 0.6585113790650381)
        to
        (5.537722643615906, 0.7347741508630667)
        to
        (5.644217309839289, 0.8027116379309631)
        to
        (5.750711976062672, 0.8615540813938061)
        to
        (5.857206642286055, 0.9106347728549131)
        to
        (5.963701308509438, 0.9493976084683813)
        to
        (6.070195974732821, 0.9774033898178666)
        to
        (6.1766906409562035, 0.9943348002101371)
        to
        (6.283185307179586, 1.0);
    \draw[fill=myfill, draw=none] (0.0, 0.0)
        to
        (0.10649466622338281, 0.1062934856473654)
        to
        (0.21298933244676563, 0.21138262362962432)
        to
        (0.31948399867014843, 0.31407671202194876)
        to
        (0.42597866489353126, 0.41321218576837815)
        to
        (0.5324733311169141, 0.5076658003388399)
        to
        (0.6389679973402969, 0.5963673585385013)
        to
        (0.7454626635636797, 0.6783118362696161)
        to
        (0.8519573297870625, 0.7525707698561385)
        to
        (0.9584519960104453, 0.818302775908169)
        to
        (1.0649466622338282, 0.8747630845319613)
        to
        (1.171441328457211, 0.921311977870413)
        to
        (1.2779359946805937, 0.9574220383620055)
        to
        (1.3844306609039765, 0.9826841245925209)
        to
        (1.4909253271273595, 0.9968120070307501)
        to
        (1.5974199933507423, 0.9996456111234526)
        to
        (1.703914659574125, 0.9911528310040072)
        to
        (1.8104093257975078, 0.97142989326471)
        to
        (1.9169039920208906, 0.9407002666710333)
        to
        (2.0233986582442736, 0.8993121301712192)
        to
        (2.1298933244676563, 0.847734427889671)
        to
        (2.236387990691039, 0.7865515558026426)
        to
        (2.342882656914422, 0.7164567402983153)
        to
        (2.4493773231378047, 0.6382441836448203)
        to
        (2.5558719893611874, 0.5528000653611936)
        to
        (2.66236665558457, 0.4610925014493262)
        to
        (2.768861321807953, 0.3641605752528225)
        to
        (2.875355988031336, 0.26310256422752126)
        to
        (2.981850654254719, 0.1590634960190721)
        to
        (3.0883453204781017, 0.053222174842178754)
        to
        (3.1948399867014845, -0.053222174842178505)
        to
        (3.3013346529248673, -0.15906349601907185)
        to
        (3.40782931914825, -0.26310256422752104)
        to
        (3.514323985371633, -0.3641605752528219)
        to
        (3.6208186515950156, -0.46109250144932556)
        to
        (3.7273133178183984, -0.5528000653611931)
        to
        (3.833807984041781, -0.6382441836448197)
        to
        (3.940302650265164, -0.7164567402983147)
        to
        (4.046797316488547, -0.7865515558026425)
        to
        (4.15329198271193, -0.8477344278896709)
        to
        (4.259786648935313, -0.8993121301712191)
        to
        (4.366281315158695, -0.9407002666710332)
        to
        (4.472775981382078, -0.9714298932647099)
        to
        (4.579270647605461, -0.9911528310040071)
        to
        (4.685765313828844, -0.9996456111234525)
        to
        (4.7922599800522265, -0.9968120070307502)
        to
        (4.898754646275609, -0.9826841245925211)
        to
        (5.005249312498992, -0.9574220383620057)
        to
        (5.111743978722375, -0.9213119778704132)
        to
        (5.218238644945758, -0.8747630845319616)
        to
        (5.32473331116914, -0.8183027759081695)
        to
        (5.431227977392523, -0.752570769856139)
        to
        (5.537722643615906, -0.6783118362696167)
        to
        (5.644217309839289, -0.5963673585385021)
        to
        (5.750711976062672, -0.50766580033884)
        to
        (5.857206642286055, -0.4132121857683782)
        to
        (5.963701308509438, -0.3140767120219489)
        to
        (6.070195974732821, -0.21138262362962446)
        to
        (6.1766906409562035, -0.1062934856473656)
        to
        (6.283185307179586, -2.4492935982947064e-16)
        to
        (6.283185307179586, 0.0)
        to
        (0.0, 0.0) -- cycle;
\end{tikzpicture}

2.6 Filled paths and patterns

Pass fill= and/or pattern= to draw() to create shaded regions.

[16]:
tf6 = tz.TikzFigure()

# Shaded area under sin curve (closed path)
area_nodes = sin_nodes + [(float(x[-1]), 0.0), (float(x[0]), 0.0)]
tf6.draw(area_nodes, fill="steelblue!20", draw="none", cycle=True)

# Hatched region using a pattern
cos_area = cos_nodes + [(float(x[-1]), 0.0), (float(x[0]), 0.0)]
tf6.draw(
    cos_area,
    pattern="north east lines",
    pattern_color="tomato",
    draw="none",
    cycle=True,
)

# Curves on top
tf6.draw(sin_nodes, color="steelblue", line_width=1.5)
tf6.draw(cos_nodes, color="tomato", line_width=1.2)

print(tf6.generate_tikz())
% --------------------------------------------- %
% Tikzfigure generated by tikzfigure v0.3.0     %
% https://github.com/max-models/tikzfigure      %
% --------------------------------------------- %
\begin{tikzpicture}
    \draw[fill=steelblue!20, draw=none] (0.0, 0.0)
        to
        (0.10649466622338281, 0.1062934856473654)
        to
        (0.21298933244676563, 0.21138262362962432)
        to
        (0.31948399867014843, 0.31407671202194876)
        to
        (0.42597866489353126, 0.41321218576837815)
        to
        (0.5324733311169141, 0.5076658003388399)
        to
        (0.6389679973402969, 0.5963673585385013)
        to
        (0.7454626635636797, 0.6783118362696161)
        to
        (0.8519573297870625, 0.7525707698561385)
        to
        (0.9584519960104453, 0.818302775908169)
        to
        (1.0649466622338282, 0.8747630845319613)
        to
        (1.171441328457211, 0.921311977870413)
        to
        (1.2779359946805937, 0.9574220383620055)
        to
        (1.3844306609039765, 0.9826841245925209)
        to
        (1.4909253271273595, 0.9968120070307501)
        to
        (1.5974199933507423, 0.9996456111234526)
        to
        (1.703914659574125, 0.9911528310040072)
        to
        (1.8104093257975078, 0.97142989326471)
        to
        (1.9169039920208906, 0.9407002666710333)
        to
        (2.0233986582442736, 0.8993121301712192)
        to
        (2.1298933244676563, 0.847734427889671)
        to
        (2.236387990691039, 0.7865515558026426)
        to
        (2.342882656914422, 0.7164567402983153)
        to
        (2.4493773231378047, 0.6382441836448203)
        to
        (2.5558719893611874, 0.5528000653611936)
        to
        (2.66236665558457, 0.4610925014493262)
        to
        (2.768861321807953, 0.3641605752528225)
        to
        (2.875355988031336, 0.26310256422752126)
        to
        (2.981850654254719, 0.1590634960190721)
        to
        (3.0883453204781017, 0.053222174842178754)
        to
        (3.1948399867014845, -0.053222174842178505)
        to
        (3.3013346529248673, -0.15906349601907185)
        to
        (3.40782931914825, -0.26310256422752104)
        to
        (3.514323985371633, -0.3641605752528219)
        to
        (3.6208186515950156, -0.46109250144932556)
        to
        (3.7273133178183984, -0.5528000653611931)
        to
        (3.833807984041781, -0.6382441836448197)
        to
        (3.940302650265164, -0.7164567402983147)
        to
        (4.046797316488547, -0.7865515558026425)
        to
        (4.15329198271193, -0.8477344278896709)
        to
        (4.259786648935313, -0.8993121301712191)
        to
        (4.366281315158695, -0.9407002666710332)
        to
        (4.472775981382078, -0.9714298932647099)
        to
        (4.579270647605461, -0.9911528310040071)
        to
        (4.685765313828844, -0.9996456111234525)
        to
        (4.7922599800522265, -0.9968120070307502)
        to
        (4.898754646275609, -0.9826841245925211)
        to
        (5.005249312498992, -0.9574220383620057)
        to
        (5.111743978722375, -0.9213119778704132)
        to
        (5.218238644945758, -0.8747630845319616)
        to
        (5.32473331116914, -0.8183027759081695)
        to
        (5.431227977392523, -0.752570769856139)
        to
        (5.537722643615906, -0.6783118362696167)
        to
        (5.644217309839289, -0.5963673585385021)
        to
        (5.750711976062672, -0.50766580033884)
        to
        (5.857206642286055, -0.4132121857683782)
        to
        (5.963701308509438, -0.3140767120219489)
        to
        (6.070195974732821, -0.21138262362962446)
        to
        (6.1766906409562035, -0.1062934856473656)
        to
        (6.283185307179586, -2.4492935982947064e-16)
        to
        (6.283185307179586, 0.0)
        to
        (0.0, 0.0) -- cycle;
    \draw[draw=none, pattern=north east lines, pattern color=tomato] (0.0, 1.0)
        to
        (0.10649466622338281, 0.9943348002101371)
        to
        (0.21298933244676563, 0.9774033898178667)
        to
        (0.31948399867014843, 0.9493976084683813)
        to
        (0.42597866489353126, 0.9106347728549132)
        to
        (0.5324733311169141, 0.8615540813938061)
        to
        (0.6389679973402969, 0.8027116379309637)
        to
        (0.7454626635636797, 0.7347741508630673)
        to
        (0.8519573297870625, 0.6585113790650386)
        to
        (0.9584519960104453, 0.5747874102144069)
        to
        (1.0649466622338282, 0.4845508703326502)
        to
        (1.171441328457211, 0.38882417547332077)
        to
        (1.2779359946805937, 0.28869194733962117)
        to
        (1.3844306609039765, 0.18528872408711453)
        to
        (1.4909253271273595, 0.0797861055530831)
        to
        (1.5974199933507423, -0.026620521437774693)
        to
        (1.703914659574125, -0.13272552728372186)
        to
        (1.8104093257975078, -0.23732669987111465)
        to
        (1.9169039920208906, -0.33923886611803006)
        to
        (2.0233986582442736, -0.43730732045885534)
        to
        (2.1298933244676563, -0.5304209081197424)
        to
        (2.236387990691039, -0.6175246149461918)
        to
        (2.342882656914422, -0.6976315211349846)
        to
        (2.4493773231378047, -0.769833983429906)
        to
        (2.5558719893611874, -0.8333139190825148)
        to
        (2.66236665558457, -0.8873520750565714)
        to
        (2.768861321807953, -0.9313361774523383)
        to
        (2.875355988031336, -0.9647678688145159)
        to
        (2.981850654254719, -0.9872683547213446)
        to
        (3.0883453204781017, -0.9985826956767619)
        to
        (3.1948399867014845, -0.9985826956767619)
        to
        (3.3013346529248673, -0.9872683547213446)
        to
        (3.40782931914825, -0.964767868814516)
        to
        (3.514323985371633, -0.9313361774523385)
        to
        (3.6208186515950156, -0.8873520750565718)
        to
        (3.7273133178183984, -0.8333139190825152)
        to
        (3.833807984041781, -0.7698339834299065)
        to
        (3.940302650265164, -0.697631521134985)
        to
        (4.046797316488547, -0.617524614946192)
        to
        (4.15329198271193, -0.5304209081197426)
        to
        (4.259786648935313, -0.43730732045885556)
        to
        (4.366281315158695, -0.3392388661180305)
        to
        (4.472775981382078, -0.2373266998711151)
        to
        (4.579270647605461, -0.1327255272837223)
        to
        (4.685765313828844, -0.02662052143777516)
        to
        (4.7922599800522265, 0.07978610555308265)
        to
        (4.898754646275609, 0.18528872408711383)
        to
        (5.005249312498992, 0.2886919473396205)
        to
        (5.111743978722375, 0.3888241754733201)
        to
        (5.218238644945758, 0.4845508703326496)
        to
        (5.32473331116914, 0.5747874102144063)
        to
        (5.431227977392523, 0.6585113790650381)
        to
        (5.537722643615906, 0.7347741508630667)
        to
        (5.644217309839289, 0.8027116379309631)
        to
        (5.750711976062672, 0.8615540813938061)
        to
        (5.857206642286055, 0.9106347728549131)
        to
        (5.963701308509438, 0.9493976084683813)
        to
        (6.070195974732821, 0.9774033898178666)
        to
        (6.1766906409562035, 0.9943348002101371)
        to
        (6.283185307179586, 1.0)
        to
        (6.283185307179586, 0.0)
        to
        (0.0, 0.0) -- cycle;
    \draw[color=steelblue, line width=1.5] (0.0, 0.0)
        to
        (0.10649466622338281, 0.1062934856473654)
        to
        (0.21298933244676563, 0.21138262362962432)
        to
        (0.31948399867014843, 0.31407671202194876)
        to
        (0.42597866489353126, 0.41321218576837815)
        to
        (0.5324733311169141, 0.5076658003388399)
        to
        (0.6389679973402969, 0.5963673585385013)
        to
        (0.7454626635636797, 0.6783118362696161)
        to
        (0.8519573297870625, 0.7525707698561385)
        to
        (0.9584519960104453, 0.818302775908169)
        to
        (1.0649466622338282, 0.8747630845319613)
        to
        (1.171441328457211, 0.921311977870413)
        to
        (1.2779359946805937, 0.9574220383620055)
        to
        (1.3844306609039765, 0.9826841245925209)
        to
        (1.4909253271273595, 0.9968120070307501)
        to
        (1.5974199933507423, 0.9996456111234526)
        to
        (1.703914659574125, 0.9911528310040072)
        to
        (1.8104093257975078, 0.97142989326471)
        to
        (1.9169039920208906, 0.9407002666710333)
        to
        (2.0233986582442736, 0.8993121301712192)
        to
        (2.1298933244676563, 0.847734427889671)
        to
        (2.236387990691039, 0.7865515558026426)
        to
        (2.342882656914422, 0.7164567402983153)
        to
        (2.4493773231378047, 0.6382441836448203)
        to
        (2.5558719893611874, 0.5528000653611936)
        to
        (2.66236665558457, 0.4610925014493262)
        to
        (2.768861321807953, 0.3641605752528225)
        to
        (2.875355988031336, 0.26310256422752126)
        to
        (2.981850654254719, 0.1590634960190721)
        to
        (3.0883453204781017, 0.053222174842178754)
        to
        (3.1948399867014845, -0.053222174842178505)
        to
        (3.3013346529248673, -0.15906349601907185)
        to
        (3.40782931914825, -0.26310256422752104)
        to
        (3.514323985371633, -0.3641605752528219)
        to
        (3.6208186515950156, -0.46109250144932556)
        to
        (3.7273133178183984, -0.5528000653611931)
        to
        (3.833807984041781, -0.6382441836448197)
        to
        (3.940302650265164, -0.7164567402983147)
        to
        (4.046797316488547, -0.7865515558026425)
        to
        (4.15329198271193, -0.8477344278896709)
        to
        (4.259786648935313, -0.8993121301712191)
        to
        (4.366281315158695, -0.9407002666710332)
        to
        (4.472775981382078, -0.9714298932647099)
        to
        (4.579270647605461, -0.9911528310040071)
        to
        (4.685765313828844, -0.9996456111234525)
        to
        (4.7922599800522265, -0.9968120070307502)
        to
        (4.898754646275609, -0.9826841245925211)
        to
        (5.005249312498992, -0.9574220383620057)
        to
        (5.111743978722375, -0.9213119778704132)
        to
        (5.218238644945758, -0.8747630845319616)
        to
        (5.32473331116914, -0.8183027759081695)
        to
        (5.431227977392523, -0.752570769856139)
        to
        (5.537722643615906, -0.6783118362696167)
        to
        (5.644217309839289, -0.5963673585385021)
        to
        (5.750711976062672, -0.50766580033884)
        to
        (5.857206642286055, -0.4132121857683782)
        to
        (5.963701308509438, -0.3140767120219489)
        to
        (6.070195974732821, -0.21138262362962446)
        to
        (6.1766906409562035, -0.1062934856473656)
        to
        (6.283185307179586, -2.4492935982947064e-16);
    \draw[color=tomato, line width=1.2] (0.0, 1.0)
        to
        (0.10649466622338281, 0.9943348002101371)
        to
        (0.21298933244676563, 0.9774033898178667)
        to
        (0.31948399867014843, 0.9493976084683813)
        to
        (0.42597866489353126, 0.9106347728549132)
        to
        (0.5324733311169141, 0.8615540813938061)
        to
        (0.6389679973402969, 0.8027116379309637)
        to
        (0.7454626635636797, 0.7347741508630673)
        to
        (0.8519573297870625, 0.6585113790650386)
        to
        (0.9584519960104453, 0.5747874102144069)
        to
        (1.0649466622338282, 0.4845508703326502)
        to
        (1.171441328457211, 0.38882417547332077)
        to
        (1.2779359946805937, 0.28869194733962117)
        to
        (1.3844306609039765, 0.18528872408711453)
        to
        (1.4909253271273595, 0.0797861055530831)
        to
        (1.5974199933507423, -0.026620521437774693)
        to
        (1.703914659574125, -0.13272552728372186)
        to
        (1.8104093257975078, -0.23732669987111465)
        to
        (1.9169039920208906, -0.33923886611803006)
        to
        (2.0233986582442736, -0.43730732045885534)
        to
        (2.1298933244676563, -0.5304209081197424)
        to
        (2.236387990691039, -0.6175246149461918)
        to
        (2.342882656914422, -0.6976315211349846)
        to
        (2.4493773231378047, -0.769833983429906)
        to
        (2.5558719893611874, -0.8333139190825148)
        to
        (2.66236665558457, -0.8873520750565714)
        to
        (2.768861321807953, -0.9313361774523383)
        to
        (2.875355988031336, -0.9647678688145159)
        to
        (2.981850654254719, -0.9872683547213446)
        to
        (3.0883453204781017, -0.9985826956767619)
        to
        (3.1948399867014845, -0.9985826956767619)
        to
        (3.3013346529248673, -0.9872683547213446)
        to
        (3.40782931914825, -0.964767868814516)
        to
        (3.514323985371633, -0.9313361774523385)
        to
        (3.6208186515950156, -0.8873520750565718)
        to
        (3.7273133178183984, -0.8333139190825152)
        to
        (3.833807984041781, -0.7698339834299065)
        to
        (3.940302650265164, -0.697631521134985)
        to
        (4.046797316488547, -0.617524614946192)
        to
        (4.15329198271193, -0.5304209081197426)
        to
        (4.259786648935313, -0.43730732045885556)
        to
        (4.366281315158695, -0.3392388661180305)
        to
        (4.472775981382078, -0.2373266998711151)
        to
        (4.579270647605461, -0.1327255272837223)
        to
        (4.685765313828844, -0.02662052143777516)
        to
        (4.7922599800522265, 0.07978610555308265)
        to
        (4.898754646275609, 0.18528872408711383)
        to
        (5.005249312498992, 0.2886919473396205)
        to
        (5.111743978722375, 0.3888241754733201)
        to
        (5.218238644945758, 0.4845508703326496)
        to
        (5.32473331116914, 0.5747874102144063)
        to
        (5.431227977392523, 0.6585113790650381)
        to
        (5.537722643615906, 0.7347741508630667)
        to
        (5.644217309839289, 0.8027116379309631)
        to
        (5.750711976062672, 0.8615540813938061)
        to
        (5.857206642286055, 0.9106347728549131)
        to
        (5.963701308509438, 0.9493976084683813)
        to
        (6.070195974732821, 0.9774033898178666)
        to
        (6.1766906409562035, 0.9943348002101371)
        to
        (6.283185307179586, 1.0);
\end{tikzpicture}

2.7 Layers in TikzFigure

The layer= parameter on every drawing call controls render order. Lower-numbered layers are drawn first (behind), higher layers on top.

[17]:
tf7 = tz.TikzFigure()

# layer 0: background fill (drawn first)
tf7.rectangle((0, -1.2), (2 * np.pi, 1.2), fill="gray!8", draw="gray!30", layer=0)

# layer 1: shaded area
area = sin_nodes + [(float(x[-1]), 0), (float(x[0]), 0)]
tf7.draw(area, fill="steelblue!25", draw="none", cycle=True, layer=1)

# layer 2: the curve (drawn last, on top)
tf7.draw(sin_nodes, color="steelblue", line_width=2.0, layer=2)
tf7.add_node(np.pi / 2, 1.15, content=r"$\sin(x)$", color="steelblue", layer=2)

print(tf7.generate_tikz())
% --------------------------------------------- %
% Tikzfigure generated by tikzfigure v0.3.0     %
% https://github.com/max-models/tikzfigure      %
% --------------------------------------------- %
\begin{tikzpicture}
    % Define the layers library
    \pgfdeclarelayer{0}
    \pgfdeclarelayer{1}
    \pgfdeclarelayer{2}
    \pgfsetlayers{0,1,2}

    % Layer 0
    \begin{pgfonlayer}{0}
        \draw[fill=gray!8, draw=gray!30] ({0}, {-1.2}) rectangle ({6.283185307179586}, {1.2});
    \end{pgfonlayer}{0}

    % Layer 1
    \begin{pgfonlayer}{1}
        \draw[fill=steelblue!25, draw=none] (0.0, 0.0)
            to
            (0.10649466622338281, 0.1062934856473654)
            to
            (0.21298933244676563, 0.21138262362962432)
            to
            (0.31948399867014843, 0.31407671202194876)
            to
            (0.42597866489353126, 0.41321218576837815)
            to
            (0.5324733311169141, 0.5076658003388399)
            to
            (0.6389679973402969, 0.5963673585385013)
            to
            (0.7454626635636797, 0.6783118362696161)
            to
            (0.8519573297870625, 0.7525707698561385)
            to
            (0.9584519960104453, 0.818302775908169)
            to
            (1.0649466622338282, 0.8747630845319613)
            to
            (1.171441328457211, 0.921311977870413)
            to
            (1.2779359946805937, 0.9574220383620055)
            to
            (1.3844306609039765, 0.9826841245925209)
            to
            (1.4909253271273595, 0.9968120070307501)
            to
            (1.5974199933507423, 0.9996456111234526)
            to
            (1.703914659574125, 0.9911528310040072)
            to
            (1.8104093257975078, 0.97142989326471)
            to
            (1.9169039920208906, 0.9407002666710333)
            to
            (2.0233986582442736, 0.8993121301712192)
            to
            (2.1298933244676563, 0.847734427889671)
            to
            (2.236387990691039, 0.7865515558026426)
            to
            (2.342882656914422, 0.7164567402983153)
            to
            (2.4493773231378047, 0.6382441836448203)
            to
            (2.5558719893611874, 0.5528000653611936)
            to
            (2.66236665558457, 0.4610925014493262)
            to
            (2.768861321807953, 0.3641605752528225)
            to
            (2.875355988031336, 0.26310256422752126)
            to
            (2.981850654254719, 0.1590634960190721)
            to
            (3.0883453204781017, 0.053222174842178754)
            to
            (3.1948399867014845, -0.053222174842178505)
            to
            (3.3013346529248673, -0.15906349601907185)
            to
            (3.40782931914825, -0.26310256422752104)
            to
            (3.514323985371633, -0.3641605752528219)
            to
            (3.6208186515950156, -0.46109250144932556)
            to
            (3.7273133178183984, -0.5528000653611931)
            to
            (3.833807984041781, -0.6382441836448197)
            to
            (3.940302650265164, -0.7164567402983147)
            to
            (4.046797316488547, -0.7865515558026425)
            to
            (4.15329198271193, -0.8477344278896709)
            to
            (4.259786648935313, -0.8993121301712191)
            to
            (4.366281315158695, -0.9407002666710332)
            to
            (4.472775981382078, -0.9714298932647099)
            to
            (4.579270647605461, -0.9911528310040071)
            to
            (4.685765313828844, -0.9996456111234525)
            to
            (4.7922599800522265, -0.9968120070307502)
            to
            (4.898754646275609, -0.9826841245925211)
            to
            (5.005249312498992, -0.9574220383620057)
            to
            (5.111743978722375, -0.9213119778704132)
            to
            (5.218238644945758, -0.8747630845319616)
            to
            (5.32473331116914, -0.8183027759081695)
            to
            (5.431227977392523, -0.752570769856139)
            to
            (5.537722643615906, -0.6783118362696167)
            to
            (5.644217309839289, -0.5963673585385021)
            to
            (5.750711976062672, -0.50766580033884)
            to
            (5.857206642286055, -0.4132121857683782)
            to
            (5.963701308509438, -0.3140767120219489)
            to
            (6.070195974732821, -0.21138262362962446)
            to
            (6.1766906409562035, -0.1062934856473656)
            to
            (6.283185307179586, -2.4492935982947064e-16)
            to
            (6.283185307179586, 0)
            to
            (0.0, 0) -- cycle;
    \end{pgfonlayer}{1}

    % Layer 2
    \begin{pgfonlayer}{2}
        \draw[color=steelblue, line width=2.0] (0.0, 0.0)
            to
            (0.10649466622338281, 0.1062934856473654)
            to
            (0.21298933244676563, 0.21138262362962432)
            to
            (0.31948399867014843, 0.31407671202194876)
            to
            (0.42597866489353126, 0.41321218576837815)
            to
            (0.5324733311169141, 0.5076658003388399)
            to
            (0.6389679973402969, 0.5963673585385013)
            to
            (0.7454626635636797, 0.6783118362696161)
            to
            (0.8519573297870625, 0.7525707698561385)
            to
            (0.9584519960104453, 0.818302775908169)
            to
            (1.0649466622338282, 0.8747630845319613)
            to
            (1.171441328457211, 0.921311977870413)
            to
            (1.2779359946805937, 0.9574220383620055)
            to
            (1.3844306609039765, 0.9826841245925209)
            to
            (1.4909253271273595, 0.9968120070307501)
            to
            (1.5974199933507423, 0.9996456111234526)
            to
            (1.703914659574125, 0.9911528310040072)
            to
            (1.8104093257975078, 0.97142989326471)
            to
            (1.9169039920208906, 0.9407002666710333)
            to
            (2.0233986582442736, 0.8993121301712192)
            to
            (2.1298933244676563, 0.847734427889671)
            to
            (2.236387990691039, 0.7865515558026426)
            to
            (2.342882656914422, 0.7164567402983153)
            to
            (2.4493773231378047, 0.6382441836448203)
            to
            (2.5558719893611874, 0.5528000653611936)
            to
            (2.66236665558457, 0.4610925014493262)
            to
            (2.768861321807953, 0.3641605752528225)
            to
            (2.875355988031336, 0.26310256422752126)
            to
            (2.981850654254719, 0.1590634960190721)
            to
            (3.0883453204781017, 0.053222174842178754)
            to
            (3.1948399867014845, -0.053222174842178505)
            to
            (3.3013346529248673, -0.15906349601907185)
            to
            (3.40782931914825, -0.26310256422752104)
            to
            (3.514323985371633, -0.3641605752528219)
            to
            (3.6208186515950156, -0.46109250144932556)
            to
            (3.7273133178183984, -0.5528000653611931)
            to
            (3.833807984041781, -0.6382441836448197)
            to
            (3.940302650265164, -0.7164567402983147)
            to
            (4.046797316488547, -0.7865515558026425)
            to
            (4.15329198271193, -0.8477344278896709)
            to
            (4.259786648935313, -0.8993121301712191)
            to
            (4.366281315158695, -0.9407002666710332)
            to
            (4.472775981382078, -0.9714298932647099)
            to
            (4.579270647605461, -0.9911528310040071)
            to
            (4.685765313828844, -0.9996456111234525)
            to
            (4.7922599800522265, -0.9968120070307502)
            to
            (4.898754646275609, -0.9826841245925211)
            to
            (5.005249312498992, -0.9574220383620057)
            to
            (5.111743978722375, -0.9213119778704132)
            to
            (5.218238644945758, -0.8747630845319616)
            to
            (5.32473331116914, -0.8183027759081695)
            to
            (5.431227977392523, -0.752570769856139)
            to
            (5.537722643615906, -0.6783118362696167)
            to
            (5.644217309839289, -0.5963673585385021)
            to
            (5.750711976062672, -0.50766580033884)
            to
            (5.857206642286055, -0.4132121857683782)
            to
            (5.963701308509438, -0.3140767120219489)
            to
            (6.070195974732821, -0.21138262362962446)
            to
            (6.1766906409562035, -0.1062934856473656)
            to
            (6.283185307179586, -2.4492935982947064e-16);
        \node[color=steelblue] (node0) at ({1.5707963267948966}, {1.15}) {$\sin(x)$};
    \end{pgfonlayer}{2}
\end{tikzpicture}

2.8 Escaping to raw TikZ code

For anything not yet covered by the API, use add_raw() to inject verbatim TikZ.

[18]:
tf8 = tz.TikzFigure()
tf8.draw(sin_nodes, color="steelblue", line_width=1.5)

# Inject custom TikZ — a dashed grid line
tf8.add_raw(r"\draw[gray!40, dashed] (0, 0) -- (6.28, 0);")

# Annotation with arrow using raw TikZ
tf8.add_raw(
    r"\draw[->, gray] (1.0, 0.6) -- (1.57, 1.0) node[right, font=\small] {peak};"
)

print(tf8.generate_tikz())
% --------------------------------------------- %
% Tikzfigure generated by tikzfigure v0.3.0     %
% https://github.com/max-models/tikzfigure      %
% --------------------------------------------- %
\begin{tikzpicture}
    \draw[color=steelblue, line width=1.5] (0.0, 0.0)
        to
        (0.10649466622338281, 0.1062934856473654)
        to
        (0.21298933244676563, 0.21138262362962432)
        to
        (0.31948399867014843, 0.31407671202194876)
        to
        (0.42597866489353126, 0.41321218576837815)
        to
        (0.5324733311169141, 0.5076658003388399)
        to
        (0.6389679973402969, 0.5963673585385013)
        to
        (0.7454626635636797, 0.6783118362696161)
        to
        (0.8519573297870625, 0.7525707698561385)
        to
        (0.9584519960104453, 0.818302775908169)
        to
        (1.0649466622338282, 0.8747630845319613)
        to
        (1.171441328457211, 0.921311977870413)
        to
        (1.2779359946805937, 0.9574220383620055)
        to
        (1.3844306609039765, 0.9826841245925209)
        to
        (1.4909253271273595, 0.9968120070307501)
        to
        (1.5974199933507423, 0.9996456111234526)
        to
        (1.703914659574125, 0.9911528310040072)
        to
        (1.8104093257975078, 0.97142989326471)
        to
        (1.9169039920208906, 0.9407002666710333)
        to
        (2.0233986582442736, 0.8993121301712192)
        to
        (2.1298933244676563, 0.847734427889671)
        to
        (2.236387990691039, 0.7865515558026426)
        to
        (2.342882656914422, 0.7164567402983153)
        to
        (2.4493773231378047, 0.6382441836448203)
        to
        (2.5558719893611874, 0.5528000653611936)
        to
        (2.66236665558457, 0.4610925014493262)
        to
        (2.768861321807953, 0.3641605752528225)
        to
        (2.875355988031336, 0.26310256422752126)
        to
        (2.981850654254719, 0.1590634960190721)
        to
        (3.0883453204781017, 0.053222174842178754)
        to
        (3.1948399867014845, -0.053222174842178505)
        to
        (3.3013346529248673, -0.15906349601907185)
        to
        (3.40782931914825, -0.26310256422752104)
        to
        (3.514323985371633, -0.3641605752528219)
        to
        (3.6208186515950156, -0.46109250144932556)
        to
        (3.7273133178183984, -0.5528000653611931)
        to
        (3.833807984041781, -0.6382441836448197)
        to
        (3.940302650265164, -0.7164567402983147)
        to
        (4.046797316488547, -0.7865515558026425)
        to
        (4.15329198271193, -0.8477344278896709)
        to
        (4.259786648935313, -0.8993121301712191)
        to
        (4.366281315158695, -0.9407002666710332)
        to
        (4.472775981382078, -0.9714298932647099)
        to
        (4.579270647605461, -0.9911528310040071)
        to
        (4.685765313828844, -0.9996456111234525)
        to
        (4.7922599800522265, -0.9968120070307502)
        to
        (4.898754646275609, -0.9826841245925211)
        to
        (5.005249312498992, -0.9574220383620057)
        to
        (5.111743978722375, -0.9213119778704132)
        to
        (5.218238644945758, -0.8747630845319616)
        to
        (5.32473331116914, -0.8183027759081695)
        to
        (5.431227977392523, -0.752570769856139)
        to
        (5.537722643615906, -0.6783118362696167)
        to
        (5.644217309839289, -0.5963673585385021)
        to
        (5.750711976062672, -0.50766580033884)
        to
        (5.857206642286055, -0.4132121857683782)
        to
        (5.963701308509438, -0.3140767120219489)
        to
        (6.070195974732821, -0.21138262362962446)
        to
        (6.1766906409562035, -0.1062934856473656)
        to
        (6.283185307179586, -2.4492935982947064e-16);
    \draw[gray!40, dashed] (0, 0) -- (6.28, 0);
    \draw[->, gray] (1.0, 0.6) -- (1.57, 1.0) node[right, font=\small] {peak};
\end{tikzpicture}

2.9 Putting it all together — a complete figure

Combine paths, shapes, nodes, and colours into a single publication-ready figure.

[19]:
tf_final = tz.TikzFigure(figsize=(12, 7))

# --- colours ---
tf_final.colorlet("cblue", "blue!65!white")
tf_final.colorlet("cred", "red!75!black")

# --- background ---
tf_final.rectangle((0, -1.3), (2 * np.pi, 1.3), fill="gray!5", draw="gray!30")

# --- zero axis ---
tf_final.line((0, 0), (2 * np.pi, 0), color="gray!60", dash_pattern="on 2pt off 2pt")

# --- shaded area between curves ---
# approximate: shade where sin > cos (first half)
x_half = x[x <= np.pi]
upper = np.sin(x_half)
lower = np.cos(x_half)
region = [(float(xi), float(u)) for xi, u in zip(x_half, upper)] + [
    (float(xi), float(l)) for xi, l in zip(reversed(x_half), reversed(lower))
]
tf_final.draw(region, fill="cblue!20", draw="none", cycle=True)

# --- curves ---
tf_final.draw(sin_nodes, color="cblue", line_width=1.8)
tf_final.draw(cos_nodes, color="cred", line_width=1.5)

# --- markers at key points ---
tf_final.circle((np.pi / 2, 1.0), radius=0.08, fill="cblue", draw="none")
tf_final.circle((np.pi, 0.0), radius=0.08, fill="cblue", draw="none")
tf_final.circle((0, 1.0), radius=0.08, fill="cred", draw="none")

# --- labels ---
tf_final.add_node(
    np.pi / 2 + 0.3, 1.05, content=r"$\sin(x)$", color="cblue", anchor="west"
)
tf_final.add_node(0.2, 1.1, content=r"$\cos(x)$", color="cred", anchor="west")

# --- save and show ---
with open("complete_figure.tex", "w") as f:
    f.write(tf_final.generate_tikz())
print("Saved complete_figure.tex")
print()
print(tf_final.generate_tikz())
Saved complete_figure.tex

% --------------------------------------------- %
% Tikzfigure generated by tikzfigure v0.3.0     %
% https://github.com/max-models/tikzfigure      %
% --------------------------------------------- %
\begin{tikzpicture}
    \colorlet{cblue}{blue!65!white}
    \colorlet{cred}{red!75!black}
    \draw[fill=gray!5, draw=gray!30] ({0}, {-1.3}) rectangle ({6.283185307179586}, {1.3});
    \draw[color=gray!60, dash pattern=on 2pt off 2pt] ({0}, {0}) -- ({6.283185307179586}, {0});
    \draw[fill=cblue!20, draw=none] (0.0, 0.0)
        to
        (0.10649466622338281, 0.1062934856473654)
        to
        (0.21298933244676563, 0.21138262362962432)
        to
        (0.31948399867014843, 0.31407671202194876)
        to
        (0.42597866489353126, 0.41321218576837815)
        to
        (0.5324733311169141, 0.5076658003388399)
        to
        (0.6389679973402969, 0.5963673585385013)
        to
        (0.7454626635636797, 0.6783118362696161)
        to
        (0.8519573297870625, 0.7525707698561385)
        to
        (0.9584519960104453, 0.818302775908169)
        to
        (1.0649466622338282, 0.8747630845319613)
        to
        (1.171441328457211, 0.921311977870413)
        to
        (1.2779359946805937, 0.9574220383620055)
        to
        (1.3844306609039765, 0.9826841245925209)
        to
        (1.4909253271273595, 0.9968120070307501)
        to
        (1.5974199933507423, 0.9996456111234526)
        to
        (1.703914659574125, 0.9911528310040072)
        to
        (1.8104093257975078, 0.97142989326471)
        to
        (1.9169039920208906, 0.9407002666710333)
        to
        (2.0233986582442736, 0.8993121301712192)
        to
        (2.1298933244676563, 0.847734427889671)
        to
        (2.236387990691039, 0.7865515558026426)
        to
        (2.342882656914422, 0.7164567402983153)
        to
        (2.4493773231378047, 0.6382441836448203)
        to
        (2.5558719893611874, 0.5528000653611936)
        to
        (2.66236665558457, 0.4610925014493262)
        to
        (2.768861321807953, 0.3641605752528225)
        to
        (2.875355988031336, 0.26310256422752126)
        to
        (2.981850654254719, 0.1590634960190721)
        to
        (3.0883453204781017, 0.053222174842178754)
        to
        (3.0883453204781017, -0.9985826956767619)
        to
        (2.981850654254719, -0.9872683547213446)
        to
        (2.875355988031336, -0.9647678688145159)
        to
        (2.768861321807953, -0.9313361774523383)
        to
        (2.66236665558457, -0.8873520750565714)
        to
        (2.5558719893611874, -0.8333139190825148)
        to
        (2.4493773231378047, -0.769833983429906)
        to
        (2.342882656914422, -0.6976315211349846)
        to
        (2.236387990691039, -0.6175246149461918)
        to
        (2.1298933244676563, -0.5304209081197424)
        to
        (2.0233986582442736, -0.43730732045885534)
        to
        (1.9169039920208906, -0.33923886611803006)
        to
        (1.8104093257975078, -0.23732669987111465)
        to
        (1.703914659574125, -0.13272552728372186)
        to
        (1.5974199933507423, -0.026620521437774693)
        to
        (1.4909253271273595, 0.0797861055530831)
        to
        (1.3844306609039765, 0.18528872408711453)
        to
        (1.2779359946805937, 0.28869194733962117)
        to
        (1.171441328457211, 0.38882417547332077)
        to
        (1.0649466622338282, 0.4845508703326502)
        to
        (0.9584519960104453, 0.5747874102144069)
        to
        (0.8519573297870625, 0.6585113790650386)
        to
        (0.7454626635636797, 0.7347741508630673)
        to
        (0.6389679973402969, 0.8027116379309637)
        to
        (0.5324733311169141, 0.8615540813938061)
        to
        (0.42597866489353126, 0.9106347728549132)
        to
        (0.31948399867014843, 0.9493976084683813)
        to
        (0.21298933244676563, 0.9774033898178667)
        to
        (0.10649466622338281, 0.9943348002101371)
        to
        (0.0, 1.0) -- cycle;
    \draw[color=cblue, line width=1.8] (0.0, 0.0)
        to
        (0.10649466622338281, 0.1062934856473654)
        to
        (0.21298933244676563, 0.21138262362962432)
        to
        (0.31948399867014843, 0.31407671202194876)
        to
        (0.42597866489353126, 0.41321218576837815)
        to
        (0.5324733311169141, 0.5076658003388399)
        to
        (0.6389679973402969, 0.5963673585385013)
        to
        (0.7454626635636797, 0.6783118362696161)
        to
        (0.8519573297870625, 0.7525707698561385)
        to
        (0.9584519960104453, 0.818302775908169)
        to
        (1.0649466622338282, 0.8747630845319613)
        to
        (1.171441328457211, 0.921311977870413)
        to
        (1.2779359946805937, 0.9574220383620055)
        to
        (1.3844306609039765, 0.9826841245925209)
        to
        (1.4909253271273595, 0.9968120070307501)
        to
        (1.5974199933507423, 0.9996456111234526)
        to
        (1.703914659574125, 0.9911528310040072)
        to
        (1.8104093257975078, 0.97142989326471)
        to
        (1.9169039920208906, 0.9407002666710333)
        to
        (2.0233986582442736, 0.8993121301712192)
        to
        (2.1298933244676563, 0.847734427889671)
        to
        (2.236387990691039, 0.7865515558026426)
        to
        (2.342882656914422, 0.7164567402983153)
        to
        (2.4493773231378047, 0.6382441836448203)
        to
        (2.5558719893611874, 0.5528000653611936)
        to
        (2.66236665558457, 0.4610925014493262)
        to
        (2.768861321807953, 0.3641605752528225)
        to
        (2.875355988031336, 0.26310256422752126)
        to
        (2.981850654254719, 0.1590634960190721)
        to
        (3.0883453204781017, 0.053222174842178754)
        to
        (3.1948399867014845, -0.053222174842178505)
        to
        (3.3013346529248673, -0.15906349601907185)
        to
        (3.40782931914825, -0.26310256422752104)
        to
        (3.514323985371633, -0.3641605752528219)
        to
        (3.6208186515950156, -0.46109250144932556)
        to
        (3.7273133178183984, -0.5528000653611931)
        to
        (3.833807984041781, -0.6382441836448197)
        to
        (3.940302650265164, -0.7164567402983147)
        to
        (4.046797316488547, -0.7865515558026425)
        to
        (4.15329198271193, -0.8477344278896709)
        to
        (4.259786648935313, -0.8993121301712191)
        to
        (4.366281315158695, -0.9407002666710332)
        to
        (4.472775981382078, -0.9714298932647099)
        to
        (4.579270647605461, -0.9911528310040071)
        to
        (4.685765313828844, -0.9996456111234525)
        to
        (4.7922599800522265, -0.9968120070307502)
        to
        (4.898754646275609, -0.9826841245925211)
        to
        (5.005249312498992, -0.9574220383620057)
        to
        (5.111743978722375, -0.9213119778704132)
        to
        (5.218238644945758, -0.8747630845319616)
        to
        (5.32473331116914, -0.8183027759081695)
        to
        (5.431227977392523, -0.752570769856139)
        to
        (5.537722643615906, -0.6783118362696167)
        to
        (5.644217309839289, -0.5963673585385021)
        to
        (5.750711976062672, -0.50766580033884)
        to
        (5.857206642286055, -0.4132121857683782)
        to
        (5.963701308509438, -0.3140767120219489)
        to
        (6.070195974732821, -0.21138262362962446)
        to
        (6.1766906409562035, -0.1062934856473656)
        to
        (6.283185307179586, -2.4492935982947064e-16);
    \draw[color=cred, line width=1.5] (0.0, 1.0)
        to
        (0.10649466622338281, 0.9943348002101371)
        to
        (0.21298933244676563, 0.9774033898178667)
        to
        (0.31948399867014843, 0.9493976084683813)
        to
        (0.42597866489353126, 0.9106347728549132)
        to
        (0.5324733311169141, 0.8615540813938061)
        to
        (0.6389679973402969, 0.8027116379309637)
        to
        (0.7454626635636797, 0.7347741508630673)
        to
        (0.8519573297870625, 0.6585113790650386)
        to
        (0.9584519960104453, 0.5747874102144069)
        to
        (1.0649466622338282, 0.4845508703326502)
        to
        (1.171441328457211, 0.38882417547332077)
        to
        (1.2779359946805937, 0.28869194733962117)
        to
        (1.3844306609039765, 0.18528872408711453)
        to
        (1.4909253271273595, 0.0797861055530831)
        to
        (1.5974199933507423, -0.026620521437774693)
        to
        (1.703914659574125, -0.13272552728372186)
        to
        (1.8104093257975078, -0.23732669987111465)
        to
        (1.9169039920208906, -0.33923886611803006)
        to
        (2.0233986582442736, -0.43730732045885534)
        to
        (2.1298933244676563, -0.5304209081197424)
        to
        (2.236387990691039, -0.6175246149461918)
        to
        (2.342882656914422, -0.6976315211349846)
        to
        (2.4493773231378047, -0.769833983429906)
        to
        (2.5558719893611874, -0.8333139190825148)
        to
        (2.66236665558457, -0.8873520750565714)
        to
        (2.768861321807953, -0.9313361774523383)
        to
        (2.875355988031336, -0.9647678688145159)
        to
        (2.981850654254719, -0.9872683547213446)
        to
        (3.0883453204781017, -0.9985826956767619)
        to
        (3.1948399867014845, -0.9985826956767619)
        to
        (3.3013346529248673, -0.9872683547213446)
        to
        (3.40782931914825, -0.964767868814516)
        to
        (3.514323985371633, -0.9313361774523385)
        to
        (3.6208186515950156, -0.8873520750565718)
        to
        (3.7273133178183984, -0.8333139190825152)
        to
        (3.833807984041781, -0.7698339834299065)
        to
        (3.940302650265164, -0.697631521134985)
        to
        (4.046797316488547, -0.617524614946192)
        to
        (4.15329198271193, -0.5304209081197426)
        to
        (4.259786648935313, -0.43730732045885556)
        to
        (4.366281315158695, -0.3392388661180305)
        to
        (4.472775981382078, -0.2373266998711151)
        to
        (4.579270647605461, -0.1327255272837223)
        to
        (4.685765313828844, -0.02662052143777516)
        to
        (4.7922599800522265, 0.07978610555308265)
        to
        (4.898754646275609, 0.18528872408711383)
        to
        (5.005249312498992, 0.2886919473396205)
        to
        (5.111743978722375, 0.3888241754733201)
        to
        (5.218238644945758, 0.4845508703326496)
        to
        (5.32473331116914, 0.5747874102144063)
        to
        (5.431227977392523, 0.6585113790650381)
        to
        (5.537722643615906, 0.7347741508630667)
        to
        (5.644217309839289, 0.8027116379309631)
        to
        (5.750711976062672, 0.8615540813938061)
        to
        (5.857206642286055, 0.9106347728549131)
        to
        (5.963701308509438, 0.9493976084683813)
        to
        (6.070195974732821, 0.9774033898178666)
        to
        (6.1766906409562035, 0.9943348002101371)
        to
        (6.283185307179586, 1.0);
    \draw[fill=cblue, draw=none] ({1.5707963267948966}, {1.0}) circle (0.08);
    \draw[fill=cblue, draw=none] ({3.141592653589793}, {0.0}) circle (0.08);
    \draw[fill=cred, draw=none] ({0}, {1.0}) circle (0.08);
    \node[color=cblue, anchor=west] (node0) at ({1.8707963267948966}, {1.05}) {$\sin(x)$};
    \node[color=cred, anchor=west] (node1) at ({0.2}, {1.1}) {$\cos(x)$};
\end{tikzpicture}

[20]:
# Renders to PDF (requires pdflatex):
tf_final.show()
../_images/tutorials_tutorial_07_tikz_40_0.png

2.10 Embedding in a LaTeX document

The generated code is a standalone tikzpicture environment.
Drop it into any LaTeX document:
\usepackage{tikz}

\begin{figure}[h]
  \centering
  \input{complete_figure.tex}
  \caption{Trigonometric functions with shaded region.}
  \label{fig:trig}
\end{figure}

Or compile a standalone PDF with tikzfigure’s generate_standalone() method:

standalone_src = tf_final.generate_standalone()
with open('standalone.tex', 'w') as f:
    f.write(standalone_src)
# Then: pdflatex standalone.tex

Summary

Canvas → TikZ workflow

canvas = Canvas(width='10cm', ratio=0.6)
canvas.plot(x, y, color='steelblue', line_width=1.5)
tikz = canvas.render(backend='tikzfigure')
print(tikz.generate_tikz())       # inspect LaTeX
tikz.show()                       # render (needs pdflatex)

Direct tikzfigure API — key methods

Method

Purpose

tf.draw(nodes, color=, line_width=, fill=, ...)

Path through coordinate list

tf.line(start, end, arrows='->', ...)

Straight line segment

tf.rectangle(corner1, corner2, fill=, draw=, ...)

Rectangle

tf.circle(center, radius, fill=, ...)

Circle

tf.arc(start, start_angle, end_angle, radius, ...)

Arc

tf.add_node(x, y, content=, shape=, fill=, ...)

Labelled node

tf.colorlet(name, color_expr)

Define named colour

tf.add_raw(tikz_code)

Inject verbatim TikZ

tf.generate_tikz()

Return LaTeX string

tf.show()

Compile + display (needs pdflatex)

TikZ colour syntax cheatsheet

Expression

Meaning

'red', 'blue', 'green'

Standard colours

'blue!70!white'

70% blue + 30% white

'red!80!black'

80% red + 20% black

'blue!50!red'

50% blend

'gray!20'

20% gray (80% white)

Part 1.8 — Canvas primitives supported by TikZ

The Canvas TikZ backend supports line, scatter, bar, horizontal-bar, filled-region, and error-bar plots. The following example renders the canvas as TikZ source.

[21]:
import numpy as np
from maxplotlib import Canvas

x = np.arange(5)
y = np.array([1.0, 2.2, 1.4, 3.0, 2.5])

primitive_canvas = Canvas()
primitive_canvas.plot(x, y, color="black", linewidth=1.5)
primitive_canvas.scatter(x, y + 0.35, color="crimson")
primitive_canvas.bar(x, y * 0.35, color="steelblue", alpha=0.7)
primitive_canvas.fill_between(x, y, 0, color="gold", alpha=0.2)
primitive_canvas.errorbar(x, y, yerr=0.15, color="darkgreen")
primitive_canvas.configure(
    title="TikZ-supported Canvas primitives",
    xlabel="Sample",
    ylabel="Value",
    grid=True,
)
[21]:
Canvas(nrows=1, ncols=1, caption=None, label=None)
[22]:
tikz_primitive_figure = primitive_canvas.render(backend="tikzfigure")
print(str(tikz_primitive_figure)[:2000])
\begin{tikzpicture}
    \begin{groupplot}[group style={group size=1 by 1, horizontal sep=1.5cm}]
            \nextgroupplot[grid=true, title=Subplot 1, axis width=None]
            \addplot[color=black, line width=1.5] coordinates {(0.0,1.0) (1.0,2.2) (2.0,1.4) (3.0,3.0) (4.0,2.5)};
            \addplot[color=crimson, mark=*, line width=0] coordinates {(0.0,1.35) (1.0,2.5500000000000003) (2.0,1.75) (3.0,3.35) (4.0,2.85)};
            \addplot[cycle=True, color=steelblue, opacity=0.7, fill=steelblue, fill opacity=0.7, line width=0] coordinates {(-0.4,0) (0.4,0) (0.4,0.35) (-0.4,0.35)};
            \addplot[cycle=True, color=steelblue, opacity=0.7, fill=steelblue, fill opacity=0.7, line width=0] coordinates {(0.6,0) (1.4,0) (1.4,0.77) (0.6,0.77)};
            \addplot[cycle=True, color=steelblue, opacity=0.7, fill=steelblue, fill opacity=0.7, line width=0] coordinates {(1.6,0) (2.4,0) (2.4,0.48999999999999994) (1.6,0.48999999999999994)};
            \addplot[cycle=True, color=steelblue, opacity=0.7, fill=steelblue, fill opacity=0.7, line width=0] coordinates {(2.6,0) (3.4,0) (3.4,1.0499999999999998) (2.6,1.0499999999999998)};
            \addplot[cycle=True, color=steelblue, opacity=0.7, fill=steelblue, fill opacity=0.7, line width=0] coordinates {(3.6,0) (4.4,0) (4.4,0.875) (3.6,0.875)};
            \addplot[cycle=True, color=gold, opacity=0.2, fill=gold, fill opacity=0.2] coordinates {(0,1.0) (1,2.2) (2,1.4) (3,3.0) (4,2.5) (4,0) (3,0) (2,0) (1,0) (0,0)};
            \addplot[color=darkgreen] coordinates {(0,1.0) (1,2.2) (2,1.4) (3,3.0) (4,2.5)};
            \addplot[color=darkgreen] coordinates {(0,0.85) (0,1.15)};
            \addplot[color=darkgreen] coordinates {(1,2.0500000000000003) (1,2.35)};
            \addplot[color=darkgreen] coordinates {(2,1.25) (2,1.5499999999999998)};
            \addplot[color=darkgreen] coordinates {(3,2.85) (3,3.15)};
            \addplot[color=darkgreen] coordinates {(4,2.35) (4,2.65)};

    \end{groupplot}
\end{tikzpicture}

The next cell shows the TikZ generated from the canvas. Unsupported primitives now raise NotImplementedError instead of being silently omitted.

Part 1.9 — More TikZ-supported primitives

Step and stairs plots, stems, reference lines, spans, and polygon fills are also translated to TikZ. The generated TikZ is printed below.

[23]:
more_canvas = Canvas()
more_canvas.step(x, y, color="black", where="post")
more_canvas.stem(x, y, linefmt="m-", markerfmt="mo")
more_canvas.hlines([1.5, 2.5], 0, 4, color="gray", linestyle="--")
more_canvas.vlines([1, 3], 0, 3.5, color="gray")
more_canvas.axvspan(1, 2, color="orange", alpha=0.2)
more_canvas.axhspan(1, 2, color="green", alpha=0.2)
more_canvas.fill(x, [0.2, 0.8, 0.4, 1.0, 0.2], color="cyan", alpha=0.2)
more_canvas.configure(
    title="Additional TikZ primitives", xlabel="Sample", ylabel="Value", grid=True
)
[23]:
Canvas(nrows=1, ncols=1, caption=None, label=None)
[24]:
more_tikz = more_canvas.render(backend="tikzfigure")
print(str(more_tikz)[:2000])
\begin{tikzpicture}
    \begin{groupplot}[group style={group size=1 by 1, horizontal sep=1.5cm}]
            \nextgroupplot[grid=true, title=Subplot 1, axis width=None]
            \addplot[color=black] coordinates {(0,1.0) (1,1.0) (1,2.2) (2,2.2) (2,1.4) (3,1.4) (3,3.0) (4,3.0) (4,2.5)};
            \addplot[color=black, mark=*, line width=0] coordinates {(0,1.0) (1,2.2) (2,1.4) (3,3.0) (4,2.5)};
            \addplot[color=black] coordinates {(0,0) (0,1.0)};
            \addplot[color=black] coordinates {(1,0) (1,2.2)};
            \addplot[color=black] coordinates {(2,0) (2,1.4)};
            \addplot[color=black] coordinates {(3,0) (3,3.0)};
            \addplot[color=black] coordinates {(4,0) (4,2.5)};
            \addplot[color=gray, dash pattern=on 4pt off 2pt] coordinates {(0,1.5) (4,1.5)};
            \addplot[color=gray] coordinates {(1,0) (1,3.5)};
            \addplot[cycle=True, color=orange, opacity=0.2, fill=orange, fill opacity=0.2] coordinates {(1,0) (2,0) (2,1) (1,1)};
            \addplot[cycle=True, color=green, opacity=0.2, fill=green, fill opacity=0.2] coordinates {(0,1) (1,1) (1,2) (0,2)};
            \addplot[cycle=True, color=cyan, opacity=0.2, fill=cyan, fill opacity=0.2] coordinates {(0,0.2) (1,0.8) (2,0.4) (3,1.0) (4,0.2)};

    \end{groupplot}
\end{tikzpicture}