1

I am using the following commands to draw a barchart.

\begin{tikzpicture}
        \pgfplotstableread{ % Read the data into a table macro
            Label       Female   Male  
            2006-2008    3     1 
            2007-2009    2     0
            2008-2010    9     2
            2019-2011    3     3
            2010-2012    11    1 
            2011-2013    6     0
            2012-2014    12    7
            2013-2015    10    7
            2014-2016    10    3
        }\datatable

        \begin{axis}[
        compat=newest, %Better label placement
%        axis on top,
        ybar stacked,   % Stacked horizontal bars
        xmin=0,              % Start x axis at 0
        ymax=25,
        ymin=0, ylabel={Number of students}, xlabel={Batches},bar width=10pt,
        y axis line style={opacity=0},
        yticklabels={\empty},
        ytick style={draw=none},enlarge y limits={upper, value=0.2},
        xtick=data,     % Use as many tick labels as x coordinates
        enlarge x limits = .2,
        xticklabels from table={\datatable}{Label}  % Get the labels from the Label column of the \datatable
        ]
        \addplot [fill=yellow] table [y=Male, x expr=\coordindex] {\datatable};    % Plot the "First" column against the data index
        \addplot [fill=green!70!blue]table [y=Female, x expr=\coordindex] {\datatable};
%        \addplot [fill=red!80!yellow] table [y=Third, x expr=\coordindex] {\datatable};
\legend{Male,Female}
        \end{axis}
        \end{tikzpicture}

I got the picture as,

enter image description here

  1. How to get the spaces properly on the x axis?

  2. How to place the y values on the corresponding bars?

  3. How to rotate the label 90 degree?

  4. How to take out the ticks on the top?

  5. Is there any other way of representing this data(two dim only)? (optional)

I hope all the questions can be answered in a single answer?

1

1 Answer 1

1
  1. See below.

  2. Add nodes near coords to the axis options.

  3. xticklabel style={rotate=90}

  4. You've basically answered that one yourself: xtick style={draw=none}


\documentclass[border=4mm,tikz]{standalone}
\usepackage{pgfplots,pgfplotstable}
\begin{document}
\begin{tikzpicture}
        \pgfplotstableread{ % Read the data into a table macro
            Label       Female   Male  
            2006-2008    3     1 
            2007-2009    2     0
            2008-2010    9     2
            2019-2011    3     3
            2010-2012    11    1 
            2011-2013    6     0
            2012-2014    12    7
            2013-2015    10    7
            2014-2016    10    3
        }\datatable

        \begin{axis}[
        compat=newest, %Better label placement
%        axis on top,
        ybar stacked,   % Stacked horizontal bars
        xmin=0,              % Start x axis at 0
        ymax=25,
        ymin=0, ylabel={Number of students}, xlabel={Batches},bar width=10pt,
        y axis line style={opacity=0},
        xtick style={draw=none}, % added
        yticklabels={\empty},
        ytick style={draw=none},enlarge y limits={upper, value=0.2},
        xtick=data,     % Use as many tick labels as x coordinates
        enlarge x limits = .2,
        xticklabels from table={\datatable}{Label},  % Get the labels from the Label column of the \datatable
        xticklabel style={rotate=90}, % added
        nodes near coords % added
        ]
        \addplot [fill=yellow] table [y=Male, x expr=\coordindex] {\datatable};    % Plot the "First" column against the data index
        \addplot [fill=green!70!blue]table [y=Female, x expr=\coordindex] {\datatable};
%        \addplot [fill=red!80!yellow] table [y=Third, x expr=\coordindex] {\datatable};
\legend{Male,Female}
        \end{axis}
        \end{tikzpicture}
\end{document}

Part 2

I don't think there's an option for automatically increasing the width in order to accommodate wide ticklabels, so you will have to set width=<some large enough length>. Below I decreased the font size of the xticklabels a bit, and modified the value for enlarge x limits.

\documentclass[border=4mm,tikz]{standalone}
\usepackage{pgfplots,pgfplotstable}
\begin{document}
\begin{tikzpicture}
        \pgfplotstableread{ % Read the data into a table macro
            Label       Female   Male  
            2006-2008    3     1 
            2007-2009    2     0
            2008-2010    9     2
            2019-2011    3     3
            2010-2012    11    1 
            2011-2013    6     0
            2012-2014    12    7
            2013-2015    10    7
            2014-2016    10    3
        }\datatable

        \begin{axis}[
        compat=newest, %Better label placement
%        axis on top,
        ybar stacked,   % Stacked horizontal bars
        xmin=0,              % Start x axis at 0
        ymax=25,
        ymin=0, ylabel={Number of students}, xlabel={Batches},bar width=10pt,
        y axis line style={opacity=0},
        xtick style={draw=none}, % added
        yticklabels={\empty},
        ytick style={draw=none},enlarge y limits={upper, value=0.2},
        xtick=data,     % Use as many tick labels as x coordinates
        enlarge x limits = 0.02, % modified
        xticklabels from table={\datatable}{Label},  % Get the labels from the Label column of the \datatable
        nodes near coords,
        width=14cm,
        xticklabel style={font=\footnotesize}
        ]
        \addplot [fill=yellow] table [y=Male, x expr=\coordindex] {\datatable};    % Plot the "First" column against the data index
        \addplot [fill=green!70!blue]table [y=Female, x expr=\coordindex] {\datatable};
%        \addplot [fill=red!80!yellow] table [y=Third, x expr=\coordindex] {\datatable};
\legend{Male,Female}
        \end{axis}
        \end{tikzpicture}
\end{document}
2
  • @Torbjon T. I mean that "without the rotate its should arrange itself according to the text width. for example it should be 2008-2010 2009-2011 not messing with each other.
    – David
    Commented Jul 3, 2016 at 14:04
  • @David Don't know if you noticed, but I updated my answer right after your previous comment. Commented Jul 3, 2016 at 20:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .