Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'd like to add a dashed line to the end of a plot to indicate a future projection as opposed to real data. I can do this by adding a second plot, as in the example below. However this isn't really neat - its repeated code and doesn't lend itself to doing this with data pulled in using pgfplotstableread for example. Is there a way of changing linestyle for a selected section of a plot? ie saying that for the last segment of each plot should be in a different style?

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}

\usepackage {tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[width=\textwidth,
ymin=0,
ymax=16,
ytick={0,4,...,16},
xmin=0,
xmax=12,
no markers,
xtick={0,4,...,12},
xticklabels={Ancient history,The past,Now,The Future}
]
\addplot+ [color=blue] coordinates
{(0,2)
(4,4)
(8,9)
};
\addplot+ [color=blue,style=dashed] coordinates
{(8,9)
(12,16)};
\end{axis} 
\end{tikzpicture} 

\end{document}
share|improve this question
    
I don't know how to use them with pgfplots but you could use decorations like in Reuse part of a path –  Ignasi May 28 '12 at 9:17
add comment

2 Answers

up vote 10 down vote accepted

Based on percusse's excellent answer, here's a similar approach that uses a node with the options pos=1,sloped, which will place a node at the end of the plot that is oriented with the plot direction. The current plot style can be used by putting the drawing commands into execute at end plot visualization, when the key is still defined (unlike in execute at end plot).

Drawback in this implementation: You'll have to manually put node [predict future nodestyle {} at the end of your \addplot command, since I haven't found a way of letting the predict future style take care of this.

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{decorations.markings,calc}

\newcommand{\predictfuture}{

}

\tikzset{
    predict future/.style={
        /pgfplots/execute at end plot visualization={\draw [current plot style, dashed] (@auxnode.center) -- ($(@auxnode.center)!10cm!(@auxnode.east)$);}
    },
    predict future nodestyle/.style={
        pos=1, inner sep=0pt, sloped, alias=@auxnode
    }
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[width=\textwidth,
ymin=0,
ymax=16,
ytick={0,4,...,16},
xmin=0,
xmax=12,
no markers,
xtick={0,4,...,12},
xticklabels={Ancient history,The past,Now,The Future}
]
\addplot+ [color=blue,predict future] coordinates
{(0,2)
(4,4)
(8,9)
} node [predict future nodestyle] {};
\addplot+ [domain=0:5.5,samples=50,predict future] {15-0.2*x^2+sin(2*pi*25*x)} node [predict future nodestyle] {};
\addplot+ [predict future,domain=0.1:5.5,samples=50] {2.5*ln(x)} node [predict future nodestyle] {};
\end{axis} 
\end{tikzpicture} 
\end{document}
share|improve this answer
    
Awesome, thanks guys. Sorry that I can only mark one answer as accepted... –  user8786 May 31 '12 at 7:45
    
Wow! This is even smarter. Nice trick with the node shape. As far as I understand current plot style is populated in between execute at end plot and execute at end plot visualization so the latter is too late for me and the former is too early. I tried to catch the internal variables which hold something like the \tikz@drawmodes or accessing the \pgfplots@plotspeclist but that's like looking under each stone. It's amazing how Christian can keep track of these variables. –  percusse May 31 '12 at 11:19
    
The “drawback” can be minimized by using insert path: future/.style={insert path={node [predict future nodestyle] {}}}. Now you at least only have to write [future] right before the ; (or whatever one chooses for that style). –  Qrrbrbirlbel May 18 '13 at 1:38
add comment

This is an almost working answer. I am still trying to find a way to pass the line color, line width etc. to the prediction line. Please let me know if you see how to do it. I have tried using the current plot style key but couldn't make it work.

I should have supplied the current plot style to the scope in the first place and should have used the execute at end plot visualization as Jake neatly used in his answer with a clever trick.

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{decorations.markings,calc}

\tikzset{predict future/.style={
        decoration={markings,
        mark=at position 0.985 with {\coordinate (precoord);}
                },
        postaction={decorate},
        /pgfplots/execute at end plot visualization={\begin{scope}[current plot style]
        \draw[dashed] let \p1=(precoord),
        \p2 = (current plot end),
        \p3=($(\p2)-(\p1)$),
        \p4=($(rel axis cs:1,1)-(rel axis cs:0,0)$),
        \n3={atan2(\x3,\y3)},
        \n4={veclen(\x4,\y4)}
        in 
        (current plot end) -- ++(\n3:\n4);
                \end{scope}
        }
    }
}


\begin{document}
\begin{tikzpicture}
\begin{axis}[width=\textwidth,
ymin=0,
ymax=16,
ytick={0,4,...,16},
xmin=0,
xmax=12,
no markers,
xtick={0,4,...,12},
xticklabels={Ancient history,The past,Now,The Future},
]
\addplot+ [color=blue,predict future] coordinates
{(0,2)
(4,4)
(8,9)
};
\addplot+ [predict future,line width=2mm,domain=0:5.5,samples=50] {15-0.2*x^2+sin(2*pi*25*x)};
\addplot+ [predict future,double=yellow,double distance=0.5cm,domain=0.1:5.5,samples=50] {2.5*ln(x)};
\end{axis} 
\end{tikzpicture} 
\end{document}

The basic mechanism is to put a coordinate close to the end point and get the tangent angle. This is possible since the start and the end points of the last used plot is avaliable to us with (current plot start) and (current plot end). Then we draw a very long line on that direction obtained by the tangent given by these very close points such that it goes off the plot. I have added two more rather esoteric curves for testing purposes.

enter image description here

share|improve this answer
    
That's a great idea! The current plot style is available when using the execute at end plot visualization key, see my answer. I couldn't manage to get this to work with your code, though. Any ideas? –  Jake May 31 '12 at 5:17
    
@Jake I tried adding visualization and also to supply the option to a scope instead of directly to the \draw command and it works now. Probably it had some irrelevant options causing it to break down. –  percusse Jun 1 '12 at 0:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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