想要在 tikzpicture 条形图中从 y=98.23(附图中用箭头指向)开始绘制一条虚线和水平线。
以下是代码
\documentclass[10pt,journal,compsoc]{IEEEtran}
\usepackage{pgfplots}
\usepackage{subfig}
\usetikzlibrary{positioning,shapes,arrows,shadows,patterns}
% correct bad hyphenation here
\hyphenation{op-tical net-works semi-conduc-tor}
\begin{document}
\begin{figure}
\centering
\captionsetup{justification = centering}
\subfloat[xxx]{
\label{11(a)}
\begin{tikzpicture}[scale =.5, transform shape]
\begin{axis}[
major x tick style = transparent,
ybar = 2*\pgflinewidth,
bar width=7pt,
ymajorgrids = true,
ylabel={xxx(\%)},
symbolic x coords={xx, yy, zz, ll},
xtick=data,
scaled y ticks = false,
enlarge x limits=0.25,
ymin=95,
minor y tick num=5,
xlabel style={yshift=-.3cm},
ylabel style={yshift=-.3cm},
tick label style={font=\footnotesize},
legend style={at={(.5,-0.1)}, anchor=north, /tikz/every even
column/.append style={column sep=.5cm}},
legend columns = -1,
]
\addplot[style = {fill=red, mark=none, postaction={pattern=dots}},
error bars/.cd, y dir=both, y explicit, error bar style ={thick}]
coordinates {
(xx, 97.772418) +- (0.00477703, 0.00477703)
(yy, 96.83437) +- (0.039005931, 0.039005931)
(zz, 96.9428) +- (0.05470002, 0.05470002)
(ll, 96.72381) +- (0.023826187, 0.023826187)};
\addplot[style = {fill=blue, mark=none, postaction={pattern=north
east lines}}, error bars/.cd, y dir=both, y explicit, error bar style =
{thick}]
coordinates {
(xx, 97.615588) +- (0.036178026, 0.036178026)
(yy, 96.77373) +- (0.045379505, 0.045379505)
(zz, 97.61842) +- (0.036159489, 0.036159489)
(ll, 96.64351) +- (0.044978336, 0.044978336)};
\addplot[style = {fill=green, mark=none, postaction=
{pattern=horizontal lines}}, error bars/.cd, y dir=both, y explicit, error
bar style ={thick}]
coordinates {
(xx, 98.856128) +- (0.031866164, 0.031866164)
(yy, 99.00258) +- (0.026840445, 0.026840445)
(zz, 98.02313) +- (0.057913644, 0.057913644)
(ll, 98.30856) +- (0.048969882, 0.048969882)};
\addplot[style = {fill=yellow, mark=none, postaction=
{pattern=vertical lines}}, error bars/.cd, y dir=both, y explicit, error bar
style ={thick}]
coordinates {
(xx, 97.778428) +- (0.035212399, 0.035212399)
(yy, 96.82722) +- (0.045398952, 0.045398952)
(zz, 97.75243) +- (0.034266698, 0.034266698)
(ll, 96.72982) +- (0.043016218, 0.043016218)};
\legend{xxx, xxx, xxx, xxx}
\end{axis}
\end{tikzpicture}
}
\end{figure}
\end{document}
答案1
除了 Torbjørn T. 在在问题下方评论您还可以添加一个extra y tick
以获得所需的结果。唯一棘手的部分是将此线移到条形图之前(此时“正常”网格线仍应位于条形图后面)。
(我还“优化”了你的代码,现在应该更简单并且希望更具可读性。)
有关详细信息,请查看代码中的注释。
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
% use this `compat' level or higher to make use of the advanced label positioning
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}[
scale =.5,
transform shape,
]
\begin{axis}[
ymin=95,
ylabel={xxx (\%)},
ybar=2*\pgflinewidth,
bar width=7pt,
% ----------
% (adapted. This looks a bit strange, but currently has to be done
% in this way because of <https://sourceforge.net/p/pgfplots/bugs/182/>.)
major tick length=0pt,
ytick style={
/pgfplots/major tick length=1.5mm,
},
% ----------
ymajorgrids=true,
symbolic x coords={xx, yy, zz, ll},
xtick=data,
% align the xtick labels at the baseline
typeset ticklabels with strut,
enlarge x limits=0.25,
minor y tick num=5,
legend columns = -1,
legend style={
at={(xticklabel cs:.5)}, % <-- (adapted)
anchor=north,
/tikz/every even column/.append style={
column sep=.5cm,
},
},
% ---------------------------------------------------------------------
% added stuff
% ---------------------------------------------------------------------
% allow different layers
set layers,
% (needed because of bug <https://sourceforge.net/p/pgfplots/bugs/153/>)
cell picture=true,
% now add the "horizontal line" ...
extra y ticks=98.23,
% ... don't show any label and ...
extra y tick labels={},
% ... adapt the style to your needs
extra y tick style={
% in case you should remove the grid from the "normal" ticks ...
ymajorgrids=true,
% ... but don't show an extra tick (line)
ytick style={
/pgfplots/major tick length=0pt,
},
grid style={
red,
dashed,
% to draw this line before the bars, move it a higher layer
/pgfplots/on layer=axis foreground,
},
},
% ---------------------------------------------------------------------
% moved common options from the `\addplot' commands here
mark=none,
error bars/y dir=both,
error bars/y explicit,
error bars/error bar style={
thick,
},
]
\addplot [
fill=red,
postaction={pattern=dots},
] coordinates {
(xx, 97.772418) +- (0.00477703, 0.00477703)
(yy, 96.83437) +- (0.039005931, 0.039005931)
(zz, 96.9428) +- (0.05470002, 0.05470002)
(ll, 96.72381) +- (0.023826187, 0.023826187)
};
\addplot [
fill=blue,
postaction={pattern=north east lines},
] coordinates {
(xx, 97.615588) +- (0.036178026, 0.036178026)
(yy, 96.77373) +- (0.045379505, 0.045379505)
(zz, 97.61842) +- (0.036159489, 0.036159489)
(ll, 96.64351) +- (0.044978336, 0.044978336)
};
\addplot [
fill=green,
postaction={pattern=horizontal lines},
] coordinates {
(xx, 98.856128) +- (0.031866164, 0.031866164)
(yy, 99.00258) +- (0.026840445, 0.026840445)
(zz, 98.02313) +- (0.057913644, 0.057913644)
(ll, 98.30856) +- (0.048969882, 0.048969882)
};
\addplot [
fill=yellow,
postaction={pattern=vertical lines},
] coordinates {
(xx, 97.778428) +- (0.035212399, 0.035212399)
(yy, 96.82722) +- (0.045398952, 0.045398952)
(zz, 97.75243) +- (0.034266698, 0.034266698)
(ll, 96.72982) +- (0.043016218, 0.043016218)
};
\legend{xxx, xxx, xxx, xxx}
\end{axis}
\end{tikzpicture}
\end{document}