Tikz“限制 y 到域”未按预期工作

Tikz“限制 y 到域”未按预期工作

我正在尝试使用 Tikz 绘制一个图。我需要在线上放置一个标签,最好位于图的右上角,并且与斜率相同的线上。

\documentclass[border=10pt]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}\pgfplotsset{compat=1.9}


\begin{document}

\begin{tikzpicture}
\begin{axis}[
    restrict y to domain=256:1204,
    xmin=1990,
    xmax=2021,
    ymin=256,
    ymax=1024,
    enlarge y limits={rel=0.13},
    enlarge x limits={rel=0.07},
    xtick={1990,1995,2000,2005,2010,2015,2020},
    xticklabel style={/pgf/number format/1000 sep=,font=\scriptsize,semithick},
    extra x ticks={1991,1992,1993,1994,1996,1997,1998,1999,2001,2002,2003,2004,2006,2007,2008,2009,2010,2011,2012,2013,2014,2016,2017,2018,2019,2021},
    extra x tick labels={},
    extra x tick style=
    {
        tick label style={red, below, yshift=0.5ex},
        major tick length=0.5ex,
        tick pos= bottom,
        grid=none
    },
    ytick={256,384,512,640,768,896,1024},
    yticklabel style={/pgf/number format/1000 sep=,font=\scriptsize},
    extra y ticks={288,320,352,416,448,480,544,576,608,672,704,736,800,832,864,928,960,992,1024,1056,1088},
    extra y tick labels={},
    extra y tick style=
    {
    tick label style={red, below,yshift=-1ex},
    major tick length=0.5ex,
    tick pos= left,
    grid=none
    },
    xlabel={Year},
    ylabel={Digits},
    grid = major,
]
\addplot +[domain=1990:2020,samples=2] {x-2000)*32+521} node {$x-2000)*32+521$};

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

当我使用时,restrict y to domain=256:1204该线消失了。我需要将其设置为 25:2048 才能看到该线。

在此处输入图片描述

  1. 如何限制线路以使其留在里面。
  2. 如何如图所示添加标签。

答案1

restrict y to domain=256:1204,我想说的是,如果你有更严格的限制,你不需要使用ymin=256, ymax=1024。你不需要增加样本,也似乎restrict y to domain=256:1204,没有任何目的。只需使用no marks和适合pos节点的

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}% <- 1.16 would be better
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmin=1990,
    xmax=2021,
    ymin=256,
    ymax=1024,
    enlarge y limits={rel=0.13},
    enlarge x limits={rel=0.07},
    xtick={1990,1995,2000,2005,2010,2015,2020},
    xticklabel style={/pgf/number format/1000 sep=,font=\scriptsize,semithick},
    extra x ticks={1991,1992,1993,1994,1996,1997,1998,1999,2001,2002,2003,2004,2006,2007,2008,2009,2010,2011,2012,2013,2014,2016,2017,2018,2019,2021},
    extra x tick labels={},
    extra x tick style=
    {
        tick label style={red, below, yshift=0.5ex},
        major tick length=0.5ex,
        tick pos= bottom,
        grid=none
    },
    ytick={256,384,512,640,768,896,1024},
    yticklabel style={/pgf/number format/1000 sep=,font=\scriptsize},
    extra y ticks={288,320,352,416,448,480,544,576,608,672,704,736,800,832,864,928,960,992,1024,1056,1088},
    extra y tick labels={},
    extra y tick style=
    {
    tick label style={red, below,yshift=-1ex},
    major tick length=0.5ex,
    tick pos= left,
    grid=none
    },
    xlabel={Year},
    ylabel={Digits},
    grid = major,
]
\addplot +[domain=1990:2020,no marks,samples=2] {(x-2000)*32+521} 
node[pos=0.65,sloped,above] {$(x-2000)*32+521$};

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

在此处输入图片描述

我认为你的问题中有一个拼写错误,你想写restrict y to domain=256:1024但却写了restrict y to domain=256:1204,对吗?如果你想让 pgfplots 切断图,你需要增加样本。

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}% <- 1.16 would be better
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmin=1990,
    xmax=2021,
    ymin=256,
    ymax=1024,
    restrict y to domain=256:1024,
    enlarge y limits={rel=0.13},
    enlarge x limits={rel=0.07},
    xtick={1990,1995,2000,2005,2010,2015,2020},
    xticklabel style={/pgf/number format/1000 sep=,font=\scriptsize,semithick},
    extra x ticks={1991,1992,1993,1994,1996,1997,1998,1999,2001,2002,2003,2004,2006,2007,2008,2009,2010,2011,2012,2013,2014,2016,2017,2018,2019,2021},
    extra x tick labels={},
    extra x tick style=
    {
        tick label style={red, below, yshift=0.5ex},
        major tick length=0.5ex,
        tick pos= bottom,
        grid=none
    },
    ytick={256,384,512,640,768,896,1024},
    yticklabel style={/pgf/number format/1000 sep=,font=\scriptsize},
    extra y ticks={288,320,352,416,448,480,544,576,608,672,704,736,800,832,864,928,960,992,1024,1056,1088},
    extra y tick labels={},
    extra y tick style=
    {
    tick label style={red, below,yshift=-1ex},
    major tick length=0.5ex,
    tick pos= left,
    grid=none
    },
    xlabel={Year},
    ylabel={Digits},
    grid = major,
]
\addplot +[domain=1990:2020,no marks,samples=201] {(x-2000)*32+521} 
node[pos=0.65,sloped,above] {$(x-2000)*32+521$};

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

在此处输入图片描述

当然,在这种简单的情况下,您只需使用domain=1990:2015.72,但对于更复杂的情况,分析反演可能更麻烦(但您可能会自动使用足够多的样本)。

相关内容