tikz 的几个问题

tikz 的几个问题

晚上好,我对 LaTeX 和 Tikz 还比较陌生,需要你的帮助。我已经创建了一个图形,并且取得了比较大的进展,但仍有三个未解决的问题。它们是:

  1. 如何让标题从此处所示的相同位置开始(https://ibb.co/8cqxWnr),即从同一个 x 坐标开始?

  2. 我在顶部添加了一个图例,其中包含 5 个条目,其中一个条目代表中间。但是,我现在想添加第 6 个条目。我该怎么做?新条目应具有以下模式:

    \填充[图案=网格,图案颜色=黑色]

  3. 我想添加一个像这里显示的框(https://ibb.co/sKbKTL7) 到我的图表的右侧。我需要为此添加什么代码?

这是我的代码:谢谢!

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{document}
\newcommand{\myScheduleStuff}{
    \fill[pattern=north east lines,pattern color=teal, draw=black] (.3,0) rectangle (1.3,1);
    \fill[pattern=vertical lines,pattern color=orange, draw=black] (1.4,0) rectangle (2.4,1);
    \fill[pattern=north west lines,pattern color=violet, draw=black] (2.5,0) rectangle (3.5,1);
    \draw (.2,-.5)--(.2,.5);
    \draw (3.6,-.5)--(3.6,.5);
    \node at (1.9,-.3) () {\bfseries Day 1};
    
    \fill[pattern=north east lines,pattern color=teal, draw=black] (3.7,0) rectangle (4.7,1);
    \fill[pattern=vertical lines,pattern color=orange, draw=black] (4.8,0) rectangle (5.8,1);
    \fill[pattern=north west lines,pattern color=violet, draw=black] (5.9,0) rectangle (6.9,1);
    \draw (.2,-.5)--(.2,.5);
    \draw (3.6,-.5)--(3.6,.5);
    \node at (5.3,-.3) () {\bfseries Day 2};
    
    \fill[pattern=north east lines,pattern color=teal, draw=black] (7.1,0) rectangle (8.1,1);
    \fill[pattern=vertical lines,pattern color=orange, draw=black] (9.3,0) rectangle (10.3,1);
    \fill[pattern=north west lines,pattern color=violet, draw=black] (9.3,1) rectangle (10.3,2);
    \draw (10.4,-.5)--(10.4,.5);
    \draw (7,-.5)--(7,.5);
    \node at (8.7,-.3) () {\bfseries Day 3};

    \fill[pattern=north east lines,pattern color=teal, draw=black] (10.5,0) rectangle (11.5,1);
    \fill[pattern=vertical lines,pattern color=orange, draw=black] (12.7,0) rectangle (13.7,1);
    \fill[pattern=north west lines,pattern color=violet, draw=black] (12.7,1) rectangle (13.7,2);
    \draw (10.4,-.5)--(10.4,.5);
    \draw (13.8,-.5)--(13.8,.5);
    \node at (12.1,-.3) () {\bfseries Day 4};

    \fill[pattern=north east lines,pattern color=teal, draw=black] (15,0) rectangle (16,1);
    \fill[pattern=vertical lines,pattern color=orange, draw=black] (13.9,0) rectangle (14.9,1);
    \fill[pattern=north west lines,pattern color=violet, draw=black] (13.9,1) rectangle (14.9,2);
    \draw (17.2,-.5)--(17.2,.5);
    \draw (13.8,-.5)--(13.8,.5);
    \node at (15.5,-.3) () {\bfseries Day 5};

    \fill[pattern=north east lines,pattern color=teal, draw=black] (17.3,0) rectangle (18.3,1);
    \fill[pattern=vertical lines,pattern color=orange, draw=black] (18.4,0) rectangle (19.4,1);
    \fill[pattern=north west lines,pattern color=violet, draw=black] (19.5,0) rectangle (20.5,1);
    \draw (17.2,-.5)--(17.2,.5);
    \draw (20.6,-.5)--(20.6,.5);
    \node at (18.9,-.3) () {\bfseries Day 6};

    \fill[pattern=north east lines,pattern color=teal, draw=black] (20.7,0) rectangle (21.7,1);
    \fill[pattern=vertical lines,pattern color=orange, draw=black] (20.7,1) rectangle (21.7,2);
    \fill[pattern=north west lines,pattern color=violet, draw=black] (22.9,0) rectangle (23.9,1);
    \draw (24,-.5)--(24,.5);
    \draw (20.6,-.5)--(20.6,.5);
    \node at (22.3,-.3) () {\bfseries Day 7};
    
    % Achse
    \draw[-latex,line width=2pt] (0,0)--(25,0);
    }


\begin{tikzpicture}
\myScheduleStuff
        
 % Legende
\node (leg) at (12,4) {\textbf{Legend}}; % Random position of the center of the legend
\node[anchor=north] (w2leg) at (leg.south) {\tikz{\fill[pattern=north west lines,pattern color=violet] (0,0) rectangle (0.4,0.4);} Worker 3};

\node[anchor=east,left of= w2leg,node distance=2.5cm] (w1leg) {\tikz{\fill[pattern=vertical lines,pattern color=orange] (0,0) rectangle (0.4,0.4);} Worker 2};

\node[anchor=west,right of= w2leg,node distance=2.5cm] (w3leg) {\tikz{\fill[pattern=crosshatch dots,pattern color=grey] (0,0) rectangle (0.4,0.4);} Overstaffing};

\node[anchor=west,left of= w1leg,node distance=2.5cm] (w4leg) {\tikz{\fill[pattern=north east lines,pattern color=teal] (0,0) rectangle (0.4,0.4);} Worker 1};

\node[anchor=west,right of= w3leg,node distance=2.5cm] (w5leg) {\tikz{\fill[pattern=crosshatch dots,pattern color=red] (0,0) rectangle (0.4,0.4);} Understaffing};

\node[anchor=west,right of= w3leg,node distance=2.5cm] (w5leg) {\tikz{\fill[pattern=grid,pattern color=black] (0,0) rectangle (0.4,0.4);} Performance decrease};

\draw (w4leg.south west) rectangle (leg.north -| w5leg.east); % Using corners here
\draw (0,-1) -- ++(25,0); % Separation line
\begin{scope}[yshift=-4cm]
\myScheduleStuff
\end{scope}

\draw (0,-5) -- ++(25,0); % Separation line

\node at (1.5,3.8) () {\bfseries Demand pattern};
\node at (1.5,-1.5) () {\bfseries Classic approach};
\node at (1.5,-5.5) () {\bfseries Behaviour-aware approach};

\begin{scope}[yshift=-8cm]
\end{scope}

\end{tikzpicture}
\end{document}

答案1

这里有一个可能的解决方案。1.- 定义tikzstyles以保存输入 2.- 使用positioning库轻松放置元素 3.- 避免嵌套tikzpictures(在图例中)4.- 在图表右侧添加箭头和框

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns, positioning, fit, shapes.arrows}

\tikzset{
    pteal/.style={pattern=north east lines, pattern color=teal},
    porange/.style={pattern=vertical lines, pattern color=orange},
    pviolet/.style={pattern=north west lines, pattern color=violet},
    pred/.style={pattern=crosshatch dots, pattern color=red},
    pgray/.style={pattern=crosshatch dots, pattern color=gray},
    pblack/.style={pattern=grid, pattern color=black}
    }

\begin{document}
\newcommand{\myScheduleStuff}{
    \fill[draw,pteal] (.3,0) rectangle (1.3,1);
    \fill[draw,porange] (1.4,0) rectangle (2.4,1);
    \fill[draw,pviolet] (2.5,0) rectangle (3.5,1);
    \draw (.2,-.5)--(.2,.5);
    \draw (3.6,-.5)--(3.6,.5);
    \node at (1.9,-.3) () {\bfseries Day 1};
    
    \fill[draw,pteal] (3.7,0) rectangle (4.7,1);
    \fill[draw,porange] (4.8,0) rectangle (5.8,1);
    \fill[draw,pviolet] (5.9,0) rectangle (6.9,1);
    \draw (.2,-.5)--(.2,.5);
    \draw (3.6,-.5)--(3.6,.5);
    \node at (5.3,-.3) () {\bfseries Day 2};
    
    \fill[draw,pteal] (7.1,0) rectangle (8.1,1);
    \fill[draw,porange] (9.3,0) rectangle (10.3,1);
    \fill[draw,pviolet] (9.3,1) rectangle (10.3,2);
    \draw (10.4,-.5)--(10.4,.5);
    \draw (7,-.5)--(7,.5);
    \node at (8.7,-.3) () {\bfseries Day 3};

    \fill[draw,pteal] (10.5,0) rectangle (11.5,1);
    \fill[draw,porange] (12.7,0) rectangle (13.7,1);
    \fill[draw,pviolet] (12.7,1) rectangle (13.7,2);
    \draw (10.4,-.5)--(10.4,.5);
    \draw (13.8,-.5)--(13.8,.5);
    \node at (12.1,-.3) () {\bfseries Day 4};

    \fill[draw,pteal] (15,0) rectangle (16,1);
    \fill[draw,porange] (13.9,0) rectangle (14.9,1);
    \fill[draw,pviolet] (13.9,1) rectangle (14.9,2);
    \draw (17.2,-.5)--(17.2,.5);
    \draw (13.8,-.5)--(13.8,.5);
    \node at (15.5,-.3) () {\bfseries Day 5};

    \fill[draw,pteal] (17.3,0) rectangle (18.3,1);
    \fill[draw,porange] (18.4,0) rectangle (19.4,1);
    \fill[draw,pviolet] (19.5,0) rectangle (20.5,1);
    \draw (17.2,-.5)--(17.2,.5);
    \draw (20.6,-.5)--(20.6,.5);
    \node at (18.9,-.3) () {\bfseries Day 6};

    \fill[draw,pteal] (20.7,0) rectangle (21.7,1);
    \fill[draw,porange] (20.7,1) rectangle (21.7,2);
    \fill[draw,pviolet] (22.9,0) rectangle (23.9,1);
    \draw (24,-.5)--(24,.5);
    \draw (20.6,-.5)--(20.6,.5);
    \node at (22.3,-.3) () {\bfseries Day 7};
    
    % Achse
    \draw[-latex,line width=2pt] (0,0)--(25,0) coordinate (EndofAxis);
    \node[single arrow,draw, right=2mm of EndofAxis] (arrow) {};
    \matrix[right=5mm of arrow, draw, dashed, ampersand replacement=\&,column sep=1mm,
    label={Shift changes}]{%
\node[fill=red!30,text=red,]{3};\&\node[fill=blue!30]{2};\&\node[fill=green!30]{2};\\};
    }


\begin{tikzpicture}
\myScheduleStuff
        
 % Legende
\node (leg) at (12,4) {\textbf{Legend}}; % Random position of the center of the legend

\node[minimum size=4mm, pviolet, label=right:Worker 3, below left=3mm and 10mm of leg] (w3leg) {};

\node[minimum size=4mm, porange, label=right:Worker 2, left=2cm of w3leg] (w2leg) {};

\node[minimum size=4mm, pteal, label=right:Worker 1, left=2cm of w2leg] (w1leg) {};

\node[minimum size=4mm, pgray, label=right:Overstaffing, right=2cm of w3leg] (w4leg) {};

\node[minimum size=4mm, pred, label=right:Understaffing, right=2.5cm of w4leg] (w5leg) {};

\node[minimum size=4mm, pblack, label={[name=pd]right:Performance decrease}, right=2.5cm of w5leg] (w6leg) {};

\node[draw, fit= (pd) (leg) (w1leg)]{}; % Using corners here
\draw (0,-1) -- ++(25,0); % Separation line
\begin{scope}[yshift=-4cm]
\myScheduleStuff
\end{scope}

\draw (0,-5) -- ++(25,0); % Separation line

\node[right, font=\bfseries] at (0,3.8) () {Demand pattern};
\node[right, font=\bfseries] at (0,-1.5) () {Classic approach};
\node[right, font=\bfseries] at (0,-5.5) () {Behaviour-aware approach};

\begin{scope}[yshift=-8cm]
\end{scope}

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

一些建议:

  • 允许一些border独立
  • 使用gray
  • 此处的哲学:1 个问题 <=> 1 个难题(不多;这就是为什么您看到关闭该问题的需求needs more focus
\documentclass[10pt,border=3mm]{standalone}% <<<<<
...
\node[anchor=west,right of= w2leg,node distance=2.5cm] (w3leg) {\tikz{\fill[pattern=crosshatch dots,pattern color=gray] (0,0) rectangle (0.4,0.4);} Overstaffing};% <<< not grey

一些解决方案:

  • 使用锚点对齐文本节点,就像之前所做的那样(或者至少在代码中存在)
  • 参见 pgfmanual (版本 3.1.9a) 中的第 17.5.1 章

%\node at (1.5,3.8) () {\bfseries Demand pattern};
%\node  at (1.5,-1.5) () {\bfseries Classic approach};
%\node  at (0,-5.5) () {\bfseries Behaviour-aware approach};

\draw (0,3.8)  node [anchor=west] {\bfseries Demand pattern};
\draw (0,-1.5) node [anchor=west] {\bfseries Classic approach};
\draw (0,-5.5) node [anchor=west] {\bfseries Behaviour-aware approach};

在此处输入图片描述

相关内容