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},
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}
Best Answer
You can add a new plot with a different plot type by using
\addplot [<plot type>] ...;
.If you want to add a line plot to a column plot, you would use
\addplot [sharp plot] ...;
.If you want to keep the current color cycle uninterrupted, add a
+
in front of the options:\addplot +[sharp plot] ...;
To calculate the cumulated sums automatically, you can make use of the PGFplotstable package that ships with PGFplots. It allows you to save data into tables which can then be fed to PGFplots using
\addplot table {<\tablemacro>}
. You create the table usingwhere the first data line is the header that assigns the column names, and the
\results
is the macro the table is saved to. To create a new column that contains the cumulated results, you issuewhich tells PGFplotstable that whenever you access the column
Cumulated
(which does not exist yet), it will automatically create a new column consisting of the sum of\pgfmathaccuma
(initially empty) and the current value ofCount
.In your plot, you would then just say
Here's the complete code: