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 have CSV data representing the time in nanoseconds for calls to my custom malloc(), realloc() and free() implementations. The data looks like this:

function,time
malloc,331
malloc,386
malloc,326
malloc,321
malloc,321
malloc,316
malloc,331
malloc,337
malloc,311
malloc,322
realloc,502
realloc,662
realloc,536
realloc,477
realloc,441
realloc,552
realloc,477
realloc,437
realloc,447
realloc,542
free,321
free,327
free,321
free,402
free,357
free,386
free,417
free,361
free,341
free,431
malloc,341
malloc,316
malloc,326
malloc,326
malloc,311
...

I need to plot this data using pgfplots to look something like (just a sketch):

A sketch of what I need

My questions are:

  • Is this possible?
  • How do I get the bars so tightly packed? It would even be okay if they were completly adjacent. I may have 500-1000 data points (operations) to plot.
  • How can I transform the "malloc", "realloc" and "free" in the data into red, green and blue?

Many thanks in advance!

share|improve this question
    
Did you play around width bar width key? –  percusse May 18 at 21:02
    
Since your horizontal axis consists of discrete timesteps, I think what you are looking for is a comb plot; not a bar plot. Also, using malloc, realloc etc. to decide the colour is, as far as I know, only supported for scatter plots that make use of point meta=explicit symbolic and scatter/classes. I'd say your best solution would be to save separate data files for malloc, realloc etc. that store the timestep (x axis) in one column and the time (y axis) in the other, and use three \addplots in a ycomb plot. –  sudosensei May 18 at 22:11

2 Answers 2

up vote 5 down vote accepted

The code:

\documentclass{standalone}

% this section just for the example
\usepackage{filecontents}
\begin{filecontents}{malloc.dat}
operation time
1 331
2 386
3 326
4 321
5 321
\end{filecontents}

\begin{filecontents}{realloc.dat}
operation time
6 502
7 662
8 536
9 477
10 441
\end{filecontents}

\begin{filecontents}{free.dat}
operation time
11 321
12 327
13 321
14 402
15 357
\end{filecontents}
% end of the section just for the example

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  bar cycle list/.style={/pgfplots/cycle list={%
    {black,fill=red!70!black,mark=none},%
    {black,fill=green!70!black,mark=none},%
    {black,fill=blue!70!black,mark=none},%
    {black,fill=orange!70!black,mark=none},%
    }
  },
  width=7cm,
  ybar=0pt,
  bar width=1,
  bar shift=0pt,
  xtick={0,5,10,15},
  xlabel={operation \#},
  ylabel={time [$\mathrm{ns}$]},
]

\addplot+[] table[x=operation,y=time] {malloc.dat};
\addplot+[] table[x=operation,y=time] {realloc.dat};
\addplot+[] table[x=operation,y=time] {free.dat};

\legend{malloc(),realloc(),free()};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

The answers:

  1. Yes.

  2. Adjust the values for the width, bar width, and bar shift options; in the code above I used

      width=7cm,
      ybar=0pt,
      bar width=1,
      bar shift=0pt
    
  3. You can change the bar cycle list style to cycle through the colors of your choice..

share|improve this answer
    
Thanks a lot! I've accepted both your's and sudosensei's answers because they are both great. –  estan May 19 at 4:04
    
I realized you can't accept multiple answers, so I'm accepting yours since yours has bars :) But sudosensei's was really good as well and gave me some food for thought. –  estan May 19 at 4:14
    
I had tried this, but without utilising bar shift. I learned something new today! Great answer. @estan This should definitely be the accepted answer. –  sudosensei May 19 at 9:20

Since your horizontal axis consists of discrete timesteps, I think what you are looking for is a comb plot; not a bar plot. Also, using malloc, realloc etc. to decide the colour is, as far as I know, only supported for scatter plots that make use of point meta=explicit symbolic and scatter/classes. I'd say your best solution would be to save separate data files for malloc, realloc and free that store the timestep (x axis) in one column and the time (y axis) in the other, and use three \addplots in a ycomb plot.

The result is not as pretty as I'd like it to be. You can disable the markers on the top using no markers in the axis options, but then the legend entries show nothing. I don't know enough about pgfplots to fix this unfortunately, but hopefully this is a step in the right direction.

Output:

Output

Code:

\documentclass{standalone}

\usepackage{filecontents}

\begin{filecontents}{malloc.dat}
operation time
1 331
2 386
3 326
4 321
5 321
16 341
17 316
18 326
19 326
20 311
\end{filecontents}

\begin{filecontents}{realloc.dat}
operation time
6 502
7 662
8 536
9 477
10 441
\end{filecontents}

\begin{filecontents}{free.dat}
operation time
11 321
12 327
13 321
14 402
15 357
\end{filecontents}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  width=10cm,
  height=10cm,
  ycomb,
  xlabel={operation \#},
  ylabel={time [$\mathrm{ns}$]},
  legend style={
    nodes={right},
  },
]

  \addplot+[very thick] table[x=operation,y=time] {malloc.dat};
  \addplot+[very thick] table[x=operation,y=time] {realloc.dat};
  \addplot+[very thick] table[x=operation,y=time] {free.dat};

  \legend{\texttt{malloc()},\texttt{realloc()},\texttt{free()}};

\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
Awesome. Thanks! I'll play a bit with it to see what fits best, but yea, splitting the data like that is probably a good idea (and no problem at all). –  estan May 19 at 4:05

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.