4

I am trying to append a circle to the top of the bars in my barchart in 'tikzpicture'. My problem is that I can't get the circles on top of the bars centered per bar. They are currently centered per label on the x-axis. Anybody has a fix for this? Any other suggestions to improve my code is always welcome!

enter image description here

\documentclass[]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{document}

\begin{filecontents}{testdata.csv}
index,  first,  second
1,  28, 42
2,  16, 8
3,  7,  0
4,  6,  8
5,  5,  8
6,  2,  0
7,  3,  0
8,  1,  0
9,  4,  8
10, 4,  0
11, 6,  17
12, 2,  0
13, 0,  0
14, 3,  0
15, 2,  0
16, 2,  0
17, 1,  0
18, 0,  0
19, 1,  0
20, 1,  0
21, 1,  0
22, 0,  0
23, 0,  0
24, 0,  0
25, 0,  0
26, 0,  0
27, 1,  0
28, 0,  0
29, 0,  0
30, 0,  0
31, 1,  8
32, 1,  0
33, 0,  0
34, 1,  0
35, 1,  0

\end{filecontents}

\pgfplotstableread[col sep=comma]{testdata.csv}\datatable

\begin{tikzpicture}[trim axis left]

\begin{axis}[
x tick label style={
    /pgf/number format/1000 sep=},
scale only axis,
height=12cm,
width=\textwidth,
ybar=0pt,
ymin=0,
every node near coord/.append style={font=\footnotesize},
xtick=data,
xticklabels from table={\datatable}{index},
xticklabel style={rotate=90},
yticklabel={\pgfmathparse{\tick*1}\pgfmathprintnumber{\pgfmathresult}\%},
yticklabel style={font=\scriptsize},
ymajorgrids,
xmajorgrids,
bar width=0.1cm,
xticklabel style={font=\tiny},
enlarge x limits=0.01,
enlarge y limits={value=0.15,upper},
]
\addplot+[mark=*, red, nodes near coords={\pgfmathfloatifflags{\pgfplotspointmeta}{0}{}{\pgfmathprintnumber{\pgfplotspointmeta}\%}},] table [x=index, y=first] {\datatable};
\addplot+[style={blue,fill=blue,mark=*},nodes near coords={\pgfmathfloatifflags{\pgfplotspointmeta}{0}{}{\pgfmathprintnumber{\pgfplotspointmeta}\%}},] table [x=index, y=second] {\datatable};

\end{axis}
\end{tikzpicture}

\end{document}

1 Answer 1

4

You could use mark options={xshift=\pgfplotbarshift}:

enter image description here

Code:

\documentclass[]{article}
\usepackage{pgfplotstable}% loads pgfplots
\pgfplotsset{compat=1.14}% <- added
\usepackage{filecontents}
\begin{document}
\begin{filecontents}{testdata.csv}
index,  first,  second
1,  28, 42
2,  16, 8
3,  7,  0
4,  6,  8
5,  5,  8
6,  2,  0
7,  3,  0
8,  1,  0
9,  4,  8
10, 4,  0
11, 6,  17
12, 2,  0
13, 0,  0
14, 3,  0
15, 2,  0
16, 2,  0
17, 1,  0
18, 0,  0
19, 1,  0
20, 1,  0
21, 1,  0
22, 0,  0
23, 0,  0
24, 0,  0
25, 0,  0
26, 0,  0
27, 1,  0
28, 0,  0
29, 0,  0
30, 0,  0
31, 1,  8
32, 1,  0
33, 0,  0
34, 1,  0
35, 1,  0
\end{filecontents}
\pgfplotstableread[col sep=comma]{testdata.csv}\datatable
\begin{tikzpicture}[trim axis left]
\begin{axis}[
x tick label style={
    /pgf/number format/1000 sep=},
scale only axis,
height=12cm,
width=\textwidth,
ybar=0pt,
ymin=0,
every node near coord/.append style={font=\footnotesize},
xtick=data,
xticklabels from table={\datatable}{index},
xticklabel style={rotate=90},
yticklabel={\pgfmathparse{\tick*1}\pgfmathprintnumber{\pgfmathresult}\%},
yticklabel style={font=\scriptsize},
ymajorgrids,
xmajorgrids,
bar width=0.1cm,
xticklabel style={font=\tiny},
enlarge x limits={abs=\pgfplotbarwidth},% <- changed
enlarge y limits={value=0.15,upper},
%%
nodes near coords={\pgfmathfloatifflags{\pgfplotspointmeta}{0}{}
    {\pgfmathprintnumber{\pgfplotspointmeta}\%}},
mark options={xshift=\pgfplotbarshift},% <- added
]
\addplot+[red,mark=*] table [x=index, y=first] {\datatable};
\addplot+[blue,mark=*] table [x=index, y=second] {\datatable};
\end{axis}
\end{tikzpicture}
\end{document}
0

You must log in to answer this question.

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