1

I want to create a residual for this graph:

\begin{figure}[hbt!]
\centering
\begin{tikzpicture}
    \begin{axis}[,width=\textwidth,
            axis lines=middle,
            grid=major,
            xmin=-50, xmax=670,
            ymin=18.4, ymax=25.2,
            xlabel=$t(s)$,
            ylabel=$T(^{\circ} \mathrm{C})$,
            tick style={thick},
            ticklabel style={font=\footnotesize},
            xtick={-50,0,...,650},
            ytick={19,20,...,25}, minor ytick={19.5,20.5,...,24.5}
        ]
        \addplot[black,thin,samples=100,domain=-50:625] {0.007303*x+19.8545};
        \addplot [blue, thin, only marks,mark=*] coordinates { 
            (0,20) (60,20.1) (120,20.6) (180,21.2) (240,21.7) (300,22.2) (360,22.5) (420,23.0) (480,23.1) (540,23.8) (600,24.3)
        };
    \end{axis}
\end{tikzpicture}

\end{figure}

It looks like this: enter image description here

And the residual that i want to create has to look something like this: enter image description here I can't seem to find anything about this particular style of residual, and since i'm totally new to LaTeX i have no idea how i would create something like this.

Since i'm already making this post, i have a few problems with the graph in general that i would like help with too.

  1. The xlabel and ylabel is at the end of each axis. If i wanted to move them underneath and besides the axis instead, how would i do that? Like it is shown in this example: enter image description here

  2. I would like for the left outer line of the grid to be removed, while still keeping the tick at -50 around.

  3. If i wanted to make some floating text besides the linear function, with the text 0.007303x+19.8545, how would i do that? Alternatively i could add a legend in the top left corner.
  4. Why doesn't the 0 tick show on the x-axis. I can't seem to make it appear.
  • The x- and ylabel are there in your screenshot, at the ends of the axis lines. – Torbjørn T. Oct 8 at 20:58
  • Oh, of course. I'm a dummy, my bad! – Nikolai Oct 8 at 21:10
  • I've rephrashed the question now! :) – Nikolai Oct 8 at 21:17
2

For the residual, one obvious approach is to calculate the values in some other program, and make a new list of coordinates, just like you did for the original data.

You can also do it with pgfplots, if you read the data into a pgfplotstable table. If the table is called \MyData, you can do e.g.

\addplot [mark=*,blue, only marks]
    table[
      x=x,
      y expr={\thisrow{y}-f(\thisrow{x})}
    ] {\MyData};

where f(x) is a function I defined with

\tikzset{
  declare function={
    f(\x) = {0.007303*\x+19.8545}; % for convenience
  }
}

For the other questions:

  1. Might be better options, but this works:

        xlabel style={at={(xticklabel cs:0.5)},below},
        ylabel style={at={(yticklabel cs:0.5)},rotate=90,above},
    
  2. You could use an extra x tick for this, e.g.

        xtick={0,50,...,650}, % <-- start at zero
        extra x ticks={-50}, % <-- added
        extra x tick style={grid=none}, % <-- added
    
  3. You can simply append a node at the end of the \addplot, e.g.

    \addplot[black,thin,samples=100,domain=-50:625] {f(x)} node[above left] {$0.007303x+19.8545$};
    
  4. Tick labels at axis intersections are hidden by default, add

    hide obscured x ticks=false
    

Complete code below. I didn't add any styling of the residual plot. Should it be immediately below the main plot?

\documentclass[border=5mm]{standalone}
\usepackage{pgfplotstable} % <-- pgfplotstable instead of pgfplots (the former loads the latter)
\pgfplotstableread{
x y
0   20
60   20.1
120   20.6
180   21.2
240   21.7
300   22.2
360   22.5
420   23.0
480   23.1
540   23.8
600   24.3
}\MyData

\tikzset{
  declare function={
    f(\x) = {0.007303*\x+19.8545}; % for convenience
  }
}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
            xlabel={$t(s)$},
            ylabel={$T\left(^{\circ} \mathrm{C}\right)$},
            width=\textwidth,
            axis lines=middle,
            grid=major,
            xmin=-50, xmax=670,
            ymin=18.4, ymax=25.2,
            xlabel style={at={(xticklabel cs:0.5)},below},
            ylabel style={at={(yticklabel cs:0.5)},rotate=90,above},
            tick style={thick},
            ticklabel style={font=\footnotesize},
            xtick={0,50,...,650}, % <-- start at zero
            extra x ticks={-50}, % <-- added
            extra x tick style={grid=none}, % <-- added
            ytick={19,20,...,25},
            minor ytick={19.5,20.5,...,24.5},
            hide obscured x ticks=false % <-- added
        ]
        \addplot[black,thin,samples=100,domain=-50:625] {f(x)} node[above left] {$0.007303x+19.8545$};
        \addplot [blue, thin, only marks,mark=*] table {\MyData};
    \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}
\addplot [mark=*,blue, only marks]
    table[
      x=x,
      y expr={\thisrow{y}-f(\thisrow{x})}
    ] {\MyData};
\end{axis}
\end{tikzpicture}
\end{document}

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.