TIKZ 图表中的高度错误

TIKZ 图表中的高度错误

我有以下内容:

\begin{tikzpicture}
  \def\xmax{110}
  \def\ymax{1}

  \def\q{10} % Normalization
  \def\ha{101}
  \def\hb{210}
  \def\hc{340}
  \def\hd{553}
  \def\he{701}
  \def\hf{998}
  \def\hg{1023}

  \def\aa{200/654}
  \def\ab{200/780}
  \def\ac{200/1278}
  \def\ad{200/888}
  \def\ae{200/1782}
  \def\af{200/300}
  \def\ag{200/1212}

  \begin{scope}[yshift=-2.7cm, domain=0:\xmax, >=latex, yscale=4, xscale=0.11, samples=150]
    \draw[very thin,color=gray,xstep=10.0cm,ystep=0.3cm] (0,0) grid (\xmax,1.2);
    \draw[thick, ->] (0,0) -- (\xmax.1,0) node[right] {$x$};
    \draw[thick, ->] (0,0) -- (0,1.2) node[above] {$f(x)$};

    \draw[thick, color=red, domain=0:\hg/\q]   plot (\x,{
        (\x>\ha/\q)*(\aa) + 
        and(\x>\hb/\q,\x<=\hc/\q)*(\ab) +
        and(\x>\hc/\q,\x<=\hd/\q)*(\ac) +
        and(\x>\hd/\q,\x<=\he/\q)*(\ad) +
        and(\x>\he/\q,\x<=\hf/\q)*(\ae) +
        and(\x>\hf/\q,\x<=\hg/\q)*(\af) +
        (\x<=\ha/\q)*(\ag)})   node[right] {$f(x)$};

    \draw[dashed] (\ha/\q,0) node[below] {$h_1$} -- (\ha/\q,\aa) node[left] {$A_1$};
    \draw[dashed] (\hb/\q,0) node[below] {$h_2$} -- (\hb/\q,\ab) node[left] {$A_2$};
    \draw[dashed] (\hc/\q,0) node[below] {$h_3$} -- (\hc/\q,\ac) node[left] {$A_3$};
    \draw[dashed] (\hd/\q,0) node[below] {$h_4$} -- (\hd/\q,\ad) node[left] {$A_4$};
    \draw[dashed] (\he/\q,0) node[below] {$h_5$} -- (\he/\q,\ae) node[left] {$A_5$};
    \draw[dashed] (\hf/\q,0) node[below] {$h_6$} -- (\hf/\q,\af) node[left] {$A_{6,1}$};
    \draw[dashed] (\hg/\q,0) node[below] {$h_\text{M}$} -- (\hg/\q,\ag) node[left] {$A_{6,2}$};
  \end{scope}

\end{tikzpicture}

它显示了一个分段函数。在每个断点处,我想显示一条到达函数值的垂直线,因此是最后一行。然而,我得到的是:

在此处输入图片描述

x 位移正确,但 y 位移不正确(只有 y 位移才A1正确)。我可以看到函数有错误值,因为我们有这个值,A1 < A2 < A3但这种情况并没有发生。这是怎么回事?

答案1

我认为pgfplots它比普通的更适合绘图TikZ

输出

在此处输入图片描述

代码

\documentclass[12pt,tikz,border=0pt]{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
\pgfplotsset{compat=1.14}
\begin{filecontents*}{table.dat}
X       Y       label
0       654     
101     780     $A_1$
210     1278    $A_2$
340     888     $A_3$
553     1782    $A_4$
701     300     $A_5$
998     1212    $A_{6,1}$
1023    1212    {}
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
  \begin{axis}
    [
      clip=false,
      height=10cm,
      axis lines=center,
      xmajorgrids=false, xminorgrids=true,
      ymajorgrids=false, yminorgrids=true,
      xlabel={$x$},    x label style={anchor=west},  xticklabels={},
      ylabel={$f(x)$}, y label style={anchor=south}, yticklabels={},
      xtick=data,
      hide obscured x ticks=false,
      xticklabels={0,$h_1$,$h_2$,$h_3$,$h_4$,$h_5$,$h_6$},
      minor xtick={0,10,...,100},
      minor ytick={0,.1,...,1},
      ymin=0,
      enlarge x limits={value=.1,upper},
      enlarge y limits={value=.1,upper},
    ]

    \addplot
    [
      ycomb,
      dashed,
      thick,
      gray,
      nodes near coords,
      nodes near coords style=
      {
        anchor=south west,
        black,
        %fill=white,
      },
      point meta=explicit symbolic,
    ]
        table [x expr=\thisrow{X}/10, y expr=200/\thisrow{Y}, meta=label] {table.dat};

    \addplot[const plot, red, very thick] 
        table [x expr=\thisrow{X}/10, y expr=200/\thisrow{Y}] {table.dat}
        node [pos=.8] {$y=f(x)$};

  \end{axis}
\end{tikzpicture}
\end{document}

相关内容