Styling: Colors, Shapes, and Line Styles
This tutorial explores the visual styling options available in tikzfigure: - node shapes - colors and color mixing - line styles and widths - arrow types - node anchors
from tikzfigure import TikzFigureNode shapes
Section titled “Node shapes”The shape parameter accepts any TikZ node shape. Common built-in
shapes are circle, rectangle, ellipse, and diamond. The
minimum_size / minimum_width / minimum_height options control the
node size.
fig = TikzFigure()
fig.add_node(0, 0, shape="circle", fill="blue!30", content="circle")fig.add_node(3, 0, shape="rectangle", fill="red!30", content="rectangle")fig.add_node(6, 0, shape="ellipse", fill="green!30", content="ellipse")fig.add_node(9, 0, shape="diamond", fill="orange!30", content="diamond")
fig.show()
Show Tikz code
print(fig)% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} \node[shape=circle, fill=blue!30] (node0) at ({0}, {0}) {circle}; \node[shape=rectangle, fill=red!30] (node1) at ({3}, {0}) {rectangle}; \node[shape=ellipse, fill=green!30] (node2) at ({6}, {0}) {ellipse}; \node[shape=diamond, fill=orange!30] (node3) at ({9}, {0}) {diamond};\end{tikzpicture}print(fig.generate_standalone())\documentclass[border=10pt]{standalone}\usepackage{tikz}\usepackage{pgfplots}\pgfplotsset{compat=newest}\usepgfplotslibrary{groupplots}\usetikzlibrary{arrows.meta}\begin{document}% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} \node[shape=circle, fill=blue!30] (node0) at ({0}, {0}) {circle}; \node[shape=rectangle, fill=red!30] (node1) at ({3}, {0}) {rectangle}; \node[shape=ellipse, fill=green!30] (node2) at ({6}, {0}) {ellipse}; \node[shape=diamond, fill=orange!30] (node3) at ({9}, {0}) {diamond};\end{tikzpicture}
\end{document}Colors and color mixing
Section titled “Colors and color mixing”TikZ supports named colors (red, blue, green, …) and mix
expressions like red!50!white (50 % red + 50 % white). You can define
custom colors with fig.colorlet(name, expression).
fig = TikzFigure()
fig.colorlet("lightblue", "blue!30!white")fig.colorlet("salmon", "red!40!white")fig.colorlet("mint", "green!40!white")
colors = [ ("red", "red"), ("red!50!white", "red!50!white"), ("red!20!white", "red!20!white"), ("salmon", "salmon (custom)"), ("lightblue", "lightblue"), ("mint", "mint"), ("cyan!60", "cyan!60"), ("purple", "purple"),]
for i, (color, label) in enumerate(colors): fig.add_node(i * 3, 0, shape="circle", fill=color, content="", minimum_size="1cm") fig.add_node(i * 3, -1, content=f"\\texttt{{{label}}}", draw="none")
fig.show()
Show Tikz code
print(fig)% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} \colorlet{lightblue}{blue!30!white} \colorlet{salmon}{red!40!white} \colorlet{mint}{green!40!white} \node[shape=circle, fill=red, minimum size=1cm] (node0) at ({0}, {0}) {}; \node[draw=none] (node1) at ({0}, {-1}) {\texttt{red}}; \node[shape=circle, fill=red!50!white, minimum size=1cm] (node2) at ({3}, {0}) {}; \node[draw=none] (node3) at ({3}, {-1}) {\texttt{red!50!white}}; \node[shape=circle, fill=red!20!white, minimum size=1cm] (node4) at ({6}, {0}) {}; \node[draw=none] (node5) at ({6}, {-1}) {\texttt{red!20!white}}; \node[shape=circle, fill=salmon, minimum size=1cm] (node6) at ({9}, {0}) {}; \node[draw=none] (node7) at ({9}, {-1}) {\texttt{salmon (custom)}}; \node[shape=circle, fill=lightblue, minimum size=1cm] (node8) at ({12}, {0}) {}; \node[draw=none] (node9) at ({12}, {-1}) {\texttt{lightblue}}; \node[shape=circle, fill=mint, minimum size=1cm] (node10) at ({15}, {0}) {}; \node[draw=none] (node11) at ({15}, {-1}) {\texttt{mint}}; \node[shape=circle, fill=cyan!60, minimum size=1cm] (node12) at ({18}, {0}) {}; \node[draw=none] (node13) at ({18}, {-1}) {\texttt{cyan!60}}; \node[shape=circle, fill=purple, minimum size=1cm] (node14) at ({21}, {0}) {}; \node[draw=none] (node15) at ({21}, {-1}) {\texttt{purple}};\end{tikzpicture}print(fig.generate_standalone())\documentclass[border=10pt]{standalone}\usepackage{tikz}\usepackage{pgfplots}\pgfplotsset{compat=newest}\usepgfplotslibrary{groupplots}\usetikzlibrary{arrows.meta}\begin{document}% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} \colorlet{lightblue}{blue!30!white} \colorlet{salmon}{red!40!white} \colorlet{mint}{green!40!white} \node[shape=circle, fill=red, minimum size=1cm] (node0) at ({0}, {0}) {}; \node[draw=none] (node1) at ({0}, {-1}) {\texttt{red}}; \node[shape=circle, fill=red!50!white, minimum size=1cm] (node2) at ({3}, {0}) {}; \node[draw=none] (node3) at ({3}, {-1}) {\texttt{red!50!white}}; \node[shape=circle, fill=red!20!white, minimum size=1cm] (node4) at ({6}, {0}) {}; \node[draw=none] (node5) at ({6}, {-1}) {\texttt{red!20!white}}; \node[shape=circle, fill=salmon, minimum size=1cm] (node6) at ({9}, {0}) {}; \node[draw=none] (node7) at ({9}, {-1}) {\texttt{salmon (custom)}}; \node[shape=circle, fill=lightblue, minimum size=1cm] (node8) at ({12}, {0}) {}; \node[draw=none] (node9) at ({12}, {-1}) {\texttt{lightblue}}; \node[shape=circle, fill=mint, minimum size=1cm] (node10) at ({15}, {0}) {}; \node[draw=none] (node11) at ({15}, {-1}) {\texttt{mint}}; \node[shape=circle, fill=cyan!60, minimum size=1cm] (node12) at ({18}, {0}) {}; \node[draw=none] (node13) at ({18}, {-1}) {\texttt{cyan!60}}; \node[shape=circle, fill=purple, minimum size=1cm] (node14) at ({21}, {0}) {}; \node[draw=none] (node15) at ({21}, {-1}) {\texttt{purple}};\end{tikzpicture}
\end{document}Line styles
Section titled “Line styles”Paths accept TikZ style keywords as options: dashed, dotted,
thick, ultra thick, etc. line_width maps to TikZ
line width=<value>pt.
fig = TikzFigure()
styles = [ ([], 1, "plain"), (["dashed"], 1, "dashed"), (["dotted"], 1, "dotted"), (["thick"], 1, "thick"), ([], 4, "line width=4pt"), (["dashed"], 3, "dashed"),]
for i, (opts, lw, label) in enumerate(styles): y = -i * 1.2 fig.draw([(0, y), (5, y)], options=opts, line_width=lw, color="black") fig.add_node(5.1, y, content=f"\\texttt{{{label}}}", draw="none", anchor="west")fig.show()
Show Tikz code
print(fig)% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} \draw[color=black, line width=1] (0, 0.0) to (5, 0.0); \node[draw=none, anchor=west] (node0) at ({5.1}, {0.0}) {\texttt{plain}}; \draw[dashed, color=black, line width=1] (0, -1.2) to (5, -1.2); \node[draw=none, anchor=west] (node1) at ({5.1}, {-1.2}) {\texttt{dashed}}; \draw[dotted, color=black, line width=1] (0, -2.4) to (5, -2.4); \node[draw=none, anchor=west] (node2) at ({5.1}, {-2.4}) {\texttt{dotted}}; \draw[thick, color=black, line width=1] (0, -3.5999999999999996) to (5, -3.5999999999999996); \node[draw=none, anchor=west] (node3) at ({5.1}, {-3.5999999999999996}) {\texttt{thick}}; \draw[color=black, line width=4] (0, -4.8) to (5, -4.8); \node[draw=none, anchor=west] (node4) at ({5.1}, {-4.8}) {\texttt{line width=4pt}}; \draw[dashed, color=black, line width=3] (0, -6.0) to (5, -6.0); \node[draw=none, anchor=west] (node5) at ({5.1}, {-6.0}) {\texttt{dashed}};\end{tikzpicture}print(fig.generate_standalone())\documentclass[border=10pt]{standalone}\usepackage{tikz}\usepackage{pgfplots}\pgfplotsset{compat=newest}\usepgfplotslibrary{groupplots}\usetikzlibrary{arrows.meta}\begin{document}% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} \draw[color=black, line width=1] (0, 0.0) to (5, 0.0); \node[draw=none, anchor=west] (node0) at ({5.1}, {0.0}) {\texttt{plain}}; \draw[dashed, color=black, line width=1] (0, -1.2) to (5, -1.2); \node[draw=none, anchor=west] (node1) at ({5.1}, {-1.2}) {\texttt{dashed}}; \draw[dotted, color=black, line width=1] (0, -2.4) to (5, -2.4); \node[draw=none, anchor=west] (node2) at ({5.1}, {-2.4}) {\texttt{dotted}}; \draw[thick, color=black, line width=1] (0, -3.5999999999999996) to (5, -3.5999999999999996); \node[draw=none, anchor=west] (node3) at ({5.1}, {-3.5999999999999996}) {\texttt{thick}}; \draw[color=black, line width=4] (0, -4.8) to (5, -4.8); \node[draw=none, anchor=west] (node4) at ({5.1}, {-4.8}) {\texttt{line width=4pt}}; \draw[dashed, color=black, line width=3] (0, -6.0) to (5, -6.0); \node[draw=none, anchor=west] (node5) at ({5.1}, {-6.0}) {\texttt{dashed}};\end{tikzpicture}
\end{document}Arrow types
Section titled “Arrow types”Arrow direction and style are controlled via options. Standard TikZ
arrow specs work: ->, <-, <->, -stealth, |->, etc. bend_left
/ bend_right curve the path.
fig = TikzFigure()
arrows = [ ("->", "-> forward"), ("<-", "<- backward"), ("<->", "<-> both"), ("-stealth", "-stealth tip"), ("|->|", "|->| bar tips"),]
for i, (arrow, label) in enumerate(arrows): y = -i * 1.2 fig.draw([(0, y), (4, y)], options=[arrow], line_width=1.5, color="black") fig.add_node(5, y, content=f"\\texttt{{{label}}}", draw="none", anchor="west")
fig.show()
Show Tikz code
print(fig)% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} \draw[->, color=black, line width=1.5] (0, 0.0) to (4, 0.0); \node[draw=none, anchor=west] (node0) at ({5}, {0.0}) {\texttt{-> forward}}; \draw[<-, color=black, line width=1.5] (0, -1.2) to (4, -1.2); \node[draw=none, anchor=west] (node1) at ({5}, {-1.2}) {\texttt{<- backward}}; \draw[<->, color=black, line width=1.5] (0, -2.4) to (4, -2.4); \node[draw=none, anchor=west] (node2) at ({5}, {-2.4}) {\texttt{<-> both}}; \draw[-stealth, color=black, line width=1.5] (0, -3.5999999999999996) to (4, -3.5999999999999996); \node[draw=none, anchor=west] (node3) at ({5}, {-3.5999999999999996}) {\texttt{-stealth tip}}; \draw[|->|, color=black, line width=1.5] (0, -4.8) to (4, -4.8); \node[draw=none, anchor=west] (node4) at ({5}, {-4.8}) {\texttt{|->| bar tips}};\end{tikzpicture}print(fig.generate_standalone())\documentclass[border=10pt]{standalone}\usepackage{tikz}\usepackage{pgfplots}\pgfplotsset{compat=newest}\usepgfplotslibrary{groupplots}\usetikzlibrary{arrows.meta}\begin{document}% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} \draw[->, color=black, line width=1.5] (0, 0.0) to (4, 0.0); \node[draw=none, anchor=west] (node0) at ({5}, {0.0}) {\texttt{-> forward}}; \draw[<-, color=black, line width=1.5] (0, -1.2) to (4, -1.2); \node[draw=none, anchor=west] (node1) at ({5}, {-1.2}) {\texttt{<- backward}}; \draw[<->, color=black, line width=1.5] (0, -2.4) to (4, -2.4); \node[draw=none, anchor=west] (node2) at ({5}, {-2.4}) {\texttt{<-> both}}; \draw[-stealth, color=black, line width=1.5] (0, -3.5999999999999996) to (4, -3.5999999999999996); \node[draw=none, anchor=west] (node3) at ({5}, {-3.5999999999999996}) {\texttt{-stealth tip}}; \draw[|->|, color=black, line width=1.5] (0, -4.8) to (4, -4.8); \node[draw=none, anchor=west] (node4) at ({5}, {-4.8}) {\texttt{|->| bar tips}};\end{tikzpicture}
\end{document}Node anchors
Section titled “Node anchors”When placing a node at a coordinate, the anchor option controls which
part of the node sits at that point. The default is center. Useful
values: center, north, south, east, west, north west,
north east, south east, south west.
# Let's try to reproduce this: https://rmwu.github.io/tutorial/latex/2019/11/21/positioning/
fig = TikzFigure(figure_setup="every node/.append style={draw, minimum size=0.8cm}")
fig.add_node(0, 0, draw="none", content="\\textbullet", comment="center")fig.add_node(0, 0)fig.add_node(0, 0, options="anchor=center, label=below:{{center}}")
fig.add_node(2, 0, draw="none", content="\\textbullet", comment="West and east")fig.add_node(2, 0, anchor="west", content="west")fig.add_node(2, 0, anchor="east", content="east")
fig.add_node(4, 0, draw="none", content="\\textbullet", comment="North and south")fig.add_node(4, 0, anchor="north", content="north")fig.add_node(4, 0, anchor="south", content="south")
fig.add_node(6, 0, draw="none", content="\\textbullet", comment="NW, NE, SE, and SW")fig.add_node(6, 0, anchor="north west", content="nw")fig.add_node(6, 0, anchor="north east", content="ne")fig.add_node(6, 0, anchor="south east", content="se")fig.add_node(6, 0, anchor="south west", content="sw")
fig.show()
Show Tikz code
print(fig)% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} [every node/.append style={draw, minimum size=0.8cm}] % center \node[draw=none] (node0) at ({0}, {0}) {\textbullet}; \node (node1) at ({0}, {0}) {}; \node[anchor=center, label=below:{{center}}, ] (node2) at ({0}, {0}) {}; % West and east \node[draw=none] (node3) at ({2}, {0}) {\textbullet}; \node[anchor=west] (node4) at ({2}, {0}) {west}; \node[anchor=east] (node5) at ({2}, {0}) {east}; % North and south \node[draw=none] (node6) at ({4}, {0}) {\textbullet}; \node[anchor=north] (node7) at ({4}, {0}) {north}; \node[anchor=south] (node8) at ({4}, {0}) {south}; % NW, NE, SE, and SW \node[draw=none] (node9) at ({6}, {0}) {\textbullet}; \node[anchor=north west] (node10) at ({6}, {0}) {nw}; \node[anchor=north east] (node11) at ({6}, {0}) {ne}; \node[anchor=south east] (node12) at ({6}, {0}) {se}; \node[anchor=south west] (node13) at ({6}, {0}) {sw};\end{tikzpicture}print(fig.generate_standalone())\documentclass[border=10pt]{standalone}\usepackage{tikz}\usepackage{pgfplots}\pgfplotsset{compat=newest}\usepgfplotslibrary{groupplots}\usetikzlibrary{arrows.meta}\begin{document}% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} [every node/.append style={draw, minimum size=0.8cm}] % center \node[draw=none] (node0) at ({0}, {0}) {\textbullet}; \node (node1) at ({0}, {0}) {}; \node[anchor=center, label=below:{{center}}, ] (node2) at ({0}, {0}) {}; % West and east \node[draw=none] (node3) at ({2}, {0}) {\textbullet}; \node[anchor=west] (node4) at ({2}, {0}) {west}; \node[anchor=east] (node5) at ({2}, {0}) {east}; % North and south \node[draw=none] (node6) at ({4}, {0}) {\textbullet}; \node[anchor=north] (node7) at ({4}, {0}) {north}; \node[anchor=south] (node8) at ({4}, {0}) {south}; % NW, NE, SE, and SW \node[draw=none] (node9) at ({6}, {0}) {\textbullet}; \node[anchor=north west] (node10) at ({6}, {0}) {nw}; \node[anchor=north east] (node11) at ({6}, {0}) {ne}; \node[anchor=south east] (node12) at ({6}, {0}) {se}; \node[anchor=south west] (node13) at ({6}, {0}) {sw};\end{tikzpicture}
\end{document}Putting it together: Olympic rings
Section titled “Putting it together: Olympic rings”Five thick rings in the classic Olympic layout with correct interlocking overlaps. The weave order is Blue > Yellow > Black > Green > Red: at each crossing the “greater” ring appears in front.
-
- The full rings are drawn in layer 0.
-
- The overlapping crossings are drawn as arcs on top of the full rings in layer 1.
import math
fig = TikzFigure()
r = 1.0 # ring radius (cm)lw = "5pt" # line width
ring_specs = [ ((0, 0), (330, 360), "blue"), ((1.25, -1.25), (60, 90), "yellow!80!black"), ((2.5, 0), (330, 360), "black"), ((3.75, -1.25), (60, 90), "green!60!black"), ((5, 0), (None, None), "red"),]
def arc_seg(center, radius, start_angle, end_angle, color): """Draw an arc on the circle centered at `center`.""" cx, cy = center sx = cx + radius * math.cos(math.radians(start_angle)) sy = cy + radius * math.sin(math.radians(start_angle)) fig.arc( start=(sx, sy), start_angle=start_angle, end_angle=end_angle, radius=f"{radius}cm", color=color, line_width=lw, layer=1, )
for (cx, cy), (start_angle, end_angle), color in ring_specs: fig.circle(center=(cx, cy), radius=f"{r}cm", color=color, line_width=lw, layer=0) if start_angle is not None and end_angle is not None: arc_seg((cx, cy), r, start_angle, end_angle, color)
fig.show()
print(fig)
% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} % Define the layers library \pgfdeclarelayer{0} \pgfdeclarelayer{1} \pgfsetlayers{0,1}
% Layer 0 \begin{pgfonlayer}{0} \draw[color=blue, line width=5pt] (0, 0) circle (1.0cm); \draw[color=yellow!80!black, line width=5pt] (1.25, -1.25) circle (1.0cm); \draw[color=black, line width=5pt] (2.5, 0) circle (1.0cm); \draw[color=green!60!black, line width=5pt] (3.75, -1.25) circle (1.0cm); \draw[color=red, line width=5pt] (5, 0) circle (1.0cm); \end{pgfonlayer}{0}
% Layer 1 \begin{pgfonlayer}{1} \draw[color=blue, line width=5pt, start angle=330, end angle=360, radius=1.0cm] (0.8660254037844384, -0.5000000000000004) arc; \draw[color=yellow!80!black, line width=5pt, start angle=60, end angle=90, radius=1.0cm] (1.75, -0.3839745962155614) arc; \draw[color=black, line width=5pt, start angle=330, end angle=360, radius=1.0cm] (3.3660254037844384, -0.5000000000000004) arc; \draw[color=green!60!black, line width=5pt, start angle=60, end angle=90, radius=1.0cm] (4.25, -0.3839745962155614) arc; \end{pgfonlayer}{1}\end{tikzpicture}Show Tikz code
print(fig)% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} % Define the layers library \pgfdeclarelayer{0} \pgfdeclarelayer{1} \pgfsetlayers{0,1}
% Layer 0 \begin{pgfonlayer}{0} \draw[color=blue, line width=5pt] (0, 0) circle (1.0cm); \draw[color=yellow!80!black, line width=5pt] (1.25, -1.25) circle (1.0cm); \draw[color=black, line width=5pt] (2.5, 0) circle (1.0cm); \draw[color=green!60!black, line width=5pt] (3.75, -1.25) circle (1.0cm); \draw[color=red, line width=5pt] (5, 0) circle (1.0cm); \end{pgfonlayer}{0}
% Layer 1 \begin{pgfonlayer}{1} \draw[color=blue, line width=5pt, start angle=330, end angle=360, radius=1.0cm] (0.8660254037844384, -0.5000000000000004) arc; \draw[color=yellow!80!black, line width=5pt, start angle=60, end angle=90, radius=1.0cm] (1.75, -0.3839745962155614) arc; \draw[color=black, line width=5pt, start angle=330, end angle=360, radius=1.0cm] (3.3660254037844384, -0.5000000000000004) arc; \draw[color=green!60!black, line width=5pt, start angle=60, end angle=90, radius=1.0cm] (4.25, -0.3839745962155614) arc; \end{pgfonlayer}{1}\end{tikzpicture}print(fig.generate_standalone())\documentclass[border=10pt]{standalone}\usepackage{tikz}\usepackage{pgfplots}\pgfplotsset{compat=newest}\usepgfplotslibrary{groupplots}\usetikzlibrary{arrows.meta}\begin{document}% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} % Define the layers library \pgfdeclarelayer{0} \pgfdeclarelayer{1} \pgfsetlayers{0,1}
% Layer 0 \begin{pgfonlayer}{0} \draw[color=blue, line width=5pt] (0, 0) circle (1.0cm); \draw[color=yellow!80!black, line width=5pt] (1.25, -1.25) circle (1.0cm); \draw[color=black, line width=5pt] (2.5, 0) circle (1.0cm); \draw[color=green!60!black, line width=5pt] (3.75, -1.25) circle (1.0cm); \draw[color=red, line width=5pt] (5, 0) circle (1.0cm); \end{pgfonlayer}{0}
% Layer 1 \begin{pgfonlayer}{1} \draw[color=blue, line width=5pt, start angle=330, end angle=360, radius=1.0cm] (0.8660254037844384, -0.5000000000000004) arc; \draw[color=yellow!80!black, line width=5pt, start angle=60, end angle=90, radius=1.0cm] (1.75, -0.3839745962155614) arc; \draw[color=black, line width=5pt, start angle=330, end angle=360, radius=1.0cm] (3.3660254037844384, -0.5000000000000004) arc; \draw[color=green!60!black, line width=5pt, start angle=60, end angle=90, radius=1.0cm] (4.25, -0.3839745962155614) arc; \end{pgfonlayer}{1}\end{tikzpicture}
\end{document}