如何用 tikz 制作此图

如何用 tikz 制作此图

这里,我能够在同一条线上绘制图形并正确对齐它们。我该如何绘制它?在此处输入图片描述

答案1

如果您研究上一个问题的答案中的代码,那么您可以使用普通的 TikZ 轻松绘制图形。

在此处输入图片描述

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[>=stealth,thick]
\def\a{3}
\draw[<-,shift={(0,\a)}] (-1,0)--(1,0) node[midway,above=2mm]{stabilizing};
\draw[->,shift={(0,-\a)}] (-1,0)--(1,0) node[midway,below=2mm]{contracting};
\draw[->,shift={(\a,0)}] (0,-.25)--(0,.25) node[midway,right]{turn on $p_{n+1}$};
\draw[<-,shift={(-\a,0)}] (0,-.25)--(0,.25) node[midway,left]{turn off $p_{n+1}$};

\begin{scope}[shift={(-\a,\a)},scale=.6]
\draw (-2,1)--(2,1) (1,2)--(1,-2) (-1,2)--(-1,-2);
\foreach \p in {(-1,0),(1,0),(0,1),(1,-1),(-1,-1)}
\fill[magenta] \p circle(1mm);
\path 
(0,1) node[above]{$p_{n+1}$}
(0,-2) node[below]{stable};
\end{scope}

\begin{scope}[shift={(\a,\a)},scale=.6]
\draw (-2,1)--(2,1) (1,2)--(1,-2) (-1,2)--(-1,-2);
\foreach \p in {(-1,0),(1,0),(1,-1),(-1,-1)}
\fill[magenta] \p circle(1mm);
\draw[magenta] 
(0,1) circle(1mm) node[above,black]{$q$}
(0,-2) node[below,black]{unstable};
\end{scope}

\pgfmathsetmacro{\b}{sqrt(3)/3}

\begin{scope}[shift={(-\a,-\a)},scale=.6]
\draw (\b,2)--(-3*\b,-2) (-\b,2)--(3*\b,-2);
\foreach \p in {(0,1),(\b,0),(-\b,0),(2*\b,-1),(-2*\b,-1)}
\fill[magenta] \p circle(1mm);
\path 
(0,1) node[right=1mm,black]{$p_{n+1}$}
(0,-2) node[below,black]{unstable};
\end{scope}

\begin{scope}[shift={(\a,-\a)},scale=.6]
\draw (\b,2)--(-3*\b,-2) (-\b,2)--(3*\b,-2);
\foreach \p in {(\b,0),(-\b,0),(2*\b,-1),(-2*\b,-1)}
\fill[magenta] \p circle(1mm);
\draw[magenta] 
(0,1) circle(1mm) node[right=1mm,black]{$q$}
(0,-2) node[below,black]{stable};
\end{scope}

\end{tikzpicture}
\end{document}

答案2

如果您必须多次绘制同一张图,我建议使用pics。它们可以排列成矩阵。(这是一个“普通”矩阵,不需要库matrix。)(查看Black Mild 的精彩回答我意识到我犯了错误,现在我已改正。)

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[pics/shelter/.style={code={%
 \draw (-1,0.5) -- (1,0.5) coordinate[pos=0.5] (-t) (0,0) coordinate (-center);
 \draw (-0.5,1) -- (-0.5,-1) node[pos=0.5,bullet]{} node[pos=0.75,bullet]{};
 \draw (0.5,1) -- (0.5,-1) node[pos=0.5,bullet]{} node[pos=0.75,bullet]{};
}},pics/tent/.style={code={\begin{scope}[local bounding box=-bbox]
 \draw (-1/3,1) -- (4/3,-1) node[pos=0.5,bullet]{} node[pos=0.75,bullet]{}
  (1/3,1) -- (-4/3,-1)node[pos=0.5,bullet]{} node[pos=0.75,bullet]{} 
  (0,0.6) coordinate (-t) (0,0) coordinate (-center);
\end{scope}}},
bullet/.style={circle,fill,inner sep=1.5pt},
empty/.style={circle,draw,fill=white,inner sep=1.5pt},
shorten/.style={shorten >=#1/2,shorten <=#1/2}]
 \matrix[column sep=5em,row sep=4em]{
  \pic (S1) {shelter}; &  \pic (T1) {tent};\\
  \pic (S2) {shelter}; &  \pic (T2) {tent};\\
 };
 \path (S1-t) node[bullet,label=above:$p_{n+1}$]{}
  (T1-t) node[bullet,label=right:$p_{n+1}$]{}
  (S2-t) node[empty,label=above:$q$]{}
  (T2-t) node[empty,label=right:$q$]{}
  node[node font=\tiny,below=1cm of S1-center]{stable}
  node[node font=\tiny,below=1cm of T1-center]{unstable}
  node[node font=\tiny,below=1cm of T2-center]{stable}
  node[node font=\tiny,below=1cm of S2-center]{unstable};
 \draw[-latex,shorten=2.4cm] (S1-center) -- (T1-center) node[midway,above]{stabilizing};
 \draw[-latex,shorten=2.4cm] (S2-center) -- (T2-center) node[midway,above]{contracting};
 \draw[-latex,shorten=3cm] (S1-center) -- (S2-center) node[midway,left]{turn off $p_{n+1}$};
 \draw[-latex,shorten=3cm] (T2-center) -- (T1-center) node[midway,right]{turn on $p_{n+1}$};;
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容