# pgfplots stacked bar chart with markers

I have a stacked bar chart. I would like to add markers to the chart in some values. I am trying to create something like in the stacked bar chart. Is it possible to add markers instead of another stacked bar in this chart?

Also there are few more questions if possible -

1. I want to set ymin=0, but this is shifting the bar chart upwards. Is there anything wrong I am doing?

2. How to add shadow to the bars in the graph? I am getting dimension too large error.

3. How to flatten the graph such that I get the same aspect ratio as I have in the example figure below.

I have following code that works already.

\documentclass[tikz,border=5pt]{standalone}

\usepackage[T1]{fontenc}
\usepackage[oldstylenums]{kpfonts}
\usepackage{microtype}
\usepackage{pifont}
\usepackage{amssymb}
\usepackage{latexsym}
\usepackage{amsfonts}
\usepackage{pgf}
\usepackage{pgfplots}

\pgfplotsset{compat=1.8}

\usepackage{tikz}

\begin{document}
\pgfplotsset{major grid style={gray!50}}
\definecolor{step1Col}{HTML}{CC0000}
\definecolor{step2Col}{HTML}{CCCC99}
\definecolor{step3Col}{HTML}{003366}
\definecolor{step4Col}{HTML}{996600}
\definecolor{step5_6Col}{HTML}{669966}
\definecolor{step7Col}{HTML}{666699}
\definecolor{step8Col}{HTML}{FFCC00}

