2

I found many, many helpful things on tex.stackexchange for pgfplots - thanks for that!

But i can't figure out how to:

  • insert a new line after "without" or "with"
  • hide value 0 or the whole bar
  • change the x tick's so there's no decimal power ([10^..])
  • prevent nodes near coords of beein plottet outside the chart

the two charts:

changes in stacked barchart

and the sourcecode:

\documentclass[a4paper]{report}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}

\title{Tests}

\pgfplotsset{counter_barchart/.style={
  width=0.85\textwidth,
  height=5cm,
  xbar,
  xmin=0,
  xmajorgrids = true,
  tick align = outside, xtick pos = left,
  x tick label style={ /pgf/number format/1000 sep=},
  enlarge y limits=0.4,
  symbolic y coords={Ass. Optimization,with Optimization,without Optimization},
  ytick=data,
  yticklabel style={text width=0.2\textwidth},
  every node near coord/.append style={/pgf/number format/1000 sep=},
  nodes near coords,
  nodes near coords align={horizontal},
  legend style={at={(0.5,-0.35)},anchor=north,legend columns=-1},
  reverse legend
}}

\begin{document}

\begin{tikzpicture}
 \begin{axis}[counter_barchart]
  \addplot coordinates {(0,Ass. Optimization)(19243,with Optimization) (8898,without Optimization)};
  \addplot coordinates {(7854,Ass. Optimization) (6652,with Optimization) (6548,without Optimization)};
  \legend{Second,First}
 \end{axis}
\end{tikzpicture}
\\

\begin{tikzpicture}
 \begin{axis}[counter_barchart]
  \addplot coordinates {(3985,without Optimization) (5456,with Optimization)};
  \addplot coordinates {(5223,without Optimization) (11054,with Optimization)};
  \legend{Second,First}
 \end{axis}
\end{tikzpicture}

\end{document}
2
  • Hi, welcome to the site! Questions work best if they are only concerned with one issue at a time. That way, other users with the same problem stand a better chance of finding the question. In this case, your third question is answered at How do you remove the axis multiplier?. About your fourth question: What behaviour would you prefer? Do you want the label to be cut off, or should the axis limits adjust to accommodate the labels?
    – Jake
    May 9, 2014 at 10:57
  • okay! i'll ask one after one :) i was able to remove the axis multiplier. I prefer the second option - the axis limits adjust to accommodate the labels.
    – user51384
    May 9, 2014 at 11:53

1 Answer 1

4

To avoid the hyphenation in the yticklabel:

yticklabel style={text width=0.2\textwidth,align=flush left},

To hide value 0 as nodes near coords:

nodes near coords={%
  \pgfmathtruncatemacro\NNC{\pgfkeysvalueof{/data point/x}}%
  \ifnumequal{\NNC}{0}{}{\NNC}% needs package etoolbox
},

To avoid xtick scaling:

scaled x ticks=false,

To prevent nodes near coords of being plotted outside the chart:

enlarge x limits={0.15,upper},

enter image description here

Code:

\documentclass[a4paper]{report}

\usepackage{etoolbox}
\usepackage{pgfplots}

\pgfplotsset{
  counter_barchart/.style={
    width=0.85\textwidth,
    height=5cm,
    xbar,
    xmin=0,
    xmajorgrids = true,
    tick align = outside, xtick pos = left,
    x tick label style={/pgf/number format/1000 sep=},
    scaled x ticks=false,
    enlarge y limits=0.4,
    enlarge x limits={0.15,upper},
    symbolic y coords={Ass. Optimization,with Optimization,without Optimization},
    ytick=data,
    yticklabel style={text width=0.2\textwidth,align=flush left},
    nodes near coords={%
      \pgfmathtruncatemacro\NNC{\pgfkeysvalueof{/data point/x}}%
      \ifnumequal{\NNC}{0}{}{\NNC}%
    },
    nodes near coords align={horizontal},
    legend style={at={(0.5,-0.35)},anchor=north,legend columns=-1},
    reverse legend
  }
}

\begin{document}

\begin{tikzpicture}
 \begin{axis}[counter_barchart,xtick={0,4000,8000,12000,16000,20000}]
  \addplot coordinates {(0,Ass. Optimization)(19243,with Optimization) (8898,without Optimization)};
  \addplot coordinates {(7854,Ass. Optimization) (6652,with Optimization) (6548,without Optimization)};
  \legend{Second,First}
 \end{axis}
\end{tikzpicture}

\vspace{1cm}
\begin{tikzpicture}
 \begin{axis}[counter_barchart]
  \addplot coordinates {(3985,without Optimization) (5456,with Optimization)};
  \addplot coordinates {(5223,without Optimization) (11054,with Optimization)};
  \legend{Second,First}
 \end{axis}
\end{tikzpicture}

\end{document}
1
  • wow! now they look like they should :) thanks a lot!
    – user51384
    May 12, 2014 at 8:41

You must log in to answer this question.

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