如何在树形图中添加 Y 轴和图例?

如何在树形图中添加 Y 轴和图例?

我正在尝试在下面的图表中添加两件事:

  1. 传说只是说 W:浪费,S:盒装出售
  2. 添加 Y 轴作为时间,用花括号指定周期

任何关于如何去做的建议/链接

短暂性脑缺血发作

\begin{figure}[h]
 \hfill\begin{minipage}{.9\textwidth}\centering
  \begin{tikzpicture}[every tree node/.style={draw,circle},
   level distance=1.75cm,sibling distance=1.2cm, 
   edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\Tree [.\node {Q}; 
        \edge node[auto=right] {Period 1};
    [.$Q_1$  
        [.$W$ ] [.$S$ ]
    ]
    [.$Q_i$
        \edge node[auto=right] {Period i};
      [.$Q_2$ 
            \edge node[auto=right] {Period 2};
         [.$W$ ] [.$S$ ]
      ] 
      [.$W$ ]
    ] 
       ]
\end{tikzpicture}
  \caption{\texttt{Inventory Carryover }}
 \end{minipage}
\end{figure}

答案1

这使用了 Forest,我对此很熟悉。毫无疑问,您可以根据您使用的任何软件包对其进行调整,以使您的代码正常工作。(可能tikz-qtree在您的序言中,即使您已对其进行标记,tikz-trees这表明 TiZ 自己的图书馆。)

然而,Forest 更加强大和灵活,这无疑让事情变得更容易一些。

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage{forest,array}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{forest}
  for tree={
    circle,
    draw,
    math content,
    l sep*=2,
    s sep*=3,
    minimum size=2.5em,
    fit=band,
    tier/.option=level,
  },
  period/.style={
    edge label={node [midway, left, anchor=south east] {Period $#1$}},
  },
  before drawing tree={
    tempcounta/.max={>O{level}}{tree},
    tikz+={
      \coordinate (w) at ([xshift=-10pt]current bounding box.west);
      \foreach \i [count=\j from 0] in {1,...,\foresteregister{tempcounta}} \draw [decorate, decoration={brace, amplitude=5pt}] (!{group={r,until={>On={level}{\i}}{next node}}} -| w) -- (!{group={r,until={>On={level}{\j}}{next node}}} -| w) node [midway, left, xshift=-5pt] {Time Interval \i} ;
    },
  },
  [Q
    [Q_1, period=1
      [W]
      [S]
    ]
    [Q_i
      [Q_2, period=i
        [W, period=2]
        [S]
      ]
      [W]
    ]
  ]
  \node [anchor=north west] at (current bounding box.north east) {%
    \begin{tabular}{@{}>{$}l<{$}@{}>{:}c@{\hskip 5pt}l@{}}
      \multicolumn{3}{@{}l@{}}{Legend}\\
      S && Sold in a box\\
      W && Wastage\\
    \end{tabular}};
\end{forest}
\end{document}

时间树

相关内容