1

Edit:
The original question was answered by @James and his solution covers the particular problem presented here.


In This question I have exposed the original problem and I have received answer to the points 1, 2 and 4, but not the 3th.

Inspired in the answer given by Carina I started to work in some solution, but it is still not 100% right.

Based on this meta I have decided to post a sub-question to refine the definitive answer and put it on the original post for the posterity.

The (remaining) problem

  • To make the sections* and put the labels on they (in the spaces, not the lines), @Carina gave me some good ideas, but I need something a little more advanced, the idea is that those lines continues below the X labels.
  • To get the values for the section separator lines from an external file (as is done with the stacked area plots).

("sections" are those vertical slices or partitions of the X axis seen in the picture. If X is time, they can be understood as consecutive "periods of time")

The partial answer

partial chart

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}
\usepackage{filecontents}

\begin{filecontents}{data.txt}
Time core1 core2 core3 core4 mem
0 7.847 19.51 18.389 18.943  400.90
1 6.863 64.706 12.871 30  913.50
2 10 88 0 0  1215.19
3 57.576 39 0 0  1691.61
4 0.99 99 0.99 0  1694.64
5 0 40.594 60 0  1698.15
6 0 96.939 3.03 0  1699.55
7 0 50.495 48.515 0  1700.09
8 0.99 53 47 0  1703.00
9 0 28.283 69 3  1696.77
10 31.313 0 0 67.677  1697.30
11 15 84 1.01 2.941  2252.78
12 0 15 14.141 71.717  2249.72
13 31 27 6.931 37  2249.00
14 2 13.725 60.606 28  2248.16
15 9 34.343 41 19  2248.31
16 32 41.414 25.743 0  2250.18
17 26 33.663 20.408 21  2249.89
18 23 13 40 25.253  2249.89
19 47.525 18.182 22 12.121  2249.60
20 34.694 25.253 22.772 16.832  2249.32
21 22 0.99 42.574 37.374  2249.01
22 12.871 24 12.121 56.436  2251.39
23 17.172 15.152 49.02 20.202  2252.57
24 27 5.051 32.653 36  2252.72
\end{filecontents}

\begin{filecontents}{data2.txt} % For now, I'm not using this file
steps
0
0.024
10.127
10.143
21.634
24.81
\end{filecontents}

\begin{document}
    \begin{tikzpicture}[scale=1]
        \begin{axis}[
            axis y line=left,
            axis x line=bottom,
            enlarge x limits=0,
            enlarge y limits=0,
            width=15cm,
            height=8cm,
            stack plots=y,
            area style,
            xlabel={Time},
            ylabel={CPU usage},
            ymax=500
        ]
            \foreach \i in {1,...,4}{
                \addplot table [y index=\i]{data.txt} \closedcycle;
            }
        \end{axis}
        \begin{axis}[
            axis y line=right,
            axis x line=none,
            enlarge x limits=0,
            enlarge y limits=0,
            width=15cm,
            height=8cm,
            fill=none,
            ymax=7600
        ]
            \addplot[very thick,draw=green] table[y index=5]{data.txt};

            % The separators are written manually and there is no label
            \draw[black](axis cs:  0.024,0)--(axis cs:  0.024,8000);
            \draw[black](axis cs: 10.127,0)--(axis cs: 10.127,8000);
            \draw[black](axis cs: 10.143,0)--(axis cs: 10.143,8000);
            \draw[black](axis cs: 21.634,0)--(axis cs: 21.634,8000);
            \draw[black](axis cs: 24.810,0)--(axis cs: 24.810,8000);
        \end{axis}
    \end{tikzpicture}
\end{document}

Some gimp to show the desired output

(Please notice that the steps 1 and 3 are really short, so they seems like a single line but they are two)

The desired output

Thanks a lot for your time :)

