2

I'm trying to draw a plot with Tikz. I need to put a label on my line, preferable on the top right corner of the plot and over the line with the same slope.

\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}

When I use restrict y to domain=256:1204 the line disappears. I need to set it like 25:2048 to see the line.

enter image description here

  1. How to limit the line so that it stays inside.
  2. How to add a label as in the picture.
  • You can remove the dot with no marks. The line reappears for instance if you increase the samples. However, I do not precisely understand the purpose of restrict y to domain=256:1204, since you have also ymin=256, ymax=1024, which is more restrictive. If you explain the purpose, it is easier to provide you with a meaningful answer. – user194703 Dec 28 '19 at 22:56
  • @Schrödinger'scat Like in this answer I want all stay inside. no marks fine. sample set to 100, now seems. thanks. – kelalaka Dec 28 '19 at 23:00
  • When I set ymax=1000 it still goes out of the plot. – kelalaka Dec 28 '19 at 23:03
  • 2
    To remove the dot, you can use \addplot instead of \addplot+... – Paul Gaborit Dec 29 '19 at 0:30
  • 1
    With [restrict y to domain] you should try to include at least one point outside the border. With samples=2, the only point outside the border is the end. – John Kormylo Dec 29 '19 at 15:07
3

What I wanted to say is that you do not need to use restrict y to domain=256:1204, if you have the more restrictive ymin=256, ymax=1024 in place. You do not need to increase the samples, nor does restrict y to domain=256:1204, seem to have any purpose. Just using no marks and an appropriate pos for the node yields

\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}

enter image description here

I think there is a typo in your question, you wanted to write restrict y to domain=256:1024 but wrote restrict y to domain=256:1204, right? If you want pgfplots to cut off the plot, you need to increase the samples.

\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}

enter image description here

Of course, in simple cases like this you could just use domain=1990:2015.72, but for more complicated cases an analytical inversion may be more cumbersome (yet then you probably automatically use a large enough number of samples).

| improve this answer | |
  • The line starts at x0=1990. Is it possible to cut it for y>1024? – kelalaka Dec 28 '19 at 23:14
  • 1
    @kelalaka Sure. The by far simplest solution will be domain=1990:2015.72. Do you want a pgfplots solution that does not require you to invert a linear equation? – user194703 Dec 28 '19 at 23:17
  • Invert :), no. It already does alot. Now fine. Thanks. – kelalaka Dec 28 '19 at 23:20
  • 1
    FYI. You can see the final output on Crypto.Se How big an RSA key is considered secure today?. Thanks, again. – kelalaka Dec 29 '19 at 20:19
  • 1
    @kelalaka Thanks! Very interesting! – user194703 Dec 29 '19 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.