TikZ 绘图太冗长

TikZ 绘图太冗长

我想知道是否有更简洁的方式来表达以下情节(我使用简单的格式):

\input tikz
\tikzpicture
  \draw[help lines, ystep=.5] (-.2,-10) grid (12.5,15);
  \draw[->] (0, -10) to (0, 15) node[above] {$y$};
  \draw[->] (0, 0) to (12.5, 0) node[right] {$x$};
  \draw (1,-.5) node {$1$};
  \draw (2,-.5) node {$2$};
  \draw (3,-.5) node {$3$};
  \draw (4,-.5) node {$4$};
  \draw (5,-.5) node {$5$};
  \draw (6,-.5) node {$6$};
  \draw (7,-.5) node {$7$};
  \draw (8,-.5) node {$8$};
  \draw (9,-.5) node {$9$};
  \draw (10,-.5) node {$10$};
  \draw (11,-.5) node {$11$};
  \draw (12,-.5) node {$12$};

  \draw (-.5,-10) node {$-20$};
  \draw (-.5,-9) node {$-18$};
  \draw (-.5,-8) node {$-16$};
  \draw (-.5,-7) node {$-14$};
  \draw (-.5,-6) node {$-12$};
  \draw (-.5,-5) node {$-10$};
  \draw (-.5,-4) node {$-8$};
  \draw (-.5,-3) node {$-6$};
  \draw (-.5,-2) node {$-4$};
  \draw (-.5,-1) node {$-2$};
  \draw (-.5,0) node {$0$};
  \draw (-.5,1) node {$2$};
  \draw (-.5,2) node {$4$};
  \draw (-.5,3) node {$6$};
  \draw (-.5,4) node {$8$};
  \draw (-.5,5) node {$10$};

  \draw (0,-10) circle (2pt);
  \draw (1,-9) circle (2pt);
  \draw (2,-8) circle (2pt);
  \draw (3,-7) circle (2pt);
  \draw (4,-6) circle (2pt);
  \draw (5,-5) circle (2pt);
  \draw (6,-4) circle (2pt);
  \draw (7,-3) circle (2pt);
  \draw (8,-2) circle (2pt);
  \draw (9,-1) circle (2pt);
  \draw (10,0) circle (2pt);
  \draw (11,1) circle (2pt);
  \draw (12,2) circle (2pt);
\endtikzpicture
\bye

它太大了,一页都放不下。

答案1

这是一个 LaTeX 版本。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw[help lines, ystep=.5] (-.2,-10) grid (12.5,15);
  \draw[->] (0, -10) to (0, 15) node[above] {$y$};
  \draw[->] (0, 0) to (12.5, 0) node[right] {$x$};
  \foreach \X in {0,...,12}
  {\ifnum\X>0
  \node at (\X,-0.5) {$\X$};
  \fi
  \draw (\X,-10+\X) circle (2pt); }
  \foreach \Y in {-20,-18,...,10}
  {\node at (-0.5,\Y/2) {$\Y$};}
\end{tikzpicture}
\end{document}

令人惊讶的是,以下内容确实贯穿始终pdftex

\input tikz
\tikzpicture
\draw[help lines, ystep=.5] (-.2,-10) grid (12.5,15);
  \draw[->] (0, -10) to (0, 15) node[above] {$y$};
  \draw[->] (0, 0) to (12.5, 0) node[right] {$x$};
  \foreach \X in {0,...,12}
  {\ifnum\X>0
  \node at (\X,-0.5) {$\X$};
  \fi
  \draw (\X,-10+\X) circle (2pt); }
  \foreach \Y in {-20,-18,...,10}
  {\node at (-0.5,\Y/2) {$\Y$};}
\endtikzpicture
\bye

答案2

因此,在花了一个晚上反复试验阅读手册之后,在多次郑重其事地祈求召唤超自然力量来对某事物造成伤害或惩罚之后,我终于 - 胜利地 - 形成了以下内容:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{datavisualization, datavisualization.formats.functions}
\begin{document}
\begin{tikzpicture}
  \datavisualization[
    school book axes
    , x axis={unit length=5mm}
    , y axis={
      unit length=5mm
      , ticks={step=2}
      , grid
      , scaling = min at 0cm and max at 5cm
    }
    , visualize as line
  ]
  data[format=function] {
    var x : interval [0:12];
    func y = (\value x - 10) * 2;
  };
\end{tikzpicture}
\end{document}

砰!砰!沙赞!蝙蝠侠,神圣的函数数据格式!不得不说,这真是太棒了。

我发现的唯一问题是,它不适用于普通格式,迫使用户下降到较低的启蒙层面。

相关内容