如何在带有符号 x 轴标签的堆积条形图中绘制水平线?

如何在带有符号 x 轴标签的堆积条形图中绘制水平线?

根据这个问题,这对于条形图来说非常完美,我想在堆积条形图中绘制一条水平线。因此,以下是代码:

\documentclass[a4paper,10pt]{report}

\usepackage{pgfplots}
\usetikzlibrary{patterns}


\begin{document}

\begin{figure}
 \centering

\begin{tikzpicture}
\begin{axis}[width=11cm,
ybar stacked,
enlargelimits=0.3,legend style={at={(0.98,0.98)},
cells={anchor=west}
},
legend entries={Straight,Unseamed,Zigzag,Requirement},
bar width=1cm,legend columns=2,
ylabel={Tensile strength{,} $N\,mm^{-1}$},
symbolic x coords={Plain,Single lap,Double lap},
xtick=data,
ytick={0,2,4,6,8,10,12,14},
x tick label style={rotate=45,anchor=east},
]

\addplot [draw=black,pattern=crosshatch dots,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt}] 
coordinates
{
(Plain,7)+-(0.41,0.41)
(Single lap,11)+-(0.27,0.27)
(Double lap,12)+-(0.47,0.47)
};

\addplot [draw=black,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt}] 
coordinates
{
(Plain,4)+-(0.31,0.31)
(Single lap,5)+-(0.27,0.27)
(Double lap,6)+-(0.38,0.38)
};

\addplot[black,sharp plot]
coordinates {(Plain,10.857) (Double lap,10.857)}
;

\end{axis}
\end{tikzpicture}

\end{figure}

\end{document}

结果是:

在此处输入图片描述

但我需要的是一条从 x 轴起点到终点的直水平线:

在此处输入图片描述

我怎样才能做到这一点?

答案1

欢迎使用!ybar stacked仅用于真正需要的地块。

\documentclass[a4paper,10pt]{report}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{patterns}
\usepackage{siunitx}

\begin{document}

\begin{figure}
 \centering

\begin{tikzpicture}
\begin{axis}[width=11cm,
enlargelimits=0.3,legend style={at={(0.98,0.98)},
cells={anchor=west}
},
legend entries={Straight,Unseamed,Zigzag,Requirement},
bar width=1cm,legend columns=2,
ylabel={Tensile strength{,} \si{\newton\per\milli\meter}},
symbolic x coords={Plain,Single lap,Double lap},
xtick=data,
ytick={0,2,4,6,8,10,12,14},
x tick label style={rotate=45,anchor=east},
]

\addplot [ybar stacked,draw=black,pattern=crosshatch dots,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt}] 
coordinates
{
(Plain,7)+-(0.41,0.41)
(Single lap,11)+-(0.27,0.27)
(Double lap,12)+-(0.47,0.47)
};

\addplot [ybar stacked,draw=black,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt}] 
coordinates
{
(Plain,4)+-(0.31,0.31)
(Single lap,5)+-(0.27,0.27)
(Double lap,6)+-(0.38,0.38)
};

\addplot[black,sharp plot]
coordinates {(Plain,10.857) (Double lap,10.857)}
;

\end{axis}
\end{tikzpicture}

\end{figure}

\end{document}

在此处输入图片描述

题外话:人们通常将单元排版成直立形式,并且siunitx可以使用该包来实现这一点。

要在轴上画一条线,您不需要绘图,而只需要一个\draw命令。

\documentclass[a4paper,10pt]{report}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{patterns}
\usepackage{siunitx}

\begin{document}

\begin{figure}
 \centering

\begin{tikzpicture}
\begin{axis}[width=11cm,
enlargelimits=0.3,legend style={at={(0.98,0.98)},
cells={anchor=west}
},
legend entries={Straight,Unseamed,Zigzag,Requirement},
bar width=1cm,legend columns=2,
ylabel={Tensile strength{,} \si{\newton\per\milli\meter}},
symbolic x coords={Plain,Single lap,Double lap},
xtick=data,
ytick={0,2,4,6,8,10,12,14},
x tick label style={rotate=45,anchor=east},
]

\addplot [ybar stacked,draw=black,pattern=crosshatch dots,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt}] 
coordinates
{
(Plain,7)+-(0.41,0.41)
(Single lap,11)+-(0.27,0.27)
(Double lap,12)+-(0.47,0.47)
};

\addplot [ybar stacked,draw=black,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt}] 
coordinates
{
(Plain,4)+-(0.31,0.31)
(Single lap,5)+-(0.27,0.27)
(Double lap,6)+-(0.38,0.38)
};

\path (axis cs:{[normalized]1},10.857) coordinate (aux);

\end{axis}
\draw (current axis.west|-aux) -- (current axis.east|-aux);
\end{tikzpicture}

\end{figure}

\end{document}

在此处输入图片描述

相关内容