1

In my code below, I wanted to add a linear dashed line like this:

enter image description here Here is my code:

\documentclass[varwidth]{standalone}
\usepackage{filecontents}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{float}
\usepackage{pgfplots,pgfplotstable, booktabs}
\pgfplotsset{compat=1.12}
\usetikzlibrary{pgfplots.groupplots, matrix, backgrounds}
\usepgfplotslibrary{fillbetween}

\pgfplotsset{compat=1.12}
\pgfplotsset{minor grid style={dotted,gray!50}}
\pgfplotsset{major grid style={gray!50!black},
             every tick label/.append style={scale=0.85}}

\begin{filecontents*}{testdata.csv}
-155.5  21.7
-152.3  21.5
-149.8  21.2
-147.2  20.8
-145.5  20.3
-143.7  19.5
-141.3  18.4
\end{filecontents*}

\begin{document}
\begin{figure}[H]
\centering
\begin{tikzpicture}[every plot/.append style={very thick}]
\begin{axis}[width=14cm,height=9cm,
                    grid = both,
                    every major grid/.style={gray, opacity=0.7},
                    x dir=reverse,
                    title = {},
                    xlabel={Time},
                    ylabel={Temp},
                    ]

\addplot+ [smooth,blue,name path=plotA] table[mark = none, x index=0,y index=1,col sep=space] {testdata.csv};
\draw [-latex] (-143,21.3) node[above,text width=.8cm,align=center]{\scriptsize{end of \\ linear region}} to[out=270,in=130] (-145,20.1);
\end{axis}
\end{tikzpicture}
\end{figure} 
\end{document}
  • I have a function, but as x dir is reversed, it's not working – PMC1234 Jan 7 '16 at 21:56
1

You can add coordinates to the plot, like this:

\addplot+ [smooth,blue,name path=plotA] table[mark = none, x index=0,
y index=1,col sep=space] {testdata.csv}%
    coordinate [pos=1] (A)
    coordinate [pos=0.9] (B);

1 is the end of the line, 0 is the start. So we pick two that form a line, then make it longer using a negative value for shorten, like

\draw[red,loosely dashed, very thick,shorten >=-7cm] (A) -- (B);

And this is the output

enter image description here

  • Thank you for your elegant solution. Can you give me some more insights as to how you calculated the position of the linear line. I am still a little lost as to the pos=1 and pos=0.9. – Joe Jan 7 '16 at 22:59
  • @Joe Sure. In TikZ, any line has various coordinates, represented from 0 to 1. 0 is the start, 1 is the end, 0.5 is the middle. 0.75 would be at 3/4 of that line, and so on. You can refer to these points with words too, like 0.5 is midway, 0 is at start, and 1 is at end. Try writing \draw (0,0) -- (5,0) node[at start] {foo}; and replace that key with the others. – Alenanno Jan 7 '16 at 23:08
1

I have calculated the linear equation with (-143.7,19.5) and (-141.3,18.4) points and I had this approximation : y=-0.45833333333333*x-46.3625. Then, enter this in your tikzpicture : \addplot+[red,dashed,mark=no,domain=-141.3:-147.75]{-0.45833333333333*x-46.3625};

Full code :

\documentclass[varwidth]{standalone}
\usepackage{filecontents}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{float}
\usepackage{pgfplots,pgfplotstable, booktabs}
\pgfplotsset{compat=1.12}
\usetikzlibrary{pgfplots.groupplots, matrix, backgrounds}
\usepgfplotslibrary{fillbetween}

\pgfplotsset{compat=1.12}
\pgfplotsset{minor grid style={dotted,gray!50}}
\pgfplotsset{major grid style={gray!50!black},
             every tick label/.append style={scale=0.85}}

\begin{filecontents*}{testdata.csv}
-155.5  21.7
-152.3  21.5
-149.8  21.2
-147.2  20.8
-145.5  20.3
-143.7  19.5
-141.3  18.4
\end{filecontents*}

\begin{document}
\begin{figure}[H]
\centering
\begin{tikzpicture}[every plot/.append style={very thick}]
\begin{axis}[width=14cm,height=9cm,
                    grid = both,
                    every major grid/.style={gray, opacity=0.7},
                    x dir=reverse,
                    title = {},
                    xlabel={Time},
                    ylabel={Temp},
                    ]

\addplot+ [smooth,blue] table[mark = none, x index=0,y index=1,col sep=space] {testdata.csv};
\draw [-latex] (-143,21.3) node[above,text width=.8cm,align=center]{\scriptsize{end of \\ linear region}} to[out=270,in=130] (-145,20.1);
\addplot+[red,dashed,mark=no,domain=-141.3:-147.75]{-0.45833333333333*x-46.3625};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

Output :

enter image description here

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.