Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created April 24, 2024 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abikoushi/2736cc47be3c3d81ace6f59b53baa2b1 to your computer and use it in GitHub Desktop.
Save abikoushi/2736cc47be3c3d81ace6f59b53baa2b1 to your computer and use it in GitHub Desktop.
TikZ example: matrix with annotation
\documentclass[tikz,border=14pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,decorations.pathreplacing, backgrounds, positioning}
\begin{document}
\begin{tikzpicture}[
%Styles
Matrix/.style={
matrix of nodes,
text height=2.5ex,
text depth=0.75ex,
text width=8.0ex,
align=center,
column sep=0pt, row sep=0pt,
nodes={draw=black!30},
nodes in empty cells,
}
]
%mat 1
\matrix[Matrix, nodes = {fill= cyan!10}] at (0,0) (mat1){
& &\\
& &\\
& &\\
};
\matrix[Matrix, xshift = 5ex, left = of mat1] (ann_row1){
\\
\\
\\
};
\matrix[Matrix, yshift = -5ex, above = of mat1](ann_col1){
& $\bullet$ & $\dagger$ \\
};
% mat 2
\matrix[Matrix, below right = of mat1.south east, nodes = {fill= cyan!10}](mat2){
& & &\\
& & & \\
};
\matrix[Matrix, yshift = -5ex, above = of mat2](ann_col2){
& $\bullet$& &$\dagger$\\
};
\matrix[Matrix, xshift = 5ex, left = of mat2] (ann_row2){
\\
\\
};
%row index
\node[yshift = 5ex](lab2) at (ann_col1.north){feature};
\path[draw, ->] ([xshift = -10ex, yshift = 2ex]ann_col1.north west) -- ([xshift = +40ex, yshift = 2ex]ann_col1.north east);
%column index
\node[rotate=90, yshift=5ex](lab1) at (ann_row1.west){sample};
\path[draw, ->] ([yshift = 5ex, xshift = -2ex]ann_row1.north west) -- ([yshift = -15ex, xshift = -2ex]ann_row1.south west);
%%%%
%data frame
\matrix[Matrix, right = of mat2.east, yshift = 15ex, xshift=5ex] (tM){ % Matrix contents
1& 1 & & \\
2& 1 & & \\
\vdots& \vdots & \vdots & \\
5& 4 & & \\
1& 2 & $\bullet$ & \\
\vdots & \vdots & \vdots & \\
5& 2 & $\bullet$ & \\
1& 3 & $\dagger$ & \\
\vdots & \vdots& \vdots & \\
5& 7 & $\dagger$ & \\
};
\node[above=of tM](tidyfmt){\Large tidy format};
\node[yshift = 10pt](lab1t) at (tM-1-1.north){\footnotesize sample};
\node[yshift = 10pt](lab2t) at (tM-1-2.north){\footnotesize feature};
\node[yshift = 10pt](lab2t) at (tM-1-3.north){\footnotesize annotation};
\path[decorate, decoration ={brace, mirror}, draw]
(tM-10-1.south west) -- (tM-10-3.south east) node[midway, yshift = -10pt]{$\xi$};
\path[decorate, decoration ={brace, mirror}, draw]
(tM-10-4.south west) -- (tM-10-4.south east) node[midway, yshift = -10pt]{$y$};
\begin{scope}[on background layer]
\foreach \x in {1,...,10}
\fill [cyan!10] (tM-\x-4.south west) rectangle (tM-\x-4.north east);
\end{scope}
\end{tikzpicture}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment