TikzFigure Subplots Tutorial

This tutorial demonstrates how to create side-by-side subplots using the tikzfigure backend.

Basic 1×2 Layout

The simplest use case: two plots side by side.

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

%load_ext autoreload
%autoreload 2
[2]:
# Create a 1×2 canvas with custom width
x = np.linspace(0, 2 * np.pi, 200)
canvas, (ax1, ax2) = Canvas.subplots(ncols=2, width="10cm", ratio=0.3)

# Left subplot: sine wave
ax1.plot(x, np.sin(x), color="royalblue", label="sin(x)", linewidth=1.5)
ax1.set_title("sin(x)")
ax1.set_xlabel("x")
ax1.set_ylabel("amplitude")

# Right subplot: cosine wave
ax2.plot(x, np.cos(x), color="tomato", label="cos(x)", linewidth=1.5)
ax2.set_title("cos(x)")
ax2.set_xlabel("x")

# Set overall title
canvas.suptitle("1 × 2 Layout: Trigonometric Functions")

# Render with tikzfigure backend
result = canvas.show(backend="tikzfigure")
../_images/tutorials_tutorial_tikzfigure_subplots_2_0.png

1×3 Layout: Multiple Functions

You can create layouts with more than 2 subplots.

[3]:
# Create a 1×3 canvas
canvas, axes = Canvas.subplots(ncols=3, width="12cm", ratio=0.35)

# Three different functions
x = np.linspace(0, 2 * np.pi, 100)

axes[0].plot(x, np.sin(x), color="blue")
axes[0].set_title("sin(x)")
axes[0].set_xlabel("x")

axes[1].plot(x, np.cos(x), color="red")
axes[1].set_title("cos(x)")
axes[1].set_xlabel("x")

axes[2].plot(x, np.tan(x), color="darkgreen")
axes[2].set_title("tan(x)")
axes[2].set_xlabel("x")
axes[2].set_ylim(-5, 5)  # Limit y-range for tan

