我在板图中有一些节点需要不同的背景颜色。理想情况下,整个图应该是彩色的:
梅威瑟:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{bayesnet}
\begin{tikzpicture}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\node[obs] (x) {$x_i$};
\node[latent, above=15pt of x] (z) {$z_i$};
\node[latent, above left=5pt and 25pt of z] (pi) {$\pi$};
\node[latent, below left=5pt and 25pt of x] (theta) {$\theta$};
\edge{pi}{z};
\edge{theta}{x};
\edge{z}{x};
{\tikzset{plate caption/.append style={below right=1pt and 1pt of #1.south east}}
\plate[inner sep=0.35cm]{zwn}{(z)(x)}{\scriptsize{$10$}};}
\begin{pgfonlayer}{background}
% this needs to be something like "<10pt above left of pi> rectangle <50pt below right of z>"
\fill [orange!20] (pi) rectangle (z);
% this needs to be something like "<10pt below left of theta> rectangle <50pt above right of x>"
\fill [purple!20] (theta) rectangle (x);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
输出:
我想要的是:
答案1
欢迎!您可以使用节点锚点,并在需要时添加额外的班次。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{bayesnet}
\begin{document}
\begin{tikzpicture}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\node[obs] (x) {$x_i$};
\node[latent, above=15pt of x] (z) {$z_i$};
\node[latent, above left=5pt and 25pt of z] (pi) {$\pi$};
\node[latent, below left=5pt and 25pt of x] (theta) {$\theta$};
\edge{pi}{z};
\edge{theta}{x};
\edge{z}{x};
\tikzset{plate caption/.append style={below right=1pt and 1pt of #1.south east}}
\plate[inner sep=0.35cm]{zwn}{(z)(x)}{\scriptsize{$10$}};
\begin{pgfonlayer}{background}
% this needs to be something like "<10pt above left of pi> rectangle <50pt below right of z>"
\fill [orange!20] ([xshift=-2ex,yshift=2ex]pi.north west)
rectangle ([xshift=1ex]zwn.east);
% this needs to be something like "<10pt below left of theta> rectangle <50pt above right of x>"
\fill [purple!20] ([xshift=-2ex,yshift=-2ex]theta.south west)
rectangle ([xshift=1ex]zwn.east);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}