Tikzpicture:在条形图中的条形顶部添加圆圈

Tikzpicture:在条形图中的条形顶部添加圆圈

我正在尝试在“tikzpicture”中的条形图中的条形顶部添加一个圆圈。我的问题是我无法让条形顶部的圆圈以条形为中心。它们目前以 x 轴上的标签为中心。有人能解决这个问题吗?欢迎提出任何其他改进我的代码的建议!

在此处输入图片描述

\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

您可以使用mark options={xshift=\pgfplotbarshift}

在此处输入图片描述

代码:

\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}

相关内容