canvas.suptitle("1 × 3 Layout: Three Trigonometric Functions")
result = canvas.show(backend="tikzfigure")
print(result)
../_images/tutorials_tutorial_tikzfigure_subplots_4_0.png
\begin{tikzpicture}
    \begin{groupplot}[group style={group size=3 by 1, horizontal sep=1.5cm}]
            \nextgroupplot[xlabel=x, grid=false, caption=sin(x)]
            \addplot[color=blue, line width=1.0] coordinates {(0.0,0.0) (0.06346651825433926,0.0634239196565645) (0.12693303650867852,0.12659245357374926) (0.1903995547630178,0.18925124436041021) (0.25386607301735703,0.2511479871810792) (0.3173325912716963,0.3120334456984871) (0.3807991095260356,0.3716624556603276) (0.4442656277803748,0.42979491208917164) (0.5077321460347141,0.4861967361004687) (0.5711986642890533,0.5406408174555976) (0.6346651825433925,0.5929079290546404) (0.6981317007977318,0.6427876096865393) (0.7615982190520711,0.690079011482112) (0.8250647373064104,0.7345917086575333) (0.8885312555607496,0.7761464642917568) (0.9519977738150889,0.8145759520503357) (1.0154642920694281,0.8497254299495144) (1.0789308103237674,0.8814533634475821) (1.1423973285781066,0.9096319953545183) (1.2058638468324459,0.9341478602651067) (1.269330365086785,0.9549022414440739) (1.3327968833411243,0.9718115683235417) (1.3962634015954636,0.984807753012208) (1.4597299198498028,0.9938384644612541) (1.5231964381041423,0.998867339183008) (1.5866629563584815,0.9998741276738751) (1.6501294746128208,0.9968547759519424) (1.71359599286716,0.9898214418809327) (1.7770625111214993,0.9788024462147787) (1.8405290293758385,0.963842158559942) (1.9039955476301778,0.9450008187146685) (1.967462065884517,0.9223542941045814) (2.0309285841388562,0.8959937742913359) (2.0943951023931957,0.8660254037844385) (2.1578616206475347,0.8325698546347714) (2.221328138901874,0.795761840530832) (2.284794657156213,0.7557495743542583) (2.3482611754105527,0.7126941713788627) (2.4117276936648917,0.6667690005162917) (2.475194211919231,0.6181589862206051) (2.53866073017357,0.5670598638627709) (2.6021272484279097,0.5136773915734063) (2.6655937666822487,0.4582265217274105) (2.729060284936588,0.4009305354066136) (2.792526803190927,0.3420201433256689) (2.8559933214452666,0.28173255684142967) (2.9194598396996057,0.2203105327865408) (2.982926357953945,0.1580013959733499) (3.0463928762082846,0.09505604330418244) (3.1098593944626236,0.031727933498067656) (3.173325912716963,-0.03172793349806786) (3.236792430971302,-0.09505604330418263) (3.3002589492256416,-0.15800139597335008) (3.3637254674799806,-0.22031053278654056) (3.42719198573432,-0.28173255684142984) (3.490658503988659,-0.34202014332566866) (3.5541250222429985,-0.4009305354066138) (3.6175915404973376,-0.4582265217274103) (3.681058058751677,-0.5136773915734064) (3.744524577006016,-0.5670598638627706) (3.8079910952603555,-0.6181589862206053) (3.8714576135146945,-0.6667690005162915) (3.934924131769034,-0.7126941713788629) (3.998390650023373,-0.7557495743542582) (4.0618571682777125,-0.7957618405308321) (4.1253236865320515,-0.8325698546347713) (4.188790204786391,-0.8660254037844388) (4.25225672304073,-0.895993774291336) (4.3157232412950695,-0.9223542941045814) (4.3791897595494085,-0.9450008187146683) (4.442656277803748,-0.9638421585599422) (4.506122796058087,-0.9788024462147787) (4.569589314312426,-0.9898214418809327) (4.6330558325667655,-0.9968547759519423) (4.696522350821105,-0.9998741276738751) (4.759988869075444,-0.998867339183008) (4.823455387329783,-0.9938384644612541) (4.886921905584122,-0.9848077530122081) (4.950388423838462,-0.9718115683235417) (5.013854942092801,-0.9549022414440739) (5.07732146034714,-0.9341478602651068) (5.14078797860148,-0.9096319953545182) (5.204254496855819,-0.881453363447582) (5.267721015110158,-0.8497254299495144) (5.331187533364497,-0.8145759520503358) (5.394654051618837,-0.7761464642917566) (5.458120569873176,-0.7345917086575332) (5.521587088127515,-0.690079011482112) (5.585053606381854,-0.6427876096865396) (5.648520124636194,-0.5929079290546402) (5.711986642890533,-0.5406408174555974) (5.775453161144872,-0.4861967361004688) (5.838919679399211,-0.4297949120891719) (5.902386197653551,-0.37166245566032724) (5.96585271590789,-0.31203344569848707) (6.029319234162229,-0.2511479871810794) (6.092785752416569,-0.18925124436040974) (6.156252270670908,-0.12659245357374904) (6.219718788925247,-0.06342391965656452) (6.283185307179586,-2.4492935982947064e-16)};

            \nextgroupplot[xlabel=x, grid=false, caption=cos(x)]
            \addplot[color=red, line width=1.0] coordinates {(0.0,1.0) (0.06346651825433926,0.9979866764718844) (0.12693303650867852,0.9919548128307953) (0.1903995547630178,0.9819286972627067) (0.25386607301735703,0.9679487013963562) (0.3173325912716963,0.9500711177409454) (0.3807991095260356,0.9283679330160726) (0.4442656277803748,0.9029265382866212) (0.5077321460347141,0.8738493770697849) (0.5711986642890533,0.8412535328311812) (0.6346651825433925,0.8052702575310586) (0.6981317007977318,0.766044443118978) (0.7615982190520711,0.7237340381050701) (0.8250647373064104,0.6785094115571322) (0.8885312555607496,0.6305526670845225) (0.9519977738150889,0.5800569095711982) (1.0154642920694281,0.5272254676105024) (1.0789308103237674,0.4722710747726827) (1.1423973285781066,0.41541501300188644) (1.2058638468324459,0.3568862215918719) (1.269330365086785,0.2969203753282749) (1.3327968833411243,0.23575893550942728) (1.3962634015954636,0.17364817766693041) (1.4597299198498028,0.1108381999010111) (1.5231964381041423,0.04758191582374218) (1.5866629563584815,-0.01586596383480803) (1.6501294746128208,-0.07924995685678854) (1.71359599286716,-0.14231483827328523) (1.7770625111214993,-0.20480666806519074) (1.8405290293758385,-0.26647381369003503) (1.9039955476301778,-0.32706796331742166) (1.967462065884517,-0.3863451256931287) (2.0309285841388562,-0.4440666126057741) (2.0943951023931957,-0.5000000000000002) (2.1578616206475347,-0.5539200638661103) (2.221328138901874,-0.6056096871376668) (2.284794657156213,-0.654860733945285) (2.3482611754105527,-0.7014748877063214) (2.4117276936648917,-0.7452644496757547) (2.475194211919231,-0.7860530947427875) (2.53866073017357,-0.8236765814298327) (2.6021272484279097,-0.8579834132349771) (2.6655937666822487,-0.8888354486549234) (2.729060284936588,-0.9161084574320696) (2.792526803190927,-0.9396926207859083) (2.8559933214452666,-0.9594929736144974) (2.9194598396996057,-0.975429786885407) (2.982926357953945,-0.9874388886763943) (3.0463928762082846,-0.9954719225730846) (3.1098593944626236,-0.9994965423831851) (3.173325912716963,-0.9994965423831851) (3.236792430971302,-0.9954719225730846) (3.3002589492256416,-0.9874388886763943) (3.3637254674799806,-0.975429786885407) (3.42719198573432,-0.9594929736144974) (3.490658503988659,-0.9396926207859084) (3.5541250222429985,-0.9161084574320696) (3.6175915404973376,-0.8888354486549235) (3.681058058751677,-0.857983413234977) (3.744524577006016,-0.8236765814298328) (3.8079910952603555,-0.7860530947427874) (3.8714576135146945,-0.7452644496757548) (3.934924131769034,-0.7014748877063213) (3.998390650023373,-0.6548607339452852) (4.0618571682777125,-0.6056096871376666) (4.1253236865320515,-0.5539200638661105) (4.188790204786391,-0.4999999999999996) (4.25225672304073,-0.44406661260577396) (4.3157232412950695,-0.3863451256931287) (4.3791897595494085,-0.3270679633174219) (4.442656277803748,-0.26647381369003464) (4.506122796058087,-0.20480666806519054) (4.569589314312426,-0.14231483827328523) (4.6330558325667655,-0.07924995685678879) (4.696522350821105,-0.01586596383480761) (4.759988869075444,0.04758191582374238) (4.823455387329783,0.11083819990101086) (4.886921905584122,0.17364817766692997) (4.950388423838462,0.23575893550942748) (5.013854942092801,0.2969203753282749) (5.07732146034714,0.35688622159187167) (5.14078797860148,0.4154150130018868) (5.204254496855819,0.4722710747726829) (5.267721015110158,0.5272254676105024) (5.331187533364497,0.5800569095711979) (5.394654051618837,0.6305526670845228) (5.458120569873176,0.6785094115571323) (5.521587088127515,0.7237340381050701) (5.585053606381854,0.7660444431189778) (5.648520124636194,0.8052702575310587) (5.711986642890533,0.8412535328311812) (5.775453161144872,0.8738493770697849) (5.838919679399211,0.9029265382866211) (5.902386197653551,0.9283679330160727) (5.96585271590789,0.9500711177409454) (6.029319234162229,0.9679487013963562) (6.092785752416569,0.9819286972627068) (6.156252270670908,0.9919548128307953) (6.219718788925247,0.9979866764718844) (6.283185307179586,1.0)};

            \nextgroupplot[xlabel=x, ymin=-5, ymax=5, grid=false, caption=tan(x)]
            \addplot[color=darkgreen, line width=1.0] coordinates {(0.0,0.0) (0.06346651825433926,0.06355187013195693) (0.12693303650867852,0.12761917371264675) (0.1903995547630178,0.19273420248128023) (0.25386607301735703,0.2594641501339635) (0.3173325912716963,0.32843167197886425) (0.3807991095260356,0.4003396093754275) (0.4442656277803748,0.4760020819686433) (0.5077321460347141,0.5563850577210417) (0.5711986642890533,0.642660977168331) (0.6346651825433925,0.7362844008078524) (0.6981317007977318,0.8390996311772799) (0.7615982190520711,0.9534980740838499) (0.8250647373064104,1.0826551498699128) (0.8885312555607496,1.2308987096675277) (0.9519977738150889,1.4043035064481926) (1.0154642920694281,1.6116926858648353) (1.0789308103237674,1.8664140374717002) (1.1423973285781066,2.189694562989681) (1.2058638468324459,2.617494887021388) (1.269330365086785,3.21602126626822) (1.3327968833411243,4.12205614274451) (1.3962634015954636,5.671281819617707) (1.4597299198498028,8.966569877071668) (1.5231964381041423,20.992583461395604) (1.5866629563584815,-63.02006849910187) (1.6501294746128208,-12.578616007997889) (1.71359599286716,-6.95515277177347) (1.7770625111214993,-4.779153215378819) (1.8405290293758385,-3.6170239214615387) (1.9039955476301778,-2.8893102495567224) (1.967462065884517,-2.387384317195194) (2.0309285841388562,-2.017701283673776) (2.0943951023931957,-1.7320508075688765) (2.1578616206475347,-1.5030505463618924) (2.221328138901874,-1.3139846627815581) (2.284794657156213,-1.1540615205330096) (2.3482611754105527,-1.015993849343953) (2.4117276936648917,-0.8946743680131068) (2.475194211919231,-0.7864086921798574) (2.53866073017357,-0.6884496617330105) (2.6021272484279097,-0.5987031726366542) (2.6655937666822487,-0.5155358310931969) (2.729060284936588,-0.4376452724063439) (2.792526803190927,-0.36397023426620256) (2.8559933214452666,-0.2936264929383667) (2.9194598396996057,-0.22585996014126516) (2.982926357953945,-0.1600113159257296) (3.0463928762082846,-0.09548842227361133) (3.1098593944626236,-0.03174391521396965) (3.173325912716963,0.03174391521396985) (3.236792430971302,0.09548842227361154) (3.3002589492256416,0.1600113159257298) (3.3637254674799806,0.22585996014126491) (3.42719198573432,0.2936264929383669) (3.490658503988659,0.3639702342662023) (3.5541250222429985,0.43764527240634415) (3.6175915404973376,0.5155358310931967) (3.681058058751677,0.5987031726366544) (3.744524577006016,0.6884496617330101) (3.8079910952603555,0.7864086921798578) (3.8714576135146945,0.8946743680131064) (3.934924131769034,1.0159938493439535) (3.998390650023373,1.1540615205330091) (4.0618571682777125,1.3139846627815586) (4.1253236865320515,1.5030505463618917) (4.188790204786391,1.732050807568879) (4.25225672304073,2.017701283673777) (4.3157232412950695,2.387384317195194) (4.3791897595494085,2.88931024955672) (4.442656277803748,3.617023921461545) (4.506122796058087,4.779153215378823) (4.569589314312426,6.955152771773468) (4.6330558325667655,12.57861600799785) (4.696522350821105,63.020068499103544) (4.759988869075444,-20.99258346139552) (4.823455387329783,-8.966569877071688) (4.886921905584122,-5.671281819617723) (4.950388423838462,-4.122056142744507) (5.013854942092801,-3.2160212662682204) (5.07732146034714,-2.61749488702139) (5.14078797860148,-2.1896945629896787) (5.204254496855819,-1.8664140374716993) (5.267721015110158,-1.6116926858648355) (5.331187533364497,-1.4043035064481932) (5.394654051618837,-1.2308987096675266) (5.458120569873176,-1.0826551498699124) (5.521587088127515,-0.95349807408385) (5.585053606381854,-0.8390996311772806) (5.648520124636194,-0.7362844008078518) (5.711986642890533,-0.6426609771683309) (5.775453161144872,-0.556385057721042) (5.838919679399211,-0.47600208196864374) (5.902386197653551,-0.4003396093754271) (5.96585271590789,-0.3284316719788642) (6.029319234162229,-0.25946415013396373) (6.092785752416569,-0.19273420248127973) (6.156252270670908,-0.12761917371264653) (6.219718788925247,-0.06355187013195694) (6.283185307179586,-2.4492935982947064e-16)};

    \end{groupplot}
\end{tikzpicture}

Important Notes

  • Only horizontal layouts (1×n) are supported with tikzfigure backend

  • Vertical/grid layouts (nrows > 1) will raise an error

  • Use matplotlib backend for complex layouts or grids

  • Each subplot’s title becomes a subfigure caption in the LaTeX output