4

I need to plot the ragged line between two specific points, similar to the following graph:enter image description here

I have done the following:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[smooth, domain=0:3] {1+sin(x+rand*10)};
\addplot[smooth, domain=3:6] {5-0.5*x+sin(x+rand*10)};
\addplot[smooth, domain=6:9] {2+sin(x+rand*10)};
\addplot[smooth, domain=0:3, dashed] {1};
\addplot[smooth, domain=3:9, dashed] {2};
\end{axis}
\end{tikzpicture}
\end{document}

The problem is that the lines are jumping at the point (6,2). Can anyone help please?

enter image description here

  • 1
    Welcome to TeX.SX! On this site, a question should typically revolve around an abstract issue (e.g. "How do I get a double horizontal line in a table?") rather than a concrete application (e.g. "How do I make this table?"). Questions that look like "Please do this complicated thing for me" tend to get closed because they are either "off topic", "too broad", or "unclear". Please try to make your question clear and simple by giving a minimal working example (MWE): you'll stand a greater chance of getting help. – Null Aug 17 '16 at 15:46
  • 2
    You could use a single \addplot for this. Try \addplot[smooth, domain=0:9, samples=150] {rand*0.2 + (x<3 ? 1 : (x<6 ? 5-0.5*x : 2))};. – Jake Aug 19 '16 at 12:02
  • Thanks @Jake. And how can I remove the vertical line at x=3? – user27808 Aug 19 '16 at 13:04
  • Ah, I thought you wanted all segments connected. If you don't want the first and second segment to be connected, use separate \addplot[smooth, domain=0:3, samples=50] {1+rand*0.2}; \addplot[smooth, domain=3:9, samples=100] {rand*0.2 + (x<6 ? 5-0.5*x : 2)}; – Jake Aug 19 '16 at 13:09
2

Thanks to @Jake, this is the answer:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis line style = very thick, smooth, no markers,
        axis lines= middle,
        ylabel=Price,  
        xlabel=Time,  
        xmax=10, xmin=-1,
        ymax=5, ymin=-1,
        ytickmax=0, 
        xtickmax=0, 
]
\addplot[smooth, domain=0:3, samples=20] {1+rand*0.3}; 
\addplot[smooth, domain=3:9, samples=40] {rand*0.3 + (x<6 ? 5-0.5*x : 2)};
\addplot[smooth, domain=0:3, dashed] {1};
\addplot[smooth, domain=3:9, dashed] {2};
\end{axis}
\node[draw] at (5,5) {Price Adjustment};
\node[draw] at (2.5,0) {Good News};
\draw[thick,->] (2.5, 0.5) -- (2.5, 1.5);
\draw[thick,->] (4.5,4.5) -- (4, 4);
\draw [blue,decorate,decoration={brace,amplitude=5pt},xshift=-4pt,yshift=0pt]
(2.7,4.5) -- (4.4,3.1) node [black,midway] {};
\end{tikzpicture}
\end{document}

result

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.