Tikz 节点大小太大

Tikz 节点大小太大

我目前正在可视化一个扩展窗口训练集(36 个月)、固定测试集​​(12 个月)和 1 个月的验证/预测集。我已设法通过缩小 tikz 图像本身将 60 个单独的月份放在一页上,但现在我仍无法缩小节点大小。

当前图像如下所示。

在此处输入图片描述

它有点小,但可以满足我想要实现的目的。但是,一旦节点编号开始超过 10,它看起来就很奇怪,因为数字之间的间距开始混乱。有人能解决这个问题吗?我当前的代码可以在下面找到。

\documentclass[border=2mm]{standalone}

\usepackage{tikz}
\usepackage{environ}
\makeatletter
\newsavebox{\measure@tikzpicture}
\NewEnviron{scaletikzpicturetowidth}[1]{%
  \def\tikz@width{#1}%
  \def\tikzscale{1}\begin{lrbox}{\measure@tikzpicture}%
  \BODY
  \end{lrbox}%
  \pgfmathparse{#1/\wd\measure@tikzpicture}%
  \edef\tikzscale{\pgfmathresult}%
  \BODY
}
\makeatother

\usetikzlibrary{decorations.pathreplacing}

\definecolor{myLightGray}{RGB}{191,191,191}
\definecolor{myGray}{RGB}{160,160,160}
\definecolor{myDarkGray}{RGB}{88,88,88}
\definecolor{myDarkRed}{RGB}{167,114,115}
\definecolor{myRed}{RGB}{255,58,70}
\definecolor{myGreen}{RGB}{0,255,71}

\begin{document}
\begin{scaletikzpicturetowidth}{\textwidth}
\begin{tikzpicture}[scale = \tikzscale]
% draw horizontal line   
\draw[->] (0,0) -- (60.7,0);

% draw vertical lines
\foreach \x in {0,1,...,60}{
    \draw (\x cm,8pt) -- (\x cm,0.5pt);
}

% place axis labels
\foreach \x in {0,1,...,60}{
    \node[anchor=north] at (\x,-1) {\tiny $\the\numexpr\x$};
}


% draw scale below
\foreach \x in {0,1,...,35}{
    \fill[myLightGray] (\x,-0.4) rectangle (\the\numexpr\x+1,-0.55);
}

\foreach \x in {36,37,...,47}{
    \fill[myDarkGray] (\x,-0.4) rectangle (\the\numexpr\x+1,-0.55);
}

\fill[myGreen] (48,-0.4) rectangle (49,-0.55);


% draw scale below
\foreach \x in {0,1,...,36}{
    \fill[myLightGray] (\x,-0.6) rectangle (\the\numexpr\x+1,-0.75);
}

\foreach \x in {37,38,...,48}{
    \fill[myDarkGray] (\x,-0.6) rectangle (\the\numexpr\x+1,-0.75);
}

\fill[myGreen] (49,-0.6) rectangle (50,-0.75);


% draw scale below
\foreach \x in {0,1,...,37}{
    \fill[myLightGray] (\x,-0.8) rectangle (\the\numexpr\x+1,-0.95);
}

\foreach \x in {38,39,...,49}{
    \fill[myDarkGray] (\x,-0.8) rectangle (\the\numexpr\x+1,-0.95);
}

\fill[myGreen] (50,-0.8) rectangle (51,-0.95);

\end{tikzpicture}
\end{scaletikzpicturetowidth}


\end{document}

答案1

我想这应该能给你想要的一切!

在此处输入图片描述

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usepackage{environ}
\makeatletter
\newsavebox{\measure@tikzpicture}
\NewEnviron{scaletikzpicturetowidth}[1]{%
  \def\tikz@width{#1}%
  \def\tikzscale{1}\begin{lrbox}{\measure@tikzpicture}%
  \BODY
  \end{lrbox}%
  \pgfmathparse{#1/\wd\measure@tikzpicture}%
  \edef\tikzscale{\pgfmathresult}%
  \BODY
}
\makeatother

\usetikzlibrary{decorations.pathreplacing}
\definecolor{myLightGray}{RGB}{191,191,191}
\definecolor{myGray}{RGB}{160,160,160}
\definecolor{myDarkGray}{RGB}{88,88,88}
\definecolor{myDarkRed}{RGB}{167,114,115}
\definecolor{myRed}{RGB}{255,58,70}
\definecolor{myGreen}{RGB}{0,255,71}

\begin{document}
\begin{scaletikzpicturetowidth}{\textwidth}
\begin{tikzpicture}[scale = \tikzscale]
% draw horizontal line   
\draw[->] (0,0) -- (60.7,0);

% draw vertical lines
\foreach \x in {0,1,...,60}{
    \draw (\x cm,8pt) -- (\x cm,0.5pt);
}
% place axis labels
\node[anchor=north] at (0,1.6) {\tiny $0$};
\node[anchor=north] at (12,1.6) {\tiny $1\!2$};
\node[anchor=north] at (24,1.6) {\tiny $2\!4$};
\node[anchor=north] at (36,1.6) {\tiny $3\!6$};
\node[anchor=north] at (48,1.6) {\tiny $4\!8$};
\node[anchor=north] at (49,1.6) {\tiny $4\!9$};
\node[anchor=north] at (50,1.6) {\tiny $5\!0$};
\node[anchor=north] at (51,1.6) {\tiny $5\!1$};
\node[anchor=north] at (52,1.6) {\tiny $5\!2$};
\node[anchor=north] at (53,1.6) {\tiny $5\!3$};
\node[anchor=north] at (54,1.6) {\tiny $5\!4$};
\node[anchor=north] at (55,1.6) {\tiny $5\!5$};
\node[anchor=north] at (56,1.6) {\tiny $5\!6$};
\node[anchor=north] at (57,1.6) {\tiny $5\!7$};
\node[anchor=north] at (58,1.6) {\tiny $5\!8$};
\node[anchor=north] at (59,1.6) {\tiny $5\!9$};
\node[anchor=north] at (60,1.6) {\tiny $6\!0$};

% draw scale below
\foreach \x in {0,1,...,35}{
    \fill[myLightGray] (\x,-0.1) rectangle (\the\numexpr\x+1,-0.55);
}

\foreach \x in {36,37,...,47}{
    \fill[myDarkGray] (\x,-0.1) rectangle (\the\numexpr\x+1,-0.55);
}

\fill[myGreen] (48,-0.1) rectangle (49,-0.55);

% draw scale below
\foreach \x in {0,1,...,36}{
    \fill[myLightGray] (\x,-0.65) rectangle (\the\numexpr\x+1,-1.1);
}

\foreach \x in {37,38,...,48}{
    \fill[myDarkGray] (\x,-0.65) rectangle (\the\numexpr\x+1,-1.1);
}

\fill[myGreen] (49,-0.65) rectangle (50,-1.1);

% draw scale below
\foreach \x in {0,1,...,37}{
    \fill[myLightGray] (\x,-1.2) rectangle (\the\numexpr\x+1,-1.65);
}

\foreach \x in {38,39,...,49}{
    \fill[myDarkGray] (\x,-1.2) rectangle (\the\numexpr\x+1,-1.65);
}

\fill[myGreen] (50,-1.2) rectangle (51,-1.65);

\draw[decorate,decoration={brace,amplitude=5pt}] (36,-2) -- (0,-2)
node[anchor=north,midway,below=4pt] {Training Set}; 

\draw[decorate,decoration={brace,amplitude=5pt}] (48,-2) -- (36,-2)
    node[anchor=north,midway,below=4pt] {Test Set}; 

\draw [<-] (49,-0.35) -- (54,-0.35) -- (54,-2.6);]

\draw[white,decorate,decoration={brace,amplitude=5pt}] (60,-2) -- (48,-2)
    node[text=black,anchor=north,midway,below=4pt] {Prediction}; 
\end{tikzpicture}
\end{scaletikzpicturetowidth}
\end{document}

相关内容