我有一个条形图,我想在其中添加一些叠加在条形上的单点。与误差线相比,这些点处于另一个尺度。下面,我用绿色表示这些单点。请注意,我还想在图例中添加一个引用这些点的条目。理想情况下,这些点应位于每对条形的中间。我在更改第二个 y 轴(units
)的范围时遇到了一些困难,它显示在范围内[0,1]
,但理想情况下希望它在范围内。是否还可以为每个单点添加标签?欢迎
[1,5]
对代码提出任何建议和改进;-)latex
\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\node [align=center, font=\small, rotate=45,
text width=2.15cm, inner sep=0.25cm] at (0.6cm, -0.65cm) {\textsc{year 1}};
\node [align=center, font=\small, rotate=45,
text width=2.15cm, inner sep=0.25cm] at (2.90cm, -0.65cm) {\textsc{year 2}};
\node [align=center,font=\small, rotate=45,
text width=2.15cm, inner sep=0.25cm] at (5.30cm, -0.73cm) {\textsc{year 3}};
\begin{axis}[
title style={align=center, yshift=-0.35em},
ybar,
ymin={60}, ymax={93.5},
width={8cm}, height={5cm},
bar width={7pt},
ticks=both,
ytick={60,70,80,90,100.0},
ylabel style={yshift=-0.65em},
ylabel={Percent},
ymajorgrids,
nodes near coords = \rotatebox{90}{{\pgfmathprintnumber[fixed zerofill, precision=2]{\pgfplotspointmeta}}},
nodes near coords align={vertical},
every node near coord/.append style={font=\small, yshift=0.25mm},
xtick = data,
table/header=false,
table/row sep=\\,
xticklabels={},
enlarge y limits={value=0.2,upper},
legend style={font=\tiny},
%legend pos=north west
%legend style={at={(0.5, 1.20)}, anchor=north, legend columns=2},
legend style={at={(0.5, -0.52)}, anchor=north, legend columns=2},
]
\addplot table[x expr=\coordindex,y index=0]{89.17\\71.71\\81.41\\};
\addplot table[x expr=\coordindex,y index=0]{70.60\\64.11\\84.92\\};
\draw (axis cs:0.52,0) -- ({axis cs:0.52,0}|-{rel axis cs:0.5,1});
\draw (axis cs:1.49,0) -- ({axis cs:1.49,0}|-{rel axis cs:0.5,1});
\draw (axis cs:2.50,0) -- ({axis cs:2.50,0}|-{rel axis cs:0.5,1});
\legend{\textsc{Prod 1}\hspace*{8pt}, \textsc{Prod 2} }
\end{axis}
\begin{axis}[
axis y line*={right},
axis x line={none},
ymin={1}, ymax={5},
width={8cm}, height={5cm},
ylabel style={yshift=0.60em},
ylabel={units},
ytick={1,2,3,4,5}
]
% add points here?
\end{axis}
\end{tikzpicture}
\end{document}
答案1
你可以像这样绘制这些圆圈
\begin{axis}[
axis y line*={right},
axis x line={none},
ymin={1}, ymax={5},
width={8cm}, height={5cm},
ylabel style={xshift=0.60em},
ylabel={units},
]
\addplot[only marks,mark=*,mark size=3pt,green,
nodes near coords = \rotatebox{90}{{\pgfmathprintnumber[fixed zerofill,
precision=2]{\pgfplotspointmeta}}},
nodes near coords align={vertical},
point meta=y,
every node near coord/.append style={font=\small, yshift=0.25mm},
] coordinates {
(1,3.5) (2,4) (3,2)
};
\end{axis}
并添加
\addlegendimage{draw=green,fill=green!30}
\addlegendentry{\textsc{Legend}}
添加额外的图例。您必须legend columns=3
另外制作。
完整代码:
\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\node [align=center, font=\small, rotate=45,
text width=2.15cm, inner sep=0.25cm] at (0.6cm, -0.65cm) {\textsc{year 1}};
\node [align=center, font=\small, rotate=45,
text width=2.15cm, inner sep=0.25cm] at (2.90cm, -0.65cm) {\textsc{year 2}};
\node [align=center,font=\small, rotate=45,
text width=2.15cm, inner sep=0.25cm] at (5.30cm, -0.73cm) {\textsc{year 3}};
\begin{axis}[
title style={align=center, yshift=-0.35em},
ybar,
ymin={60}, ymax={93.5},
width={8cm}, height={5cm},
bar width={7pt},
ticks=both,
ytick={60,70,80,90,100.0},
ylabel style={yshift=-0.65em},
ylabel={Percent},
ymajorgrids,
nodes near coords = \rotatebox{90}{{\pgfmathprintnumber[fixed zerofill, precision=2]{\pgfplotspointmeta}}},
nodes near coords align={vertical},
every node near coord/.append style={font=\small, yshift=0.25mm},
xtick = data,
table/header=false,
table/row sep=\\,
xticklabels={},
enlarge y limits={value=0.2,upper},
legend style={font=\tiny},
%legend pos=north west
%legend style={at={(0.5, 1.20)}, anchor=north, legend columns=2},
legend style={at={(0.5, -0.52)}, anchor=north, legend columns=3},
]
\addplot table[x expr=\coordindex,y index=0]{89.17\\71.71\\81.41\\};
\addplot table[x expr=\coordindex,y index=0]{70.60\\64.11\\84.92\\};
\draw (axis cs:0.52,0) -- ({axis cs:0.52,0}|-{rel axis cs:0.5,1});
\draw (axis cs:1.49,0) -- ({axis cs:1.49,0}|-{rel axis cs:0.5,1});
\draw (axis cs:2.50,0) -- ({axis cs:2.50,0}|-{rel axis cs:0.5,1});
\legend{\textsc{Prod 1}\hspace*{8pt}, \textsc{Prod 2}}
\addlegendimage{draw=green,fill=green!30}
\addlegendentry{\textsc{Legend}}
\end{axis}
\begin{axis}[
axis y line*={right},
axis x line={none},
ymin={1}, ymax={5},
width={8cm}, height={5cm},
ylabel style={xshift=0.60em},
ylabel={units},
]
\addplot[only marks,mark=*,mark size=3pt,green,
nodes near coords = \rotatebox{90}{{\pgfmathprintnumber[fixed zerofill,
precision=2]{\pgfplotspointmeta}}},
nodes near coords align={vertical},
point meta=y,
every node near coord/.append style={font=\small, yshift=0.25mm},
] coordinates {
(1,3.5) (2,4) (3,2)
};
\end{axis}
\end{tikzpicture}
\end{document}
如果您只想在某些点显示圆圈,最好画出来。
\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\node [align=center, font=\small, rotate=45,
text width=2.15cm, inner sep=0.25cm] at (0.6cm, -0.65cm) {\textsc{year 1}};
\node [align=center, font=\small, rotate=45,
text width=2.15cm, inner sep=0.25cm] at (2.90cm, -0.65cm) {\textsc{year 2}};
\node [align=center,font=\small, rotate=45,
text width=2.15cm, inner sep=0.25cm] at (5.30cm, -0.73cm) {\textsc{year 3}};
\begin{axis}[
title style={align=center, yshift=-0.35em},
ybar,
ymin={60}, ymax={93.5},
width={8cm}, height={5cm},
bar width={7pt},
ticks=both,
ytick={60,70,80,90,100.0},
ylabel style={yshift=-0.65em},
ylabel={Percent},
ymajorgrids,
nodes near coords = \rotatebox{90}{{\pgfmathprintnumber[fixed zerofill, precision=2]{\pgfplotspointmeta}}},
nodes near coords align={vertical},
every node near coord/.append style={font=\small, yshift=0.25mm},
xtick = data,
table/header=false,
table/row sep=\\,
xticklabels={},
enlarge y limits={value=0.2,upper},
legend style={font=\tiny},
%legend pos=north west
%legend style={at={(0.5, 1.20)}, anchor=north, legend columns=2},
legend style={at={(0.5, -0.52)}, anchor=north, legend columns=3},
]
\addplot table[x expr=\coordindex,y index=0]{89.17\\71.71\\81.41\\};
\addplot table[x expr=\coordindex,y index=0]{70.60\\64.11\\84.92\\};
\draw (axis cs:0.52,0) -- ({axis cs:0.52,0}|-{rel axis cs:0.5,1});
\draw (axis cs:1.49,0) -- ({axis cs:1.49,0}|-{rel axis cs:0.5,1});
\draw (axis cs:2.50,0) -- ({axis cs:2.50,0}|-{rel axis cs:0.5,1});
%\fill[green] (axis cs:0,85) circle(3pt) node [right,rotate=90] {85};
\fill[green] (axis cs:1,90) circle(3pt) node [right,rotate=90] {90};
\fill[green] (axis cs:2,70) circle(3pt) node [right,rotate=90] {70};
\legend{\textsc{Prod 1}\hspace*{8pt}, \textsc{Prod 2}}
\addlegendimage{draw=green,fill=green!30}
\addlegendentry{\textsc{Legend}}
\end{axis}
\begin{axis}[
axis y line*={right},
axis x line={none},
ymin={1}, ymax={5},
xmin=0,xmax=5,
width={8cm}, height={5cm},
ylabel style={xshift=0.60em},
ylabel={units},
]
\end{axis}
\end{tikzpicture}
\end{document}