\begin{tikzpicture}
\begin{axis}[
ybar stacked,
draw=none,
bar width=8pt,
enlargelimits=0.15,
ylabel={clock cycles},
xlabel={tile mapping(VLD, IQ/IDCT, CC)},
ymajorgrids,
y tick label style={font=\tiny,major tick length=0pt},
x tick label style={font=\tiny,major tick length=0pt},
xticklabels ={2-1-2, 2-1-3, 2-2-3, 2-2-2, 2-3-2, 2-3-3, 3-3-3, 2-3-3, 2-2-3, 2-2-2, 2-1-2, 2-3-2},
xtick=data,
xmin=1,
xmax=10,
ymin=1500000,
ymax=12000000,
axis line style={ultra thin,white},
legend style={
legend cell align=left,
at={(1.20,1.00)},
anchor=north,
append after command={
\pgfextra{
\draw[draw=none,
(\tikzlastnode.south west)rectangle(\tikzlastnode.north east);
}
},  },
]

\legend{step1,step2, step3, step4, step5+6, step7, step8}
\end{axis}
\end{tikzpicture}
\end{document}


And the plot1.csv is

Number  Step1   Step2   Step3   Step4   Step5_6 Step7   Step8   Predicted
0   50  138 2025137 1400    15859   1358    50  11788769
1   50  894 2088724 1898    14662   2035    50  7564508
2   50  1610    3482495 1405    11490   1302    50  5970268
3   50  871 2089859 898 5021    569 50  7864363
4   50  138 3470704 1405    15888   1302    50  11788769
5   50  871 3481357 1909    11110   1324    50  7560008
6   50  871 2089855 2476    16015   885 50  7878218
7   50  1375    4875299 1903    17401   1258    50  11791029
8   50  877 2786201 1405    10704   1358    50  7871713
9   50  894 2733003 898 5027    569 50  7864363
10  50  138 3481371 1400    15882   1302    50  11788769
11  50  894 2088720 1405    18347   1302    50  7566933


Also yet another data set I would like to plot is -

Number  Step1   Step2   Step3   Step4   Step5_6 Step7   Step8   Predicted
0   50  138 0   1400    15859   1358    50  19770
1   50  894 0   1898    14662   2035    50  20100
2   50  1610    0   1405    11490   1302    50  16370
3   50  871 0   898 5021    569 50  7500
4   50  138 0   1405    15888   1302    50  19770
5   50  871 0   1909    11110   1324    50  15600
6   50  871 0   2476    16015   885 50  21355
7   50  1375    0   1903    17401   1258    50  22030
8   50  877 0   1405    10704   1358    50  14850
9   50  894 0   898 5027    569 50  7500
10  50  138 0   1400    15882   1302    50  19770
11  50  894 0   1405    18347   1302    50  22525


In the table, I would like to plot the predicted values as small squares.

Thanks !

I would really not recommend this plot because basically it's not readable and all data is dominated by Step 3 and predicted ones. First the question,

1. The plot goes up because you enlarge both limits, you need enlarge x limits=0.15,

2. I didn't get that error with your code.

3. You can do that via both declaring a width and a height dimension.

I cleaned up a bit, the code and the result is

\documentclass[tikz,border=5pt]{standalone}
\usepackage[oldstylenums]{kpfonts}
\usepackage{pgfplots,filecontents}
\pgfplotsset{compat=1.10}
\pgfplotsset{major grid style={gray!50}}

\definecolor{step1Col}{HTML}{CC0000}
\definecolor{step2Col}{HTML}{CCCC99}
\definecolor{step3Col}{HTML}{003366}
\definecolor{step4Col}{HTML}{996600}
\definecolor{step5_6Col}{HTML}{669966}
\definecolor{step7Col}{HTML}{666699}
\definecolor{step8Col}{HTML}{FFCC00}

\begin{filecontents*}{plot1.csv}
Number      Step1       Step2       Step3           Step4       Step5_6         Step7           Step8       Predicted
0           50          138         2025137         1400        15859           1358            50          11788769
1           50          894         2088724         1898        14662           2035            50          7564508
2           50          1610        3482495         1405        11490           1302            50          5970268
3           50          871         2089859         898         5021            569             50          7864363
4           50          138         3470704         1405        15888           1302            50          11788769
5           50          871         3481357         1909        11110           1324            50          7560008
6           50          871         2089855         2476        16015           885             50          7878218
7           50          1375        4875299         1903        17401           1258            50          11791029
8           50          877         2786201         1405        10704           1358            50          7871713
9           50          894         2733003         898         5027            569             50          7864363
10          50          138         3481371         1400        15882           1302            50          11788769
11          50          894         2088720         1405        18347           1302            50          7566933
\end{filecontents*}
\begin{document}
\begin{tikzpicture}[]
\begin{axis}[myplot/.style={ybar,draw=none,area legend},
width=10cm,height=5cm,
bar width=10pt,
enlarge x limits=0.15,
ylabel={clock cycles},
xlabel={tile mapping(VLD, IQ/IDCT, CC)},
ymajorgrids,
y tick label style={font=\tiny,major tick length=0pt},
x tick label style={font=\tiny,major tick length=0pt},
xticklabels ={2-1-2, 2-1-3, 2-2-3, 2-2-2, 2-3-2, 2-3-3, 3-3-3, 2-3-3, 2-2-3, 2-2-2, 2-1-2, 2-3-2},
xtick=data,
xmin=1,xmax=10,
ymin=1,ymax=1.5e7,
axis line style={draw=none},
legend style={legend cell align=left,at={(1.20,1.00)},anchor=north,
(\tikzlastnode.south west)rectangle(\tikzlastnode.north east);
}
}
},
legend image post style={draw opacity=0},
legend entries={Step 1,Step 2,Step 3,Step 4,Step 5+6,Step 7,Step 8,Predicted}
]
\end{axis}
\end{tikzpicture}
\end{document}


As you can see, most of your data vanished and you have strange entries in your legend because those datasets are invisible.

Instead I can think of two options,

• clean up the legend and mention only step3, step 5+6, and predicted columns with a disclaimer that the remaining step contribution is negligible and comparable

• Combine your negligible entries into a sum and plot that but I can't judge whether it would be a good idea here for your application.

• i am almost convinced to give you the bounty. except can you tell me, how to add shadow to the bars? sorry in my question i didn't mention it precisely. It should be shadow to bars and not to the legend. Apr 23 '14 at 15:20
• If I modify the style as myplot/.style={ybar,draw=none,area legend,blur shadow}, it will give me dimension too large error. Apr 23 '14 at 15:27
• I think you ignored that it was a stacked bar chart. I tried to replace ybar to ybar stacked and your solution didn't work for me. I added yet another data set which I use to plot the same graph, which I have added to the question. You are right that step3 dominates, but thats what I want to project. I have two graphs to prove this point, for which I used two different data sets. Apr 23 '14 at 15:51
• @Raj True. I misread the question. I'll have a look when I have the chance Apr 23 '14 at 21:21

This is another attempt with shade capability which needs to download \usetikzlibrary{shadows.blur} manually and the following options in \addplot[]. Use pdflatex please.

blur shadow={shadow yshift=0pt, shadow xshift=2pt}]  % where x pt can be altered to suit ones needs.


My study shows dimension error is due to ybar stacked because after being stacked, the datum exceeds the ranges setting.

And the second datum is also plotted.

First set of datum generates

Second set of datum yields, with the following options changed slightly (due to different datum after all)

bar width=5pt, height=5cm, width=15cm,
enlarge x limits={upper, value=0.15},ymax=25000
\addplot +[only marks, mark=square*,draw opacity=0,area     % for the last plot
legend,fill=green,xshift=0.25cm,legend image post style={xshift=-0.25cm}  %for correct legend position
] table[x=Number,y=Predicted] {plot1.csv};


Code

\documentclass[tikz,border=5pt]{standalone}

\usepackage[T1]{fontenc}
\usepackage[oldstylenums]{kpfonts}
\usepackage{microtype}
\usepackage{pifont}
\usepackage{amssymb}
\usepackage{latexsym}
\usepackage{amsfonts}
\usepackage{pgf,filecontents}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepackage{tikz}

\begin{filecontents*}{plot1.csv}
Number      Step1       Step2       Step3           Step4       Step5_6         Step7           Step8       Predicted
0           50          138         2025137         1400        15859           1358            50          11788769
1           50          894         2088724         1898        14662           2035            50          7564508
2           50          1610        3482495         1405        11490           1302            50          5970268
3           50          871         2089859         898         5021            569             50          7864363
4           50          138         3470704         1405        15888           1302            50          11788769
5           50          871         3481357         1909        11110           1324            50          7560008
6           50          871         2089855         2476        16015           885             50          7878218
7           50          1375        4875299         1903        17401           1258            50          11791029
8           50          877         2786201         1405        10704           1358            50          7871713
9           50          894         2733003         898         5027            569             50          7864363
10          50          138         3481371         1400        15882           1302            50          11788769
11          50          894         2088720         1405        18347           1302            50          7566933
\end{filecontents*}

\begin{document}
\pgfplotsset{major grid style={gray!50}}
\definecolor{step1Col}{HTML}{CC0000}
\definecolor{step2Col}{HTML}{CCCC99}
\definecolor{step3Col}{HTML}{003366}
\definecolor{step4Col}{HTML}{996600}
\definecolor{step5_6Col}{HTML}{669966}
\definecolor{step7Col}{HTML}{666699}
\definecolor{step8Col}{HTML}{FFCC00}

