在 tikZ 图中对齐节点文本

在 tikZ 图中对齐节点文本

我有以下 tikz 图片,但我不知道如何正确对齐左侧的字符(A、B、C、D、E)。我很确定有比我使用节点标签更好的方法。有什么想法吗?我一直迷失在 pgf tikz 文档中,SE 上的其他示例似乎没有适合我的解决方案……

干杯!F

\begin{tikzpicture}[scale=.75]
    \coordinate (y) at (0,6);
    \coordinate (x) at (11,0);
    \draw[<->, style=thick] (y) node[above left]{outcomes} -- (0,0) --  (x);

    \filldraw[red] (1,2) circle (5pt);
    \filldraw[red] (1,3) circle (5pt);
    \filldraw[red] (1,4) circle (5pt);

    \filldraw[blue] (2,1) circle (5pt);
    \filldraw[blue] (2,2) circle (5pt);
    \filldraw[blue] (2,3) circle (5pt);
    \filldraw[blue] (2,4) circle (5pt);

    \filldraw[red] (5,2) circle (5pt);
    \filldraw[red] (5,3) circle (5pt);
    \filldraw[red] (5,4) circle (5pt);

    \filldraw[blue] (6,2) circle (5pt);
    \filldraw[blue] (6,4) circle (5pt);

    \filldraw[red] (9,2) circle (5pt);
    \filldraw[red] (9,3) circle (5pt);
    \filldraw[red] (9,4) circle (5pt);

    \filldraw[blue] (10,3) circle (5pt);
    \filldraw[blue] (10,4) circle (5pt);
    \draw
        (0,1) node[left] {E}
        (0,2) node[left] {D}
        (0,3) node[left] {C}
        (0,4) node[left] {B}
        (0,5) node[left] {A}

        (1,0) node[below] {$\theta$}
        (2,0) node[below] {$\theta'_1$}
        (5,0) node[below] {$\theta$}
        (6,0) node[below] {$\theta'_2$}
        (9,0) node[below] {$\theta$}
        (10,0) node[below] {$\theta'_3$}
\end{tikzpicture}

答案1

如果问题是你是否可以使代码更短、更高效,答案是肯定的。

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[scale=.75,mark size=5pt]
    \coordinate (y) at (0,6);
    \coordinate (x) at (11,0);
    \draw[stealth-stealth, style=thick] (y) node[above left]{outcomes} -- (0,0) --  (x);
    \path[red] plot[only marks,mark=*] coordinates {
        (1,2) (1,3) (1,4) (5,2) (5,3) (5,4) (9,2) (9,3) (9,4)};
    \path[blue] plot[only marks,mark=*] coordinates {
        (2,1) (2,2) (2,3) (2,4) (6,2) (6,4) (10,3) (10,4)};
    \path foreach \X [count=\Y] in {A,...,E}
     {(0,6-\Y) node[left]{\X}}
     foreach \X in {1,2,3}
     {(4*\X-3,0) node[below] {$\theta$}
      (4*\X-2,0) node[below] {$\theta'_\X$}};
\end{tikzpicture}
\end{document}

在此处输入图片描述

显然,人们可以调整一些事物(arrows.meta等等),但我觉得你真正寻找的是 pgfplots。

这是我所阐述的评论。

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[scale=.75,mark size=5pt]
    \coordinate (y) at (0,6);
    \coordinate (x) at (11,0);
    \draw[stealth-stealth, style=thick] (y) node[above left]{outcomes} -- (0,0) --  (x);
    \path[red] plot[only marks,mark=*] coordinates {
        (1,2) (1,3) (1,4) (5,2) (5,3) (5,4) (9,2) (9,3) (9,4)};
    \path[blue] plot[only marks,mark=*] coordinates {
        (2,1) (2,2) (2,3) (2,4) (6,2) (6,4) (10,3) (10,4)};
    \path[nodes={text height=1.1em,text depth=0.25ex}] foreach \X [count=\Y] in {A,...,E}
     {(-2em,6-\Y) node[right,align=left]{\X}}
     foreach \X in {1,2,3}
     {(4*\X-3,0) node[below] {$\theta$}
      (4*\X-2,0) node[below] {$\theta'_\X$}};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容