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'd like to plot data files with multiple values per x-coordinate using pgfplots, ranging over the time span of a month (max. 31 days) per file. The problem is, that if I don't plot these values stacked or on top of each other, it is way too much for the width of a single A4 page. Originally I had 9 values per day, but now that I've cut it down to 3 the problem still persists.

Here is an example datafile:

userdata.dat:

Day        UsrsBnnd  UsrsRprtd UsrsRegd  
1          0         0         3
2          0         0         1
3          0         0         1
4          1         0         3
5          1         0         5
6          0         0         1
7          0         0        43
8          0         0        75
9          2         0        40
10         0         0         8
11         0         0         9
12         1         1         4
13         1         1         5
14         0         0        10
15         0         0         8
16         2         1         3
17         0         0         4
18         1         0         5
19         0         0         3
20         0         0        18
21         0         0         8
22         0         0         3
23         0         0        15
24         2         1         2
25         2         2         2
26         0         0         3
27         0         0         5
28         0         0         4
29         0         0        13
30         0         0        21
31         0         0        12

At the moment, I plot it by drawing the columns on top of each other with an opacity of 33%, but that does not create a readable, yet alone neatly arranged graphic.

MWE.tex:

\documentclass{article}
\usepackage{pgfplots}
\usepackage[margin=2cm]{geometry}

\begin{document}
\pagestyle{empty}

\begin{tikzpicture}
    \begin{axis}[/tikz/ybar, ybar legend, ymin=0, ylabel=Amount, bar width=12pt, axis x line*=left, nodes near coords=\rotatebox{90}{\scriptsize\pgfmathprintnumber\pgfplotspointmeta}, enlarge x limits=0.02, xlabel=Days, xtick=data, width=\textwidth]
        \addplot[red,fill=red!40!white,opacity=0.33] table[y index = 1] {userdata.dat};
        \addplot[yellow,fill=yellow!40!white,opacity=0.33] table[y index = 2] {userdata.dat};
        \addplot[green,fill=green!40!white,opacity=0.33] table[y index = 3] {userdata.dat};
        \legend{Users Banned, Users Reported, Users Registered}
    \end{axis}
\end{tikzpicture}

\end{document}

userdata.dat plotted using MWE.tex

I'd like some suggestions on how to handle the plotting of this and similar datafiles for best results, as embedding something like I've currently got in a paper would be counterintuitive to readers. I'd like to keep the resulting graphics as narrow as possible, because multiple plots are supposed to end up in the text, which shouldn't make up the majority of the content.

share|improve this question

1 Answer 1

up vote 7 down vote accepted

I would use three different axes for this, for example using the groupplots library:

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage{filecontents}

\begin{filecontents}{userdata.dat}
Day        UsrsBnnd  UsrsRprtd UsrsRegd  
1          0         0         3
2          0         0         1
3          0         0         1
4          1         0         3
5          1         0         5
6          0         0         1
7          0         0        43
8          0         0        75
9          2         0        40
10         0         0         8
11         0         0         9
12         1         1         4
13         1         1         5
14         0         0        10
15         0         0         8
16         2         1         3
17         0         0         4
18         1         0         5
19         0         0         3
20         0         0        18
21         0         0         8
22         0         0         3
23         0         0        15
24         2         1         2
25         2         2         2
26         0         0         3
27         0         0         5
28         0         0         4
29         0         0        13
30         0         0        21
31         0         0        12
\end{filecontents}

\begin{document}
\pagestyle{empty}

\pgfplotsset{compat=1.9}
\begin{tikzpicture}
    \begin{groupplot}[
        ybar=0pt,
        /pgf/bar width=1,
        ymin=0, xmin=0.5, xmax=31.5,
        y=1.5mm,
        width=\textwidth,
            group style={
                group size=1 by 3,
                xticklabels at=edge bottom,
                yticklabels at=edge left,
                vertical sep=5pt,
            },
            ylabel style={rotate=-90},
            axis x line*=left,
            point meta=explicit symbolic,
            every node near coord/.append style={font=\small},
            nodes near coords={\ifnum\pgfplotspointmeta>0 \pgfplotspointmeta\fi},
            cycle list={fill=gray!50, draw=black}
        ]
    \nextgroupplot [ylabel=Registered]
       \addplot +[fill=green!50] table [y index=3, meta index=3] {userdata.dat};
    \nextgroupplot [ymax=6, try min ticks=2, ylabel=Reported]
       \addplot +[fill=yellow!50] table [y index=1, meta index=1] {userdata.dat};
    \nextgroupplot [ymax=6, try min ticks=2, ylabel=Banned] 
       \addplot +[fill=red!50] table [y index=2, meta index=2] {userdata.dat};
    \end{groupplot}
\end{tikzpicture}

\end{document}
share|improve this answer
    
Thank you, this answer is a great way to handle the example datafile! It looks great and is very narrow, which makes embedding easy. Still, if the second and third column contain high values, too, it results in a quite big plot, spanning around the height of a page. –  Big-Blue Feb 15 at 13:02
    
@Big-Blue: In that case it would make sense to decrease the y=1.5mm in such a way that the overall height remains roughly constant. –  Jake Feb 15 at 13:05
    
Thank you, I believe this is the ideal solution. With a few minimal tweaks, it should be possible to automate generation of the plotting code for each data file and get perfectly scaled results. –  Big-Blue Feb 15 at 13:18

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.