\begin{tikzpicture}
\begin{axis}[scale=1,
%    ybar stacked,
%draw=none,
bar width=10pt, height=5cm, width=10cm,
enlarge x limits=0.15,
ylabel={clock cycles},
xlabel={tile mapping(VLD, IQ/IDCT, CC)},
ymajorgrids,
xtick=data,
y tick label style={font=\tiny,major tick length=0pt},
x tick label style={font=\tiny,major tick length=0pt},
xticklabels ={2-1-2, 2-1-3, 2-2-3, 2-2-2, 2-3-2, 2-3-3, 3-3-3, 2-3-3, 2-2-3, 2-2-2, 2-1-2, 2-3-2},
%xticklabel shift={(-1cm,0 cm)},
x tick label style={xshift=-0.4cm,anchor=north},
ybar=2pt,
xmin=0,   xmax=10,
ymin=1,   ymax=12000000,
axis line style={ultra thin,white},
legend style={
legend cell align=left,
at={(1.20,1.00)},
anchor=north,
append after command={
\pgfextra{
\draw[draw=none,
(\tikzlastnode.south west)rectangle(\tikzlastnode.north east);
}
},  },
]

] table[x=Number,y=Step2] {plot1.csv};

] table[x=Number,y=Step3] {plot1.csv};

] table[x=Number,y=Step4] {plot1.csv};

] table[x=Number,y=Step5_6] {plot1.csv};

] table[x=Number,y=Step7] {plot1.csv};

] table[x=Number,y=Step8] {plot1.csv};

\addplot +[only marks, mark=square*,draw opacity=0,area legend,fill=green, xshift=-0.4cm,legend image post style={xshift=0.4cm}
] table[x=Number,y=Predicted] {plot1.csv};

\legend{step1,step2, step3, step4, step5+6, step7, step8, Predicted}
\end{axis}
\end{tikzpicture}
\end{document}

• I agree to your solution, but what I really look for is a stacked bar chart. Apr 25 '14 at 7:54
• @Raj -- No problem, I will think more. Thanks for the feedback. Apr 25 '14 at 8:26

I think I have the solution for this problem. What I have to do is kind of overlay of one axis over the other. If I maintain the same axis parameters, I get the solution. The only thing remains is to tweak the legend, but it is trivial.

Of course for datum2, we will have to modify the ymax value and yticklabels.

\pgfplotsset{major grid style={gray!50}}
\definecolor{step1Col}{HTML}{CC0000}
\definecolor{step2Col}{HTML}{CCCC99}
\definecolor{step3Col}{HTML}{003366}
\definecolor{step4Col}{HTML}{996600}
\definecolor{step5_6Col}{HTML}{669966}
\definecolor{step7Col}{HTML}{666699}
\definecolor{step8Col}{HTML}{FFCC00}
\definecolor{predictedCol}{HTML}{266A2E}

\begin{tikzpicture}
\begin{axis}[
ybar stacked,
width=10cm,height=5cm,
bar width=8pt,
enlarge x limits=0.15,
ylabel={clock cycles},
xlabel={tile mapping(VLD, IQ/IDCT, CC)},
ymajorgrids,
y tick label style={font=\tiny,major tick length=0pt},
x tick label style={font=\tiny,major tick length=0pt},
xticklabels ={2-1-2, 2-1-3, 2-2-3, 2-2-2, 2-3-2, 2-3-3, 3-3-3, 2-3-3, 2-2-3, 2-2-2, 2-1-2, 2-3-2},
xtick=data,
ytick={0,2000000,4000000,6000000,8000000,10000000,12000000},
xmin=1,
xmax=10,
ymin=0,
ymax=12000000,
axis line style={draw=none},
legend style={
legend cell align=left,
at={(1.20,0.90)},
anchor=north,
append after command={
\pgfextra{
\draw[draw=none,
(\tikzlastnode.south west)rectangle(\tikzlastnode.north east);
}
},  },
]

\legend{step1,step2, step3, step4, step5+6, step7, step8, predicted}
\end{axis}

\begin{axis}[
width=10cm,
height=5cm,
xmin=1,
xmax=10,
ymin=0,
ymax=12000000,
enlarge x limits=0.15,
axis line style={draw=none},
xtick=data,
ytick={},
xticklabels ={},
yticklabels ={},
y tick label style={font=\tiny,major tick length=0pt},
x tick label style={font=\tiny,major tick length=0pt},
]

\addplot[only marks, mark=square*,draw opacity=0,    % for the last plot
fill=predictedCol,legend image post style={xshift=0.2cm}  %for correct legend position
] table[x=Number,y=Predicted] {plot1.csv};

\end{axis}
\end{tikzpicture}