2
  • Some remark: (suplement to your MWE with \pgfplotsset{compat=1.13}. By this you can simplified notations of coordinates (see pgfplots manual), the axis cs: is not necessary anymore.
    – Zarko
    Commented Feb 5, 2016 at 6:25
  • great! thanks. the more simple, the more nice, I will change it tomorrow, (here it is almost 4am)
    – onlycparra
    Commented Feb 5, 2016 at 6:53

2 Answers 2

1

Here is a more "automated" way of Zarkos anwer, where you don't have to draw all the "step lines" by hand. For details have a look at the comments in the code.

\documentclass[border=2mm,many]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
    \pgfplotsset{compat=1.11}

\usepackage{filecontents}
    \begin{filecontents}{data.txt}
        Time core1 core2 core3 core4 mem
        0   7.847  19.51   18.389  18.943    400.90
        1   6.863  64.706  12.871  30        913.50
        2  10      88       0       0       1215.19
        3  57.576  39       0       0       1691.61
        4   0.99   99       0.99    0       1694.64
        5   0      40.594  60       0       1698.15
        6   0      96.939   3.03    0       1699.55
        7   0      50.495  48.515   0       1700.09
        8   0.99   53      47       0       1703.00
        9   0      28.283  69       3       1696.77
        10 31.313   0       0      67.677   1697.30
        11 15      84       1.01    2.941   2252.78
        12   0     15      14.141  71.717   2249.72
        13  31     27       6.931  37       2249.00
        14  2      13.725  60.606  28       2248.16
        15  9      34.343  41      19       2248.31
        16 32      41.414  25.743   0       2250.18
        17 26      33.663  20.408  21       2249.89
        18 23      13      40      25.253   2249.89
        19 47.525  18.182  22      12.121   2249.60
        20 34.694  25.253  22.772  16.832   2249.32
        21 22       0.99   42.574  37.374   2249.01
        22 12.871  24      12.121  56.436   2251.39
        23 17.172  15.152  49.02   20.202   2252.57
        24 27       5.051  32.653  36       2252.72
    \end{filecontents}

    \begin{filecontents}{data2.txt}
        steps
        0
        0.024
        10.127
        10.143
        21.634
        24.81
    \end{filecontents}

\begin{document}
    \begin{tikzpicture}

        % define `xmax' value
        % (it has to be a command because it is later needed outside of an
        %  axis environment to filter the `steps' elements, which are greater
        %  than `xmax')
        \def\xmax{24}

        % define color for the vertical lines for the steps
        \colorlet{step color}{black!60}


        % define here what both axis environments have in common
        % so you don't have to repeat this stuff at every axis
        \pgfplotsset{
            every axis/.append style={
                enlargelimits=false,
                width=15cm,
                height=8cm,
                xmin=0,
                xmax=\xmax,
                axis on top,
            },
        }

        \begin{axis}[
            area style,
            stack plots=y,
            xlabel={Time},
            ylabel={CPU usage},
            ymin=0,
            ymax=150,
            ytick distance=25,  % <-- to match ticks on both axis
        ]
            \foreach \i in {1,...,4}{
                \addplot table [x=Time,y=core\i]{data.txt} \closedcycle;
            }
        \end{axis}

            %%% collect all time stamps of the steps in `\allX'
            %%% it is later used in the axis environment to draw the lines
            %%% below the axis lines
            % store table for the steps
            \pgfplotstableread[header=true]{data2.txt}{\data}
            % store number of rows
            \pgfplotstablegetrowsof{\data}
                \pgfmathsetmacro{\rows}{\pgfplotsretval-1}
            % store first element to `\allX'
            \pgfplotstablegetelem{0}{steps}\of\data
                \pgfmathsetmacro{\first}{\pgfplotsretval}
            \def\allX{\first}
            % cycle through the rest of the list and append the time to
            % `\allX' if the value is smaller than `\xmax'
            \pgfplotsforeachungrouped \i in {1,...,\rows} {
                \pgfplotstablegetelem{\i}{steps}\of\data
                \pgfmathparse{(\pgfplotsretval<\xmax) ? 1 : 0}
                \ifdim \pgfmathresult pt>0pt
                    \edef\allX{\allX,\pgfplotsretval}
                \fi
            }

        \begin{axis}[
            axis y line*=right,% <---
            no markers,
            %
            %%% draw step labels
            % therefore use the data of the first `\addplot'
            xtick=data,
            % they should be drawn in the middle of two values
            x tick label as interval,
            % define how the label should look like
            xticklabel={
                % because indexing starts at 0 --> add 1
                \pgfmathparse{\ticknum + 1}
                Step \pgfmathprintnumber{\pgfmathresult}
            },
            % to not overlap the tick label and label of the first axis
            % shift these labels further down
            x tick label style={
                yshift=-8ex,
            },
            ymin=0,
            ymax=3000,
            % (you should also provide a label on the second y axis)
            ylabel=(\emph{replace me}),
            clip=false,% <---
            % in case steps are larger than `xmax' -->  force it to be `xmax'
            % (here you see how to extract the `xmin' and `xmax' values
            %  when you are _inside_ of an axis environment)
            restrict x to domain*=
                \pgfkeysvalueof{/pgfplots/xmin}:\pgfkeysvalueof{/pgfplots/xmax},
        ]

            % use `ybar interval' plot to fake some vertical lines
            % this also enables the easy printing of the `xticklabels'
            \addplot [
                draw=step color,
                ybar interval,
            ]
                table [x=steps,y expr=\pgfkeysvalueof{/pgfplots/ymax}] {data2.txt};

            % now draw the other lines regarding to the second y axis
            \addplot [very thick,draw=green] table [x=Time,y=mem] {data.txt};

            % plot the "extended" vertical lines below the axis
            % with the help of the above prepared `\allX' variable
            \foreach \x in \allX {
                \edef\temp{\noexpand%
                    \draw [color=step color] ([yshift=-3ex] \x,0) -- ++(0,-10ex);
                }\temp
            }

        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

3
  • +1, however OP said in his answer, that the question is already solved, in his original question ... :-(
    – Zarko
    Commented Feb 7, 2016 at 23:06
  • @Zarko, that's true, but when I see it correct, it isn't solved with pgfplots there ;) I'll have a look at that tomorrow. Doesn't seem to be so much different than this question. Commented Feb 7, 2016 at 23:12
  • Stefan, thanks for this answer, as you say is more automated and easier, however, I have placed here another answer explaining that the original question was answered (at the beginning of this question I address it) but it seems that somebody has deleted that answer because I can not see it anymore. The intention of this question was to find a way to make the "sections" automatically, so independently of the above tangle, you attacked it answering my two points.
    – onlycparra
    Commented Feb 8, 2016 at 13:53
3

Edit: This solution in comparison with your MWE differ in the following:

  • in preamble is added \pgfplotsset{compat=1.13}, so in the notation of node and drawing coordinates can be omitted axis cs:
  • added are TikZ libraries \usetikzlibrary{calc,positioning} for determination of nodes coordinates
  • the ymax in both axes are changed from 500 to 200 and 8000 to 3000. By this the interesting part of graph become higher and more clear
  • in both axes are changed graph style in form which I more liked :-)
  • in second axes (with ytick on right side of graph) is added option clip=false. By this is enable draw nodes and lines below graph.
  • according to new ymax values are recalculated coordinates for separating steps
  • in assigning of steps is assumed, that steps are between given lines, i.e. there is 4 steps. For very narrow step 2 is suggest new way to show, where it is

With this changes your desired graph become:

enter image description here

Complete code with signed places of changes is:

\documentclass[border=5mm,many]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{colormaps}
    \pgfplotsset{compat=1.13}

    \usetikzlibrary{backgrounds,calc,positioning}
\usepackage{filecontents}

\begin{filecontents}{data.txt}
    Time core1 core2 core3 core4 mem
    0   7.847  19.51   18.389  18.943    400.90
    1   6.863  64.706  12.871  30        913.50
    2  10      88       0       0       1215.19
    3  57.576  39       0       0       1691.61
    4   0.99   99       0.99    0       1694.64
    5   0      40.594  60       0       1698.15
    6   0      96.939   3.03    0       1699.55
    7   0      50.495  48.515   0       1700.09
    8   0.99   53      47       0       1703.00
    9   0      28.283  69       3       1696.77
    10 31.313   0       0      67.677   1697.30
    11 15      84       1.01    2.941   2252.78
    12   0     15      14.141  71.717   2249.72
    13  31     27       6.931  37       2249.00
    14  2      13.725  60.606  28       2248.16
    15  9      34.343  41      19       2248.31
    16 32      41.414  25.743   0       2250.18
    17 26      33.663  20.408  21       2249.89
    18 23      13      40      25.253   2249.89
    19 47.525  18.182  22      12.121   2249.60
    20 34.694  25.253  22.772  16.832   2249.32
    21 22       0.99   42.574  37.374   2249.01
    22 12.871  24      12.121  56.436   2251.39
    23 17.172  15.152  49.02   20.202   2252.57
    24 27       5.051  32.653  36       2252.72
\end{filecontents}

\begin{filecontents}{data2.txt}
    steps
    0
    0.024
    10.127
    10.143
    21.634
    24.81
\end{filecontents}

\begin{document}
    \begin{tikzpicture}[scale=1,node distance=0mm]
        \begin{axis}[
%            axis y line=left,
%            axis x line=bottom,
            grid,% <---
            area style,
            enlarge x limits=false,% <---
            enlarge y limits=false,% <---
            width=15cm,
            height=8cm,
            stack plots=y,
            xlabel={Time},
            ylabel={CPU usage},
            ymax=150,
        ]
            \foreach \i in {1,...,4}{
                \addplot table [y index=\i,opacity=0.5]{data.txt} \closedcycle;
            }
        \end{axis}

        \begin{axis}[
%            axis y line=right,
%            axis x line=none,
            axis y line*=right,% <---
            grid,% <---
            enlarge x limits=false,
            enlarge y limits=false,
            width=15cm,
            height=8cm,
            fill=none,
            ymax=3000,
            clip=false,% <---
        ]
            \addplot[very thick,draw=green] table[y index=5]{data.txt};
            % The upper part of separators
            \draw
                ( 0.024,3000)-- +(0,-2200)% <---
                (10.127,3000)-- +(0,-2200)% <---
                (10.143,3000)-- +(0,-2200)% <---
                (21.634,3000)-- +(0,-2200)% <---
                (24.000,3000)-- +(0,-2200)% <---
            ;
            % nodes with steps names
            \node [above=of {$( 0.024,-300)!0.5!(10.127,-300)$}] {Step 1};% <---
            \node [above=of {$(10.127,-300)!0.5!(10.127,-300)$}] {Step 2};% <---
            \node [above=of {$(10.127,-300)!0.5!(21.634,-300)$}] {Step 3};% <---
            \node [above=of {$(21.634,-300)!0.5!(24.000,-300)$}] {Step 4};% <---
            % The lower part of separators
            \draw
                ( 0.024,150) -- (0.024,-300)% <---
                (10.127,150) |- ( 8.5,-50) -- ( 8.5,-300)% <---
                (10.143,150) |- (11.5,-50) -- (11.5,-300)% <---
                (21.634,150) -- (21.634,-300)% <---
                (24.000,150) -- (24.000,-300);% <---
        \end{axis}
    \end{tikzpicture}
\end{document}

Hopefully this solution will serve as basis for your refinement of graph or added missing steps (your picture shows more than can be concluded from your MWE)

1
  • @onlycparra, see edit of my answer. Now it contain final version of my answer with some explanation, what is going on in code.
    – Zarko
    Commented Feb 5, 2016 at 16:06

Not the answer you're looking for? Browse other questions tagged .