5

I am trying to do a stacked histogram, and I am trying to start from the code provided in this question : Tikz Stacked Bar Chart with Text

Yet, I would like to invert the axis, so that the A,B,C... labels are on the x coordinate, whilst the value on the y coordinate. What I would also like is the chart to take all the available width. Here is what I tried :

\documentclass[landscape]{article}

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

\definecolor{findOptimalPartition}{HTML}{D7191C}
\definecolor{storeClusterComponent}{HTML}{FDAE61}
\definecolor{dbscan}{HTML}{ABDDA4}
\definecolor{constructCluster}{HTML}{2B83BA}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar stacked,
    xtick=data,
    axis x line*=none,
    axis y line*=left,
    tick label style={font=\footnotesize},
    legend style={font=\footnotesize},
    label style={font=\footnotesize},
    ytick={0,100,200,300,400,500,600},
    width=.9\textwidth,
    bar width=6mm,
    ylabel={Time in ms},
    xticklabels={A, B, C, D, E, F},
    ymin=0,
    ymax=600,
    area legend,
    x=8mm,
    enlarge x limits={abs=0.625},
]
\addplot[findOptimalPartition,fill=findOptimalPartition] coordinates
{(0,20) (1,108) (2,21) (3,35) (4,12) (5,17)};
\addplot[storeClusterComponent,fill=storeClusterComponent] coordinates
{(0,20) (1,108) (2,21) (3,35) (4,12) (5,17)};
\addplot[dbscan,fill=dbscan] coordinates
{(0,20) (1,108) (2,21) (3,35) (4,12) (5,17)};
\addplot[constructCluster,fill=constructCluster] coordinates
{(0,20) (1,108) (2,21) (3,35) (4,12) (5,17)};
\end{axis}  
\end{tikzpicture}
\end{document}

So here I just tried to invert the axis, and yet I am getting an Overfull \hbox of 13000pt. As for the chart taking all the available space, I thought about changing the line x=8mm into something depending of \textwidth, is this a viable idea ?

  • 1
    Remove x=8mm,enlarge x limits={abs=0.625},. Better way to use entire text width is to use ` width=0.9\textwidth as you did. If you want more there is xscale as xscale=1.1 for example. – user11232 Feb 25 '15 at 13:24
3

Better way to control the width of a plot is to use width key. If you want full width, use width=\textwidth. Also, it is not a good idea to use

x=8mm,
enlarge x limits={abs=0.625},

for width adjustments. Hence remove them.

\documentclass[landscape]{article}

\usepackage{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\definecolor{findOptimalPartition}{HTML}{D7191C}
\definecolor{storeClusterComponent}{HTML}{FDAE61}
\definecolor{dbscan}{HTML}{ABDDA4}
\definecolor{constructCluster}{HTML}{2B83BA}

\begin{document}
\noindent  %% add this to avoid bad box
\begin{tikzpicture}
\begin{axis}[
    ybar stacked,
    xtick=data,
    axis x line*=none,
    axis y line*=left,
    tick label style={font=\footnotesize},
    legend style={font=\footnotesize},
    label style={font=\footnotesize},
    ytick={0,100,200,300,400,500,600},
    width=\textwidth,           %% this is enough
    bar width=6mm,
    ylabel={Time in ms},
    xticklabels={A, B, C, D, E, F},
    ymin=0,
    ymax=600,
    area legend,
    %x=8mm,
%    enlarge x limits={abs=0.625},
]
\addplot[findOptimalPartition,fill=findOptimalPartition] coordinates
{(0,20) (1,108) (2,21) (3,35) (4,12) (5,17)};
\addplot[storeClusterComponent,fill=storeClusterComponent] coordinates
{(0,20) (1,108) (2,21) (3,35) (4,12) (5,17)};
\addplot[dbscan,fill=dbscan] coordinates
{(0,20) (1,108) (2,21) (3,35) (4,12) (5,17)};
\addplot[constructCluster,fill=constructCluster] coordinates
{(0,20) (1,108) (2,21) (3,35) (4,12) (5,17)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You can also scale the x axis using xscale key if you are still not satisfied. For example, use xscale=2 instead of width=\textwidth to get

enter image description here

As you can see, this only scales the x axis making the plot to grow in its width and you may have to adjust yscale yourself. Or if you use scale=2 both x and y axes will be scaled.

enter image description here

Since these need more manual adjustment, it is better to use the width key.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy