0

I am trying to create a plot with execution times looking a bit like this (I want an axis with time on the bottom):

fooer   XXXXX 
foo2bar      XXXXX
        |    |     | 

Where (in this example) fooer starts at 0 and goes to 600 and foo2bar starts at 600 and goes to 1020. In the genreal case the numbers can be much bigger so large numebrs needs to be supported. I previously attempted to do this with the pgfgant package Gantt chart with milliseconds as input but I am now thinking perhaps pgfplots is a better approach. I found some examples on the Internet and thinking that this is pretty much a bar plot I set of:

\documentclass[11pt,twoside,openright]{memoir}

\usepackage{tikz}
\usepackage{pgfplots}


    \begin{document}
        \begin{tikzpicture}
      \begin{axis}[
        xbar,
        y axis line style = { opacity = 0 },
        axis x line       = none,
        tickwidth         = 0pt,
        symbolic y coords = {fooer, foo2bar},
        nodes near coords,
      ]
      \addplot coordinates { (600,fooer) (1020,foo2bar+600) };
      \end{axis}
    \end{tikzpicture}
    \end{document}

enter image description here

As you can see I am not even near... My example has many lines (I was going for two), overlapping plotting (no idea why), and a blue color that I have no idea where it came from but since I want to define colors myself I am least glad there is some color. Clearly I need help, this wasn't as easy as I was hoping... :/


EDIT: With the help of tex.stackexchange.com/questions/401935 I came a bit further:

\documentclass[11pt,twoside,openright]{memoir}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{xcolor}

\definecolor{color1}{RGB}{146,200,180}
\definecolor{color2}{RGB}{124,206,89}

\pgfplotstableread[col sep=comma]{
y,start,end,color
0,0,600,color1
1,600,1020,color2
}\loadedtable


\begin{document}
\begin{tikzpicture}
\begin{axis}[
  xbar,
  y axis line style = { opacity = 0 },
  axis x line       = none,
  tickwidth         = 0pt,
  xbar stacked, % means that the bars from the different \addplots are stacked one after the other
  y dir=reverse % means the y-axis increases downward
]
% make an invisible bar from zero to the start-value
\addplot [draw=none, forget plot] table [col sep=comma,x=start, y=y]{\loadedtable};
% then add the actual bar, stacked after the invisible one
% for pgfplots the x-value is the length of the bar, so
% we calculate end minus start 
\addplot  +[fill=black,draw=none] table [x expr=\thisrow{end}-\thisrow{start}, y=y] {\loadedtable};

% fill background
\fill [\pgfkeysvalueof{/pgfplots/color}] (\pgfkeysvalueof{/pgfplots/xmin}, \pgfkeysvalueof{/pgfplots/ymax}) rectangle
                 (\pgfkeysvalueof{/pgfplots/xmax}, 6.5);
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

I get three lines though, seems the one that is supposed to be transparent is not transparent. My colors does not seems to work at all and then there is the issue of all the space between them.

  • tex.stackexchange.com/questions/401935 might be of interest. The color is a pgfplots default. Your example throws an error, because you use foobar2+600 as the y-coordinate which doesn't make sense in the first place, and the error is because the symbolic coordinate you defined is foobar2, not foobar2+600. – Torbjørn T. Jun 29 '18 at 12:58
  • Oh is that the y coordinate. I wanted to change the starting position, the x coordinate... – jonalv Jun 29 '18 at 13:29
  • Yes, it's always coordinates { (x1, y1) (x2, y2) <etc.>}. Bars in xbar/ybar always start at zero, so to have a bar start at some other value you need to use xbar stacked, and plot an invisible bar first. A better example than the previous link I mentioned is tex.stackexchange.com/questions/147012/… – Torbjørn T. Jun 29 '18 at 13:44
  • Anotehr stab at color \addplot+[\thisrow{color}] table [x expr=\thisrow{start}-\thisrow{end}, y=Name]{\loadedtable}; but that doesn't work either. Is it not possible to change the colors... :( – jonalv Jun 29 '18 at 15:10
1

If you are interested in a solution, I came up with this:

\documentclass[11pt,twoside,openright]{memoir}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{xcolor}

\definecolor{color1}{RGB}{146,200,180}
\definecolor{color2}{RGB}{124,206,89}

\pgfplotstableread[col sep=comma]{
start,end,Name,color
0,600,fooer,color1
600,1020,foo2bar,color2
}\loadedtable


\begin{document}

\begin{tikzpicture}
\begin{axis}[
    xbar, xmin=0,
    y axis line style = { opacity = 0 },
    tickwidth         = 0pt,
    width=12cm, height=3.5cm, enlarge y limits=0.5,
    symbolic y coords={fooer,foo2bar},
    ytick=data, xbar stacked,
    bar shift=0pt,
    y dir=reverse,
    xtick={1, 500, 1000},
    xticklabels={1 ms, 500 ms, 1 s}
]

\addplot [draw=none, forget plot] table [x=end, y=Name]{\loadedtable};
\addplot table [x expr=\thisrow{start}-\thisrow{end}, y=Name]{\loadedtable};
\end{axis}
\end{tikzpicture}

\end{document}

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.