5

The following MWE shows a template I use in university to show calibration data and the corresponding linear regression. Now I was asking if I can compute an $X$-value for a given $Y$-value and show it inside the plot similar like the following picture.

show calculated values inside plot

Until now I have to calculate the values by hand and give them the beloved pgfplots.

I hope, that the question is clear enough and that there is a semi-automatic solution.

P.S.: I use LuaLaTeX for compiling. For better usability I commented out the font I use.

\documentclass{standalone}

\usepackage{luaotfload}
\usepackage[no-math]{fontspec}
%\defaultfontfeatures{Ligatures=TeX} %,Scale=MatchLowercase} still buggy
%\setmainfont{Linux Biolinum O}
%\setsansfont{Linux Biolinum O}
%\setmonofont{Linux Libertine Mono O}

\usepackage{polyglossia}
\setmainlanguage{german}
\usepackage[autostyle=true]{csquotes}

\usepackage[
    tbtags,
    sumlimits,
    intlimits,
    namelimits
    ]{amsmath}
\usepackage{amsfonts}
\usepackage{amsthm}
\usepackage{amscd}
\setcounter{MaxMatrixCols}{12}
\usepackage[german]{translator}
\usepackage[detect-all=true]{siunitx}
\sisetup{
%   strict,
    output-decimal-marker={,},
    exponent-product=\cdot,
    text-micro={\fontfamily{mdbch}\textmu},
    math-micro=\muup
}
\DeclareSIUnit\molar{M}
\usepackage[math-style=ISO,bold-style=ISO]{unicode-math}
\setmathfont{Asana Math}
\usepackage{lualatex-math}

\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable}

\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
    title={Kalibration für den Versuch XYZ},
    /pgf/number format/use comma,
    width=\linewidth,
    grid=major,
    grid style={dashed,gray!30},
    x label style={at={(axis description cs:.5,-.025)},anchor=north},
    y label style={at={(axis description cs:.05,.5)},anchor=south},
    xlabel=Konzentration~{[}\si{\milli\gram\per\milli\litre}{]},
    ylabel=Extinktion,
%   xtick={0,60,90,120,150,180},
    x tick label style={rotate=90,anchor=east},
    legend cell align=left,
    legend pos=north west,
    ]
    \addplot[
    only marks,
    thick,
    color=black,
    mark=*,
    mark options={fill=red},
    ] table [x=X,y=Y, row sep=\\] {
        X Y\\
        0 0\\
        25 0.17\\
        50 0.43\\
        75 0.60\\
        100 0.79\\
        125 1.01\\
        };
    \addlegendentry{Messpunkte}
    \addplot[thick,color=blue] table[row sep=\\,y={create col/linear regression={y=Y}},mark=none] {
        X Y\\
        0 0\\
        25 0.17\\
        50 0.43\\
        75 0.60\\
        100 0.79\\
        125 1.01\\
        };
    \addlegendentry{%
        $\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
        \pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$};
    \draw[thick,color=blue] (axis cs:20,-1) -- (axis cs:20,.15609);
    \draw[thick,color=blue] (axis cs:-20,.15609) -- (axis cs:20,.155);
    \node [coordinate,pin=below right:{\scriptsize\textit{minimal effective concentration}}] at (axis cs:20,.15609) {};
    \end{axis}
    \end{tikzpicture}
\end{document}
  • How does your picture relate to your code? – Thruston Apr 18 '15 at 22:26
  • It doesn't relate to this specific code. This is a picture from another code I wrote. I can include the snippet if necessary. The picture is only there to depict what I want to realize. – Ruehrfisch Apr 18 '15 at 22:40
4

The simplest way to draw these lines is to define all values as macros and calculate the x-value with \pgfmathparse{(\myY-\pgfplotstableregressionb)/\pgfplotstableregressiona}. The result can be saved as \pgfmathsetmacro\myX{\pgfmathresult}.

% % Y-Value % %
\def\myY{0.155}
% % % % % % % %

\def\xMin{0}
\def\yMin{0} 

The minimums are used for the intersection with the axis.

\documentclass{standalone}

\usepackage[german]{translator}
\usepackage[detect-all=true]{siunitx}
\sisetup{
%   strict,
    output-decimal-marker={,},
    exponent-product=\cdot,
    text-micro={\fontfamily{mdbch}\textmu},
    math-micro=\muup
}
\DeclareSIUnit\molar{M}


\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable}

% % Y-Value % %
\def\myY{0.155}
% % % % % % % %

\def\xMin{0}
\def\yMin{0} 

\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
    title={Kalibration für den Versuch XYZ},
    /pgf/number format/use comma,
    width=\linewidth,
    grid=major,
    grid style={dashed,gray!30},
    x label style={at={(axis description cs:.5,-.025)},anchor=north},
    y label style={at={(axis description cs:.05,.5)},anchor=south},
    xlabel=Konzentration~{[}\si{\milli\gram\per\milli\litre}{]},
    ylabel=Extinktion,
%   xtick={0,60,90,120,150,180},
    xmin=\xMin,
    ymin=\yMin,
    x tick label style={rotate=90,anchor=east},
    legend cell align=left,
    legend pos=north west,
    ]
    \addplot[
    only marks,
    thick,
    color=black,
    mark=*,
    mark options={fill=red},
    ] table [x=X,y=Y, row sep=\\] {
        X Y\\
        0 0\\
        25 0.17\\
        50 0.43\\
        75 0.60\\
        100 0.79\\
        125 1.01\\
        };
    \addlegendentry{Messpunkte}
    \addplot[thick,color=blue] table[row sep=\\,y={create col/linear regression={y=Y}},mark=none] {
        X Y\\
        0 0\\
        25 0.17\\
        50 0.43\\
        75 0.60\\
        100 0.79\\
        125 1.01\\
        };
    \addlegendentry{%
        $\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
        \pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$};
        \pgfmathparse{(\myY-\pgfplotstableregressionb)/\pgfplotstableregressiona}     
    \pgfmathsetmacro\myX{\pgfmathresult} 
    \draw[thick,color=blue] (axis cs:\myX,\yMin) -- (axis cs:\myX,\myY);
    \draw[thick,color=blue] (axis cs:\xMin,\myY) -- (axis cs:\myX,\myY); 
    \node [coordinate,pin=below right:{\scriptsize\textit{minimal effective concentration}}] at (axis cs:\myX,\myY) {};
    \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.