3

As the title suggests, I'm trying to display a data set and a function in the same graph - more or less like this excel graph, with the blue dots representing the data, and the orange line being the function.

Want it to look like this

This is the code:

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}

\begin{document}

\centering
\begin{tikzpicture}
  \begin{axis}[
    yticklabel style={/pgf/number format/fixed,
                  /pgf/number format/precision=4},
    legend pos=north east,
    name = plot,
    width=0.9\textwidth,
    height=0.5\textheight,
    xlabel={$Zeit$},
    ylabel={$Extinktion$},
    xtick=data,
    ymin = 1.271,
    ymax = 1.291,
    xmin = 15,
    xmax = 180,
    grid=major,
    axis lines = left,
  ]
\addplot [
    smooth,
    x = Zeit,
    y = Extinktion,
    black,
    only marks,
    mark = *,
    mark size = 2,
    /pgf/number format/read comma as period
    ] table{
Zeit    Extinktion
15  1,283
30  1,291
45  1,275
60  1,272
75  1,271
90  1,273
105 1,277
120 1,278
135 1,291
150 1,274
165 1,275
180 1,274
};
\addplot[
    red,
    ]
    expression{-0.0002 * x + 1.2854};
\end{axis}
\end{tikzpicture}
\end{document}

And this is what I'm presented with:

Latex Graph; no function

As you can see, the function isn't shown, even though I'm using the formula generated by Excel, which should work mathematically speaking.

1
  • 4
    Welcome to TeX.SE!!! Just ad the appropriate domain. For example: \addplot[red,domain=15:75] ... Commented May 21, 2023 at 9:36

1 Answer 1

1

This works:

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}

\begin{document}

\centering
\begin{tikzpicture}
  \begin{axis}[
    yticklabel style={/pgf/number format/fixed,
                  /pgf/number format/precision=4},
    legend pos=north east,
    name = plot,
    width=0.9\textwidth,
    height=0.5\textheight,
    xlabel={$Zeit$},
    ylabel={$Extinktion$},
    xtick=data,
    ymin = 1.271,
    ymax = 1.291,
    xmin = 15,
    xmax = 180,
    grid=major,
    axis lines = left,
  ]
\addplot [
    smooth,
    x = Zeit,
    y = Extinktion,
    black,
    only marks,
    mark = *,
    mark size = 2,
    /pgf/number format/read comma as period
    ] table{
Zeit    Extinktion
15  1,283
30  1,291
45  1,275
60  1,272
75  1,271
90  1,273
105 1,277
120 1,278
135 1,291
150 1,274
165 1,275
180 1,274
};
\addplot[domain=15:75, red,]expression{-0.0002 * x + 1.2854};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .