6

For my current project, I need to display a series of percentage changes in a plot.

This is the code I have so far

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepgfplotslibrary{dateplot}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        date coordinates in=x,
        date ZERO=2015-04-29 00:00,
        %
        xticklabel=\hour:\minute,
        xticklabel style={rotate=45,anchor=north east},
        xtick=data,
        xlabel=Date,
        %
        ylabel=Percental change in stock price,
        %
        width=\textwidth
    ]
        \addplot coordinates {
            (2015-04-29 14:30, -0.08438)
            (2015-04-29 15:00, 0.38414)
            (2015-04-29 15:30, -0.59243)
            (2015-04-29 16:00, -0.28056)
            (2015-04-29 16:30, 0.20808)
            (2015-04-29 17:00, -0.18451)
            (2015-04-29 17:30, -0.83166)
            (2015-04-29 18:00, 0.22455)
            (2015-04-29 18:30, -0.06981)
            (2015-04-29 19:00, 0.43784)
            (2015-04-29 19:30, -0.12288)
            (2015-04-29 20:00, -0.01929)
            (2015-04-29 20:30, -0.38754)
            (2015-04-29 21:00, 0.05445)
            (2015-04-30 14:30, -0.80921)
            (2015-04-30 15:00, -0.94902)
            (2015-04-30 15:30, -0.72899)
            (2015-04-30 16:00, 0.22255)
            (2015-04-30 16:30, 0.21400)
            (2015-04-30 17:00, -0.41150)
            (2015-04-30 17:30, 0.00612)
            (2015-04-30 18:00, -0.17905)
            (2015-04-30 18:30, -0.02388)
            (2015-04-30 19:00, 0.05997)
            (2015-04-30 19:30, -0.29116)
            (2015-04-30 20:00, -0.20734)
            (2015-04-30 20:30, -0.02925)
            (2015-04-30 21:00, 0.19214)
        };
    \end{axis}
\end{tikzpicture}

\end{document}

But this unfortunately this does not do exactly what I want.. This is the outcome: enter image description here

Can someone explain how I can (preferably automatically) skip the hours for which no data is logged? Basically, this is what I want, but I cannot get it to work: Collapse range in x-axis with pgfplots? (break x-axis)

Stefan Pinnow
  • 29,535
Diederik
  • 171

1 Answers1

5

This is not automatic, but other than that I think it solves your problem. You basically make two plots next to each other, and remove part of the axis frame. I also drew a zig-zag line like in the question you mentioned.

enter image description here

\documentclass[border=4mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepgfplotslibrary{dateplot,groupplots}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}

\begin{tikzpicture}
    \begin{groupplot}[
        date coordinates in=x,
        date ZERO=2015-04-29 00:00,
        %
        xticklabel=\hour:\minute,
        xticklabel style={rotate=45,anchor=north east},
        xtick=data,
        %
        ylabel=Percental change in stock price,
        %
        ymin=-1,ymax=0.5,
        group style={
           group size=2 by 1,
           horizontal sep=0pt,
           y descriptions at=edge left,
           group name=A}
    ]
\nextgroupplot[axis y line*=left,xlabel=29 April]
        \addplot coordinates {
            (2015-04-29 14:30, -0.08438)
            (2015-04-29 15:00, 0.38414)
            (2015-04-29 15:30, -0.59243)
            (2015-04-29 16:00, -0.28056)
            (2015-04-29 16:30, 0.20808)
            (2015-04-29 17:00, -0.18451)
            (2015-04-29 17:30, -0.83166)
            (2015-04-29 18:00, 0.22455)
            (2015-04-29 18:30, -0.06981)
            (2015-04-29 19:00, 0.43784)
            (2015-04-29 19:30, -0.12288)
            (2015-04-29 20:00, -0.01929)
            (2015-04-29 20:30, -0.38754)
            (2015-04-29 21:00, 0.05445)
        };

\nextgroupplot[axis y line*=right,xlabel=30 April]
        \addplot coordinates {
            (2015-04-30 14:30, -0.80921)
            (2015-04-30 15:00, -0.94902)
            (2015-04-30 15:30, -0.72899)
            (2015-04-30 16:00, 0.22255)
            (2015-04-30 16:30, 0.21400)
            (2015-04-30 17:00, -0.41150)
            (2015-04-30 17:30, 0.00612)
            (2015-04-30 18:00, -0.17905)
            (2015-04-30 18:30, -0.02388)
            (2015-04-30 19:00, 0.05997)
            (2015-04-30 19:30, -0.29116)
            (2015-04-30 20:00, -0.20734)
            (2015-04-30 20:30, -0.02925)
            (2015-04-30 21:00, 0.19214)
        };
    \end{groupplot}
\draw (A c1r1.south east) ++(-3pt,-4pt) -- +(3pt,8pt)
                          ++(2pt,0pt) -- +(3pt,8pt);
\draw[black,decoration={zigzag},decorate] (A c1r1.south east) -- (A c1r1.north east);
\end{tikzpicture}
\end{document}
Torbjørn T.
  • 206,688