根据这个问题,这对于条形图来说非常完美,我想在堆积条形图中绘制一条水平线。因此,以下是代码:
\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}