2

I want to create a bar graph using data from a table. Tikz automatically groups my data, but I need a different group size. The code that I wrote creates the following output: enter image description here

But I want groups of size 3. Something like: enter image description here

Here is my code:

\documentclass[border=1pt]{standalone}
\usepackage{pgfplots, pgfplotstable}

\pgfplotsset{width=10cm, height=5cm, compat=1.8}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread{
X   Contention   alpha   Name         Test1     Test2     Test3    Test4
1   0            0.01   DT             7        49       24       0
2   0            0.50   DT             6        37       54       2
3   0            0.99   DT             7        42       48       4
4   100          0.01   DT             7        49       24       0
6   100          0.50   DT             7        42       46       4
7   100          0.99   DT             8        46       54       2
8   0            0.01   EU             22       50       8        0
9   0            0.50   EU             16       65       30       1
11  0            0.99   EU             16       69       26       2
12  100          0.01   EU             28       46       6        0
13  100          0.50   EU             16       69       31       0
14  100          0.99   EU             17       68       40       3  
}\datatable

\begin{axis}[
ybar stacked,
 bar width=8pt,
 legend style={at={(0.2,0.9)},
    anchor=north},
 legend cell align={left}, % The command for legend alignment
xtick=data,
xticklabels from table={\datatable}{alpha},
xticklabel style={rotate=75,xshift=0ex,anchor=mid east},
]

\addplot table [x=X, y=Test1] {\datatable}; 
\addplot table [x=X, y=Test2] {\datatable}; 
\addplot table [x=X, y=Test3] {\datatable}; 
\addplot table [x=X, y=Test4] {\datatable};
\end{axis}
\end{tikzpicture}
\end{document}
2

Change X column as 1 2 3, 5 6 7, 9 10 11, 13 14 15.

\documentclass[border=1pt]{standalone}
\usepackage{pgfplots, pgfplotstable}

\pgfplotsset{width=10cm, height=5cm, compat=1.8}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread{
X   Contention   alpha   Name         Test1     Test2     Test3    Test4
1   0            0.01   DT             7        49       24       0
2   0            0.50   DT             6        37       54       2
3   0            0.99   DT             7        42       48       4
5   100          0.01   DT             7        49       24       0
6   100          0.50   DT             7        42       46       4
7   100          0.99   DT             8        46       54       2
9   0            0.01   EU             22       50       8        0
10   0            0.50   EU             16       65       30       1
11  0            0.99   EU             16       69       26       2
13  100          0.01   EU             28       46       6        0
14  100          0.50   EU             16       69       31       0
15  100          0.99   EU             17       68       40       3  
}\datatable

\begin{axis}[
ybar stacked,
 bar width=8pt,
 legend style={at={(0.2,0.9)},
    anchor=north},
 legend cell align={left}, % The command for legend alignment
xtick=data,
xticklabels from table={\datatable}{alpha},
xticklabel style={rotate=75,xshift=0ex,anchor=mid east},
]

\addplot table [x=X, y=Test1] {\datatable}; 
\addplot table [x=X, y=Test2] {\datatable}; 
\addplot table [x=X, y=Test3] {\datatable}; 
\addplot table [x=X, y=Test4] {\datatable};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |
  • Can you clarify how ybar stacked works? Why doesn't work keeping 4,8 and 12? – Colo Oct 20 at 12:57

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.